17
17
*/
18
18
package io .undertow .websockets .jsr ;
19
19
20
+ import java .io .IOException ;
21
+ import java .io .OutputStream ;
22
+ import java .io .OutputStreamWriter ;
23
+ import java .io .Writer ;
24
+ import java .nio .ByteBuffer ;
25
+ import java .nio .charset .StandardCharsets ;
26
+ import java .util .concurrent .Future ;
27
+
20
28
import io .undertow .websockets .core .BinaryOutputStream ;
21
29
import io .undertow .websockets .core .StreamSinkFrameChannel ;
22
30
import io .undertow .websockets .core .WebSocketCallback ;
23
31
import io .undertow .websockets .core .WebSocketFrameType ;
24
32
import io .undertow .websockets .core .WebSocketUtils ;
25
33
import io .undertow .websockets .core .WebSockets ;
26
- import org .xnio .channels .Channels ;
27
-
28
34
import jakarta .websocket .EncodeException ;
29
35
import jakarta .websocket .RemoteEndpoint ;
30
36
import jakarta .websocket .SendHandler ;
31
- import java .io .IOException ;
32
- import java .io .OutputStream ;
33
- import java .io .OutputStreamWriter ;
34
- import java .io .Writer ;
35
- import java .nio .ByteBuffer ;
36
- import java .nio .charset .StandardCharsets ;
37
- import java .util .concurrent .Future ;
37
+ import org .xnio .channels .Channels ;
38
38
39
39
/**
40
40
* {@link RemoteEndpoint} implementation which uses a WebSocketSession for all its operation.
@@ -243,11 +243,9 @@ class BasicWebSocketSessionRemoteEndpoint implements Basic {
243
243
private StreamSinkFrameChannel binaryFrameSender ;
244
244
private StreamSinkFrameChannel textFrameSender ;
245
245
246
- public void assertNotInFragment () {
247
- synchronized (this ) {
248
- if (textFrameSender != null || binaryFrameSender != null ) {
249
- throw JsrWebSocketMessages .MESSAGES .cannotSendInMiddleOfFragmentedMessage ();
250
- }
246
+ public synchronized void assertNotInFragment () {
247
+ if (textFrameSender != null || binaryFrameSender != null ) {
248
+ throw JsrWebSocketMessages .MESSAGES .cannotSendInMiddleOfFragmentedMessage ();
251
249
}
252
250
}
253
251
@@ -270,29 +268,23 @@ public void sendBinary(final ByteBuffer data) throws IOException {
270
268
data .clear (); //for some reason the TCK expects this, might as well just match the RI behaviour
271
269
}
272
270
273
- @ Override
271
+ @ Override
274
272
public void sendText (final String partialMessage , final boolean isLast ) throws IOException {
275
- if (partialMessage == null ) {
273
+ if (partialMessage == null ) {
276
274
throw JsrWebSocketMessages .MESSAGES .messageInNull ();
277
275
}
278
276
279
- synchronized ( this ) {
280
- if ( binaryFrameSender != null ) {
281
- throw JsrWebSocketMessages . MESSAGES . cannotSendInMiddleOfFragmentedMessage ();
282
- }
283
- if ( textFrameSender == null ) {
284
- textFrameSender = undertowSession . getWebSocketChannel (). send ( WebSocketFrameType . TEXT );
277
+ StreamSinkFrameChannel sender = getTextFrameSender ();
278
+
279
+ try {
280
+ Channels . writeBlocking ( sender , WebSocketUtils . fromUtf8String ( partialMessage ));
281
+ if ( isLast ) {
282
+ sender . shutdownWrites ( );
285
283
}
286
- try {
287
- Channels .writeBlocking (textFrameSender , WebSocketUtils .fromUtf8String (partialMessage ));
288
- if (isLast ) {
289
- textFrameSender .shutdownWrites ();
290
- }
291
- Channels .flushBlocking (textFrameSender );
292
- } finally {
293
- if (isLast ) {
294
- textFrameSender = null ;
295
- }
284
+ Channels .flushBlocking (sender );
285
+ } finally {
286
+ if (isLast ) {
287
+ clearTextFrameSender ();
296
288
}
297
289
}
298
290
}
@@ -303,27 +295,49 @@ public void sendBinary(final ByteBuffer partialByte, final boolean isLast) throw
303
295
throw JsrWebSocketMessages .MESSAGES .messageInNull ();
304
296
}
305
297
306
- synchronized (this ) {
307
- if (textFrameSender != null ) {
308
- throw JsrWebSocketMessages .MESSAGES .cannotSendInMiddleOfFragmentedMessage ();
309
- }
310
- if (binaryFrameSender == null ) {
311
- binaryFrameSender = undertowSession .getWebSocketChannel ().send (WebSocketFrameType .BINARY );
312
- }
313
- try {
314
- Channels .writeBlocking (binaryFrameSender , partialByte );
315
- if (isLast ) {
316
- binaryFrameSender .shutdownWrites ();
317
- }
318
- Channels .flushBlocking (binaryFrameSender );
298
+ StreamSinkFrameChannel sender = getBinaryFrameSender ();
299
+
300
+ try {
301
+ Channels .writeBlocking (sender , partialByte );
302
+ if (isLast ) {
303
+ sender .shutdownWrites ();
319
304
}
320
- finally {
321
- if (isLast ) {
322
- binaryFrameSender = null ;
323
- }
305
+ Channels .flushBlocking (sender );
306
+ }
307
+ finally {
308
+ if (isLast ) {
309
+ clearBinaryFrameSender ();
324
310
}
325
- partialByte .clear ();
326
311
}
312
+ partialByte .clear ();
313
+ }
314
+
315
+ private synchronized StreamSinkFrameChannel getTextFrameSender () throws IOException {
316
+ if (binaryFrameSender != null ) {
317
+ throw JsrWebSocketMessages .MESSAGES .cannotSendInMiddleOfFragmentedMessage ();
318
+ }
319
+ if (textFrameSender == null ) {
320
+ textFrameSender = undertowSession .getWebSocketChannel ().send (WebSocketFrameType .TEXT );
321
+ }
322
+ return textFrameSender ;
323
+ }
324
+
325
+ private synchronized void clearTextFrameSender () {
326
+ textFrameSender = null ;
327
+ }
328
+
329
+ private synchronized StreamSinkFrameChannel getBinaryFrameSender () throws IOException {
330
+ if (textFrameSender != null ) {
331
+ throw JsrWebSocketMessages .MESSAGES .cannotSendInMiddleOfFragmentedMessage ();
332
+ }
333
+ if (binaryFrameSender == null ) {
334
+ binaryFrameSender = undertowSession .getWebSocketChannel ().send (WebSocketFrameType .BINARY );
335
+ }
336
+ return binaryFrameSender ;
337
+ }
338
+
339
+ private synchronized void clearBinaryFrameSender () {
340
+ binaryFrameSender = null ;
327
341
}
328
342
329
343
@ Override
0 commit comments