Skip to content

Commit c17693e

Browse files
committed
Create buffers according to maximum net and application data. Added a few asserts for better understanding.
1 parent 158b9bc commit c17693e

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

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

59+
protected int bufferallocations = 0;
60+
5961
public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , ExecutorService exec , SelectionKey key ) throws IOException {
6062
if( channel == null || sslEngine == null || exec == null )
6163
throw new IllegalArgumentException( "parameter must not be null" );
@@ -127,21 +129,21 @@ private synchronized void processHandshake() throws IOException {
127129
}
128130
inData.compact();
129131
unwrap();
130-
if( sslEngine.getHandshakeStatus() == HandshakeStatus.FINISHED ) {
132+
if( readEngineResult.getHandshakeStatus() == HandshakeStatus.FINISHED ) {
131133
createBuffers( sslEngine.getSession() );
132134
return;
133135
}
134136
}
135137
consumeDelegatedTasks();
136-
assert ( sslEngine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING );
137138
if( tasks.isEmpty() || sslEngine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_WRAP ) {
138139
socketChannel.write( wrap( emptybuffer ) );
139-
if( sslEngine.getHandshakeStatus() == HandshakeStatus.FINISHED ) {
140+
if( writeEngineResult.getHandshakeStatus() == HandshakeStatus.FINISHED ) {
140141
createBuffers( sslEngine.getSession() );
142+
return;
141143
}
142144
}
145+
assert ( sslEngine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING );// this function could only leave NOT_HANDSHAKING after createBuffers was called
143146
}
144-
145147
private synchronized ByteBuffer wrap( ByteBuffer b ) throws SSLException {
146148
outCrypt.compact();
147149
writeEngineResult = sslEngine.wrap( b, outCrypt );
@@ -192,13 +194,15 @@ protected void createBuffers( SSLSession session ) {
192194
inCrypt.flip();
193195
outCrypt.rewind();
194196
outCrypt.flip();
197+
bufferallocations++;
195198
}
196199

197200
public int write( ByteBuffer src ) throws IOException {
198201
if( !isHandShakeComplete() ) {
199202
processHandshake();
200203
return 0;
201204
}
205+
assert ( bufferallocations > 1 );
202206
int num = socketChannel.write( wrap( src ) );
203207
return num;
204208

@@ -225,6 +229,7 @@ public int read( ByteBuffer dst ) throws IOException {
225229
}
226230
}
227231
}
232+
assert ( bufferallocations > 1 );
228233
/* 1. When "dst" is smaller than "inData" readRemaining will fill "dst" with data decoded in a previous read call.
229234
* 2. When "inCrypt" contains more data than "inData" has remaining space, unwrap has to be called on more time(readRemaining)
230235
*/
@@ -256,7 +261,6 @@ public int read( ByteBuffer dst ) throws IOException {
256261
}
257262
return transfered;
258263
}
259-
260264
/**
261265
* {@link #read(ByteBuffer)} may not be to leave all buffers(inData, inCrypt)
262266
**/

0 commit comments

Comments
 (0)