Skip to content

Commit 2c10852

Browse files
committed
made SSLSocketChannel2.java work both in blocking and nonblocking mode (TooTallNate#154)
1 parent b6ce887 commit 2c10852

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/main/java/org/java_websocket/SSLSocketChannel2.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class SSLSocketChannel2 implements ByteChannel, WrappedByteChannel {
5151
protected SSLEngine sslEngine;
5252
protected final boolean isblocking;
5353

54+
private Status status = Status.BUFFER_UNDERFLOW;
55+
5456
public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , ExecutorService exec , SelectionKey key ) throws IOException {
5557
this.sc = channel;
5658

@@ -106,12 +108,14 @@ private synchronized void processHandshake() throws IOException {
106108
}
107109

108110
if( res.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP ) {
109-
inCrypt.compact();
110-
int read = sc.read( inCrypt );
111-
if( read == -1 ) {
112-
throw new IOException( "connection closed unexpectedly by peer" );
111+
if( !isblocking || status == Status.BUFFER_UNDERFLOW ) {
112+
inCrypt.compact();
113+
int read = sc.read( inCrypt );
114+
if( read == -1 ) {
115+
throw new IOException( "connection closed unexpectedly by peer" );
116+
}
117+
inCrypt.flip();
113118
}
114-
inCrypt.flip();
115119
inData.compact();
116120
unwrap();
117121
}
@@ -134,7 +138,8 @@ private synchronized ByteBuffer unwrap() throws SSLException {
134138
do {
135139
rem = inData.remaining();
136140
res = sslEngine.unwrap( inCrypt, inData );
137-
} while ( res.getStatus() == SSLEngineResult.Status.OK && (rem != inData.remaining() || res.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP));
141+
status = res.getStatus();
142+
} while ( status == SSLEngineResult.Status.OK && ( rem != inData.remaining() || res.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP ) );
138143

139144
inData.flip();
140145
return inData;
@@ -173,14 +178,16 @@ public int write( ByteBuffer src ) throws IOException {
173178
public int read( ByteBuffer dst ) throws IOException {
174179
if( !dst.hasRemaining() )
175180
return 0;
176-
if( isBlocking() ) {
177-
while ( !isHandShakeComplete() ) {
181+
if( !isHandShakeComplete() ) {
182+
if( isBlocking() ) {
183+
while ( !isHandShakeComplete() ) {
184+
processHandshake();
185+
}
186+
} else {
178187
processHandshake();
179-
}
180-
} else {
181-
processHandshake();
182-
if( !isHandShakeComplete() ) {
183-
return 0;
188+
if( !isHandShakeComplete() ) {
189+
return 0;
190+
}
184191
}
185192
}
186193

@@ -196,9 +203,10 @@ public int read( ByteBuffer dst ) throws IOException {
196203
else
197204
inCrypt.compact();
198205

199-
if( sc.read( inCrypt ) == -1 ) {
200-
return -1;
201-
}
206+
if( ( isblocking && inCrypt.position() == 0 ) || status == Status.BUFFER_UNDERFLOW )
207+
if( sc.read( inCrypt ) == -1 ) {
208+
return -1;
209+
}
202210
inCrypt.flip();
203211
unwrap();
204212
return transfereTo( inData, dst );
@@ -209,8 +217,8 @@ private int readRemaining( ByteBuffer dst ) throws SSLException {
209217
if( inData.hasRemaining() ) {
210218
return transfereTo( inData, dst );
211219
}
212-
assert ( !inData.hasRemaining() );
213-
inData.clear();
220+
if( !inData.hasRemaining() )
221+
inData.clear();
214222
// test if some bytes left from last read (e.g. BUFFER_UNDERFLOW)
215223
if( inCrypt.hasRemaining() ) {
216224
unwrap();

0 commit comments

Comments
 (0)