Skip to content

Commit

Permalink
[UNDERTOW-2532] Modify it to be thread safe when transmitting text or…
Browse files Browse the repository at this point in the history
… binary message to websocket session
  • Loading branch information
raccoonback committed Nov 29, 2024
1 parent a4720e3 commit 3a105d1
Showing 1 changed file with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ class BasicWebSocketSessionRemoteEndpoint implements Basic {
private StreamSinkFrameChannel textFrameSender;

public void assertNotInFragment() {
if (textFrameSender != null || binaryFrameSender != null) {
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
synchronized (this) {
if (textFrameSender != null || binaryFrameSender != null) {
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
}
}
}

Expand Down Expand Up @@ -273,50 +275,55 @@ public void sendText(final String partialMessage, final boolean isLast) throws I
if(partialMessage == null) {
throw JsrWebSocketMessages.MESSAGES.messageInNull();
}
if (binaryFrameSender != null) {
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
}
if (textFrameSender == null) {
textFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.TEXT);
}
try {
Channels.writeBlocking(textFrameSender, WebSocketUtils.fromUtf8String(partialMessage));
if(isLast) {
textFrameSender.shutdownWrites();

synchronized (this) {
if (binaryFrameSender != null) {
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
}
if(textFrameSender == null) {
textFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.TEXT);
}
Channels.flushBlocking(textFrameSender);
} finally {
if (isLast) {
textFrameSender = null;
try {
Channels.writeBlocking(textFrameSender, WebSocketUtils.fromUtf8String(partialMessage));
if(isLast) {
textFrameSender.shutdownWrites();
}
Channels.flushBlocking(textFrameSender);
} finally {
if(isLast) {
textFrameSender = null;
}
}
}

}

@Override
public void sendBinary(final ByteBuffer partialByte, final boolean isLast) throws IOException {

if(partialByte == null) {
throw JsrWebSocketMessages.MESSAGES.messageInNull();
}
if (textFrameSender != null) {
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
}
if (binaryFrameSender == null) {
binaryFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.BINARY);
}
try {
Channels.writeBlocking(binaryFrameSender, partialByte);
if(isLast) {
binaryFrameSender.shutdownWrites();

synchronized (this) {
if (textFrameSender != null) {
throw JsrWebSocketMessages.MESSAGES.cannotSendInMiddleOfFragmentedMessage();
}
if (binaryFrameSender == null) {
binaryFrameSender = undertowSession.getWebSocketChannel().send(WebSocketFrameType.BINARY);
}
try {
Channels.writeBlocking(binaryFrameSender, partialByte);
if (isLast) {
binaryFrameSender.shutdownWrites();
}
Channels.flushBlocking(binaryFrameSender);
}
Channels.flushBlocking(binaryFrameSender);
} finally {
if (isLast) {
binaryFrameSender = null;
finally {
if (isLast) {
binaryFrameSender = null;
}
}
partialByte.clear();
}
partialByte.clear();
}

@Override
Expand Down

0 comments on commit 3a105d1

Please sign in to comment.