39
39
/**
40
40
* An <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>-compatible WebSocket frame writer.
41
41
*
42
- * <p>This class is partially thread safe. Only a single "main" thread should be sending messages
43
- * via calls to {@link #newMessageSink}, {@link #writePing}, or {@link #writeClose}. Other threads
44
- * may call {@link #writePing}, {@link #writePong}, or {@link #writeClose} which will interleave on
45
- * the wire with frames from the "main" sending thread.
42
+ * <p>This class is not thread safe.
46
43
*/
47
44
final class WebSocketWriter {
48
45
final boolean isClient ;
@@ -75,16 +72,12 @@ final class WebSocketWriter {
75
72
76
73
/** Send a ping with the supplied {@code payload}. */
77
74
void writePing (ByteString payload ) throws IOException {
78
- synchronized (this ) {
79
- writeControlFrameSynchronized (OPCODE_CONTROL_PING , payload );
80
- }
75
+ writeControlFrame (OPCODE_CONTROL_PING , payload );
81
76
}
82
77
83
78
/** Send a pong with the supplied {@code payload}. */
84
79
void writePong (ByteString payload ) throws IOException {
85
- synchronized (this ) {
86
- writeControlFrameSynchronized (OPCODE_CONTROL_PONG , payload );
87
- }
80
+ writeControlFrame (OPCODE_CONTROL_PONG , payload );
88
81
}
89
82
90
83
/**
@@ -108,18 +101,14 @@ void writeClose(int code, ByteString reason) throws IOException {
108
101
payload = buffer .readByteString ();
109
102
}
110
103
111
- synchronized (this ) {
112
- try {
113
- writeControlFrameSynchronized (OPCODE_CONTROL_CLOSE , payload );
114
- } finally {
115
- writerClosed = true ;
116
- }
104
+ try {
105
+ writeControlFrame (OPCODE_CONTROL_CLOSE , payload );
106
+ } finally {
107
+ writerClosed = true ;
117
108
}
118
109
}
119
110
120
- private void writeControlFrameSynchronized (int opcode , ByteString payload ) throws IOException {
121
- assert Thread .holdsLock (this );
122
-
111
+ private void writeControlFrame (int opcode , ByteString payload ) throws IOException {
123
112
if (writerClosed ) throw new IOException ("closed" );
124
113
125
114
int length = payload .size ();
@@ -169,10 +158,8 @@ Sink newMessageSink(int formatOpcode, long contentLength) {
169
158
return frameSink ;
170
159
}
171
160
172
- void writeMessageFrameSynchronized (int formatOpcode , long byteCount , boolean isFirstFrame ,
161
+ void writeMessageFrame (int formatOpcode , long byteCount , boolean isFirstFrame ,
173
162
boolean isFinal ) throws IOException {
174
- assert Thread .holdsLock (this );
175
-
176
163
if (writerClosed ) throw new IOException ("closed" );
177
164
178
165
int b0 = isFirstFrame ? formatOpcode : OPCODE_CONTINUATION ;
@@ -235,19 +222,15 @@ final class FrameSink implements Sink {
235
222
236
223
long emitCount = buffer .completeSegmentByteCount ();
237
224
if (emitCount > 0 && !deferWrite ) {
238
- synchronized (WebSocketWriter .this ) {
239
- writeMessageFrameSynchronized (formatOpcode , emitCount , isFirstFrame , false /* final */ );
240
- }
225
+ writeMessageFrame (formatOpcode , emitCount , isFirstFrame , false /* final */ );
241
226
isFirstFrame = false ;
242
227
}
243
228
}
244
229
245
230
@ Override public void flush () throws IOException {
246
231
if (closed ) throw new IOException ("closed" );
247
232
248
- synchronized (WebSocketWriter .this ) {
249
- writeMessageFrameSynchronized (formatOpcode , buffer .size (), isFirstFrame , false /* final */ );
250
- }
233
+ writeMessageFrame (formatOpcode , buffer .size (), isFirstFrame , false /* final */ );
251
234
isFirstFrame = false ;
252
235
}
253
236
@@ -259,9 +242,7 @@ final class FrameSink implements Sink {
259
242
@ Override public void close () throws IOException {
260
243
if (closed ) throw new IOException ("closed" );
261
244
262
- synchronized (WebSocketWriter .this ) {
263
- writeMessageFrameSynchronized (formatOpcode , buffer .size (), isFirstFrame , true /* final */ );
264
- }
245
+ writeMessageFrame (formatOpcode , buffer .size (), isFirstFrame , true /* final */ );
265
246
closed = true ;
266
247
activeWriter = false ;
267
248
}
0 commit comments