Skip to content

Commit 691e41c

Browse files
Merge pull request #701 from YuryAndr/support_underflow_handling_without_thread_sleep
Support underflow handling without thread sleep
2 parents 281b679 + 217a5e8 commit 691e41c

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/main/java/com/rabbitmq/client/impl/nio/FrameBuilder.java

+5
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,9 @@ private void handleProtocolVersionMismatch() throws IOException {
200200
}
201201
throw x;
202202
}
203+
204+
//Indicates ssl underflow state - means that cipherBuffer should aggregate next chunks of bytes
205+
public boolean isUnderflowHandlingEnabled() {
206+
return false;
207+
}
203208
}

src/main/java/com/rabbitmq/client/impl/nio/SocketChannelFrameHandlerState.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.channels.SelectionKey;
2828
import java.nio.channels.SocketChannel;
2929

30+
3031
/**
3132
*
3233
*/
@@ -176,11 +177,14 @@ void endWriteSequence() {
176177

177178
void prepareForReadSequence() throws IOException {
178179
if(ssl) {
179-
cipherIn.clear();
180-
plainIn.clear();
180+
if (!frameBuilder.isUnderflowHandlingEnabled()) {
181+
cipherIn.clear();
182+
cipherIn.flip();
183+
}
181184

182-
cipherIn.flip();
185+
plainIn.clear();
183186
plainIn.flip();
187+
184188
} else {
185189
NioHelper.read(channel, plainIn);
186190
plainIn.flip();
@@ -189,6 +193,15 @@ void prepareForReadSequence() throws IOException {
189193

190194
boolean continueReading() throws IOException {
191195
if(ssl) {
196+
if (frameBuilder.isUnderflowHandlingEnabled()) {
197+
int bytesRead = NioHelper.read(channel, cipherIn);
198+
if (bytesRead == 0) {
199+
return false;
200+
} else {
201+
cipherIn.flip();
202+
return true;
203+
}
204+
}
192205
if (!plainIn.hasRemaining() && !cipherIn.hasRemaining()) {
193206
// need to try to read something
194207
cipherIn.clear();

src/main/java/com/rabbitmq/client/impl/nio/SslEngineFrameBuilder.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.ByteBuffer;
2323
import java.nio.channels.ReadableByteChannel;
2424

25+
2526
/**
2627
* Sub-class of {@link FrameBuilder} that unwraps crypted data from the network.
2728
* @since 4.4.0
@@ -32,6 +33,8 @@ public class SslEngineFrameBuilder extends FrameBuilder {
3233

3334
private final ByteBuffer cipherBuffer;
3435

36+
private boolean isUnderflowHandlingEnabled = false;
37+
3538
public SslEngineFrameBuilder(SSLEngine sslEngine, ByteBuffer plainIn, ByteBuffer cipherIn, ReadableByteChannel channel) {
3639
super(channel, plainIn);
3740
this.sslEngine = sslEngine;
@@ -40,12 +43,14 @@ public SslEngineFrameBuilder(SSLEngine sslEngine, ByteBuffer plainIn, ByteBuffer
4043

4144
@Override
4245
protected boolean somethingToRead() throws IOException {
43-
if (applicationBuffer.hasRemaining()) {
46+
if (applicationBuffer.hasRemaining() && !isUnderflowHandlingEnabled) {
4447
return true;
4548
} else {
4649
applicationBuffer.clear();
4750

48-
while (true) {
51+
boolean underflowHandling = false;
52+
53+
try {
4954
SSLEngineResult result = sslEngine.unwrap(cipherBuffer, applicationBuffer);
5055
switch (result.getStatus()) {
5156
case OK:
@@ -59,19 +64,23 @@ protected boolean somethingToRead() throws IOException {
5964
throw new SSLException("buffer overflow in read");
6065
case BUFFER_UNDERFLOW:
6166
cipherBuffer.compact();
62-
int read = NioHelper.read(channel, cipherBuffer);
63-
if (read == 0) {
64-
return false;
65-
}
66-
cipherBuffer.flip();
67-
break;
67+
underflowHandling = true;
68+
return false;
6869
case CLOSED:
6970
throw new SSLException("closed in read");
7071
default:
7172
throw new IllegalStateException("Invalid SSL status: " + result.getStatus());
72-
}
73+
}
74+
} finally {
75+
isUnderflowHandlingEnabled = underflowHandling;
7376
}
77+
78+
return false;
7479
}
7580
}
7681

82+
@Override
83+
public boolean isUnderflowHandlingEnabled() {
84+
return isUnderflowHandlingEnabled;
85+
}
7786
}

0 commit comments

Comments
 (0)