Skip to content

Commit effeb32

Browse files
committed
pragmatic fix for TooTallNate#190
1 parent fe11114 commit effeb32

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public class SSLSocketChannel2 implements ByteChannel, WrappedByteChannel {
5656
protected SSLEngineResult readEngineResult;
5757
protected SSLEngineResult writeEngineResult;
5858

59+
/**
60+
* Should be used to count the buffer allocations.
61+
* But because of #190 where HandshakeStatus.FINISHED is not properly returned by nio wrap/unwrap this variable is used to check whether {@link #createBuffers(SSLSession)} needs to be called.
62+
**/
5963
protected int bufferallocations = 0;
6064

6165
public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , ExecutorService exec , SelectionKey key ) throws IOException {
@@ -142,7 +146,9 @@ private synchronized void processHandshake() throws IOException {
142146
return;
143147
}
144148
}
145-
assert ( sslEngine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING );// this function could only leave NOT_HANDSHAKING after createBuffers was called
149+
assert ( sslEngine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING );// this function could only leave NOT_HANDSHAKING after createBuffers was called unless #190 occurs which means that nio wrap/unwrap never return HandshakeStatus.FINISHED
150+
151+
bufferallocations = 1; // look at variable declaration why this line exists and #190. Without this line buffers would not be be recreated when #190 AND a rehandshake occur.
146152
}
147153
private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException {
148154
outCrypt.compact();
@@ -202,7 +208,10 @@ public int write( ByteBuffer src ) throws IOException {
202208
processHandshake();
203209
return 0;
204210
}
205-
assert ( bufferallocations > 1 );
211+
// assert ( bufferallocations > 1 ); //see #190
212+
if( bufferallocations <= 1 ) {
213+
createBuffers( sslEngine.getSession() );
214+
}
206215
int num = socketChannel.write( wrap( src ) );
207216
return num;
208217

@@ -229,7 +238,10 @@ public int read( ByteBuffer dst ) throws IOException {
229238
}
230239
}
231240
}
232-
assert ( bufferallocations > 1 );
241+
// assert ( bufferallocations > 1 ); //see #190
242+
if( bufferallocations <= 1 ) {
243+
createBuffers( sslEngine.getSession() );
244+
}
233245
/* 1. When "dst" is smaller than "inData" readRemaining will fill "dst" with data decoded in a previous read call.
234246
* 2. When "inCrypt" contains more data than "inData" has remaining space, unwrap has to be called on more time(readRemaining)
235247
*/

0 commit comments

Comments
 (0)