1
1
package one .xio ;
2
2
3
+ import one .xio .AsioVisitor .FSM .sslBacklog ;
4
+ import one .xio .AsioVisitor .Helper .F ;
5
+
3
6
import javax .net .ssl .SSLEngine ;
4
7
import javax .net .ssl .SSLEngineResult ;
5
8
import javax .net .ssl .SSLEngineResult .HandshakeStatus ;
26
29
* Time: 11:50 PM
27
30
*/
28
31
public interface AsioVisitor {
29
- boolean $DBG = null != System . getenv ( "DEBUG_VISITOR_ORIGINS" );
32
+ boolean $DBG = "true" . equals ( Config . get ( "DEBUG_VISITOR_ORIGINS" , "false" ) );
30
33
Map <Impl , String > $origins = $DBG
31
34
? new WeakHashMap <Impl , String >()
32
35
: null ;
@@ -93,6 +96,7 @@ public void run() {
93
96
94
97
try {
95
98
key .interestOps (0 );
99
+ assert executorService !=null :"must install FSM executorService!" ;
96
100
executorService .invokeAll (runnables );
97
101
} catch (InterruptedException e ) {
98
102
e .printStackTrace ();
@@ -108,11 +112,12 @@ public static void setExecutorService(ExecutorService svc) {
108
112
* this is a beast.
109
113
*/
110
114
public static void handShake (final Pair <SelectionKey , SSLEngine > state ) {
111
-
115
+ if (! state . getA (). isValid ()) return ;
112
116
SSLEngine sslEngine = state .getB ();
113
117
114
118
HandshakeStatus handshakeStatus = sslEngine .getHandshakeStatus ();
115
119
System .err .println ("hs: " + handshakeStatus );
120
+
116
121
switch (handshakeStatus ) {
117
122
case NEED_TASK :
118
123
delegateTasks (state , new Runnable () {
@@ -185,12 +190,13 @@ private static void needNetUnwrap(final Pair<SelectionKey, SSLEngine> state) {
185
190
final ByteBuffer fromNet = sslBacklog .fromNet .resume (state );
186
191
final ByteBuffer toApp = sslBacklog .toApp .resume (state );
187
192
188
- Helper .toRead (key , new Helper . F () {
193
+ Helper .toRead (key , new F () {
189
194
public void apply (SelectionKey key ) throws Exception {
190
195
int read = ((SocketChannel ) key .channel ()).read (fromNet );
191
196
System .err .println ("hsread: " + read );
192
197
SSLEngine sslEngine = state .getB ();
193
-
198
+ System .err .println ("nunwrap1a: " + fromNet .toString ());
199
+ System .err .println ("?<< https://" +UTF_8 .decode ((ByteBuffer ) fromNet .duplicate ().flip ()));//fromNet.flip()
194
200
SSLEngineResult unwrap = sslEngine .unwrap ((ByteBuffer ) fromNet .flip (), toApp );
195
201
System .err .println ("nunwrap2:" + unwrap );
196
202
System .err .println ("nunwrap2a: " + fromNet .toString ());
@@ -451,6 +457,7 @@ public static int read(Channel channel, ByteBuffer fromNet) throws IOException {
451
457
}
452
458
453
459
public static int read (SelectionKey key , ByteBuffer dest ) throws IOException {
460
+ if (!key .isValid ())return -1 ;
454
461
SocketChannel channel = (SocketChannel ) key .channel ();
455
462
int origin = dest .position ();
456
463
@@ -459,9 +466,10 @@ public static int read(SelectionKey key, ByteBuffer dest) throws IOException {
459
466
if (null != sslEngine ) {
460
467
461
468
462
- ByteBuffer fromNet = FSM . sslBacklog .fromNet .resume (pair (key , sslEngine ));
463
- ByteBuffer toApp = FSM . sslBacklog .toApp .resume (pair (key , sslEngine ));
469
+ ByteBuffer fromNet = sslBacklog .fromNet .resume (pair (key , sslEngine ));
470
+ ByteBuffer toApp = sslBacklog .toApp .resume (pair (key , sslEngine ));
464
471
int read1 = channel .read (fromNet );
472
+ if (-1 ==read1 )key .cancel ();
465
473
System .err .println ("r1: " +read1 );
466
474
System .err .println ("r1a: from " +fromNet );
467
475
System .err .println ("r1b: dest " +dest );
@@ -522,7 +530,7 @@ public static int write(SelectionKey key, ByteBuffer fromApp) throws IOException
522
530
int write = 0 ;
523
531
if (null != sslEngine ) {
524
532
try {
525
- ByteBuffer toNet = FSM . sslBacklog .toNet .resume (pair (key , sslEngine ));
533
+ ByteBuffer toNet = sslBacklog .toNet .resume (pair (key , sslEngine ));
526
534
System .err .println ("w:fa:0 " + fromApp );
527
535
System .err .println ("w:tn:0 " + toNet );
528
536
SSLEngineResult sslEngineResult = sslEngine .wrap (fromApp , toNet );
@@ -540,12 +548,11 @@ public static int write(SelectionKey key, ByteBuffer fromApp) throws IOException
540
548
switch (handshakeStatus ) {
541
549
case NOT_HANDSHAKING :
542
550
case FINISHED :
543
- write = (( SocketChannel ) key . channel ()). write (( ByteBuffer ) toNet . flip () );
544
- System .err .println ("w:w:2 " + write );
551
+ write = sslEngineResult . bytesConsumed ( );
552
+ System .err .println ("w:w:2 " + (( SocketChannel ) key . channel ()). write (( ByteBuffer ) toNet . flip ()) );
545
553
System .err .println ("w:tn:2 " + toNet );
546
554
toNet .compact ();
547
555
System .err .println ("w:tn:3 " + toNet );
548
-
549
556
break ;
550
557
case NEED_TASK :
551
558
case NEED_WRAP :
0 commit comments