author | chegar |
Wed, 02 May 2018 02:36:17 -0700 | |
changeset 49944 | 4690a2871b44 |
parent 49765 | ee6f7a61f3a5 |
child 50681 | 4254bed3c09d |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http.websocket; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
49765 | 28 |
import jdk.internal.net.http.common.Demand; |
29 |
import jdk.internal.net.http.common.Log; |
|
30 |
import jdk.internal.net.http.common.Logger; |
|
31 |
import jdk.internal.net.http.common.MinimalFuture; |
|
32 |
import jdk.internal.net.http.common.SequentialScheduler; |
|
33 |
import jdk.internal.net.http.common.Utils; |
|
34 |
import jdk.internal.net.http.websocket.OpeningHandshake.Result; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.io.IOException; |
49765 | 37 |
import java.lang.System.Logger.Level; |
48083 | 38 |
import java.lang.ref.Reference; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.net.ProtocolException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.net.URI; |
49765 | 41 |
import java.net.http.WebSocket; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.nio.ByteBuffer; |
49765 | 43 |
import java.nio.CharBuffer; |
44 |
import java.nio.charset.CharacterCodingException; |
|
45 |
import java.nio.charset.CharsetEncoder; |
|
46 |
import java.nio.charset.CodingErrorAction; |
|
47 |
import java.nio.charset.StandardCharsets; |
|
48 |
import java.util.Objects; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.concurrent.CompletionStage; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.util.concurrent.atomic.AtomicBoolean; |
49765 | 52 |
import java.util.concurrent.atomic.AtomicLong; |
48083 | 53 |
import java.util.concurrent.atomic.AtomicReference; |
49765 | 54 |
import java.util.function.BiConsumer; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
import java.util.function.Function; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
import static java.util.Objects.requireNonNull; |
49765 | 58 |
import static jdk.internal.net.http.common.MinimalFuture.failedFuture; |
59 |
import static jdk.internal.net.http.websocket.StatusCodes.CLOSED_ABNORMALLY; |
|
60 |
import static jdk.internal.net.http.websocket.StatusCodes.NO_STATUS_CODE; |
|
61 |
import static jdk.internal.net.http.websocket.StatusCodes.isLegalToSendFromClient; |
|
62 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.BINARY; |
|
63 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.CLOSE; |
|
64 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.ERROR; |
|
65 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.IDLE; |
|
66 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.OPEN; |
|
67 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.PING; |
|
68 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.PONG; |
|
69 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.TEXT; |
|
70 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.WAITING; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
* A WebSocket client. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
*/ |
48083 | 75 |
public final class WebSocketImpl implements WebSocket { |
76 |
||
49765 | 77 |
private static final Logger debug = |
78 |
Utils.getWebSocketLogger("[WebSocket]"::toString, Utils.DEBUG_WS); |
|
79 |
private final AtomicLong sendCounter = new AtomicLong(); |
|
80 |
private final AtomicLong receiveCounter = new AtomicLong(); |
|
81 |
||
48083 | 82 |
enum State { |
83 |
OPEN, |
|
84 |
IDLE, |
|
85 |
WAITING, |
|
86 |
TEXT, |
|
87 |
BINARY, |
|
88 |
PING, |
|
89 |
PONG, |
|
90 |
CLOSE, |
|
91 |
ERROR; |
|
92 |
} |
|
93 |
||
49765 | 94 |
private final AtomicReference<ByteBuffer> lastAutomaticPong = new AtomicReference<>(); |
95 |
private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this); |
|
48083 | 96 |
private volatile boolean inputClosed; |
49765 | 97 |
private final AtomicBoolean outputClosed = new AtomicBoolean(); |
48083 | 98 |
|
99 |
private final AtomicReference<State> state = new AtomicReference<>(OPEN); |
|
100 |
||
101 |
/* Components of calls to Listener's methods */ |
|
49765 | 102 |
private boolean last; |
48083 | 103 |
private ByteBuffer binaryData; |
104 |
private CharSequence text; |
|
105 |
private int statusCode; |
|
106 |
private String reason; |
|
107 |
private final AtomicReference<Throwable> error = new AtomicReference<>(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
private final URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
private final String subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
private final Listener listener; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
|
49765 | 113 |
private final AtomicBoolean pendingTextOrBinary = new AtomicBoolean(); |
114 |
private final AtomicBoolean pendingPingOrPong = new AtomicBoolean(); |
|
115 |
private final Transport transport; |
|
116 |
private final SequentialScheduler receiveScheduler |
|
117 |
= new SequentialScheduler(new ReceiveTask()); |
|
48083 | 118 |
private final Demand demand = new Demand(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
|
48083 | 120 |
public static CompletableFuture<WebSocket> newInstanceAsync(BuilderImpl b) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
Function<Result, WebSocket> newWebSocket = r -> { |
48083 | 122 |
WebSocket ws = newInstance(b.getUri(), |
123 |
r.subprotocol, |
|
124 |
b.getListener(), |
|
125 |
r.transport); |
|
126 |
// Make sure we don't release the builder until this lambda |
|
127 |
// has been executed. The builder has a strong reference to |
|
128 |
// the HttpClientFacade, and we want to keep that live until |
|
129 |
// after the raw channel is created and passed to WebSocketImpl. |
|
130 |
Reference.reachabilityFence(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
return ws; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
OpeningHandshake h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
h = new OpeningHandshake(b); |
48083 | 136 |
} catch (Throwable e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
return failedFuture(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
return h.send().thenApply(newWebSocket); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
|
48083 | 142 |
/* Exposed for testing purposes */ |
49765 | 143 |
static WebSocketImpl newInstance(URI uri, |
144 |
String subprotocol, |
|
145 |
Listener listener, |
|
146 |
TransportFactory transport) { |
|
48083 | 147 |
WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport); |
148 |
// This initialisation is outside of the constructor for the sake of |
|
149 |
// safe publication of WebSocketImpl.this |
|
150 |
ws.signalOpen(); |
|
151 |
return ws; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
|
48083 | 154 |
private WebSocketImpl(URI uri, |
155 |
String subprotocol, |
|
156 |
Listener listener, |
|
49765 | 157 |
TransportFactory transportFactory) { |
48083 | 158 |
this.uri = requireNonNull(uri); |
159 |
this.subprotocol = requireNonNull(subprotocol); |
|
160 |
this.listener = requireNonNull(listener); |
|
49765 | 161 |
// Why 6? 1 sendPing/sendPong + 1 sendText/sendBinary + 1 Close + |
162 |
// 2 automatic Ping replies + 1 automatic Close = 6 messages |
|
163 |
// Why 2 automatic Pong replies? One is being sent, but the byte buffer |
|
164 |
// has been set to null, another just has been added. |
|
165 |
this.transport = transportFactory.createTransport(new MessageQueue(6), |
|
166 |
new SignallingMessageConsumer()); |
|
167 |
} |
|
168 |
||
169 |
// FIXME: add to action handling of errors -> signalError() |
|
170 |
||
171 |
@Override |
|
172 |
public CompletableFuture<WebSocket> sendText(CharSequence message, |
|
173 |
boolean last) { |
|
174 |
Objects.requireNonNull(message); |
|
175 |
long id = 0; |
|
176 |
if (debug.on()) { |
|
177 |
id = sendCounter.incrementAndGet(); |
|
178 |
debug.log("enter send text %s payload length=%s last=%s", |
|
179 |
id, message.length(), last); |
|
180 |
} |
|
181 |
CompletableFuture<WebSocket> result; |
|
182 |
if (!setPendingTextOrBinary()) { |
|
183 |
result = failedFuture(new IllegalStateException("Send pending")); |
|
184 |
} else { |
|
185 |
result = transport.sendText(message, last, this, |
|
186 |
(r, e) -> clearPendingTextOrBinary()); |
|
187 |
} |
|
188 |
if (debug.on()) { |
|
189 |
debug.log("exit send text %s returned %s", id, result); |
|
190 |
} |
|
191 |
||
192 |
return replaceNull(result); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
@Override |
49765 | 196 |
public CompletableFuture<WebSocket> sendBinary(ByteBuffer message, |
197 |
boolean last) { |
|
198 |
Objects.requireNonNull(message); |
|
199 |
long id = 0; |
|
200 |
if (debug.on()) { |
|
201 |
id = sendCounter.incrementAndGet(); |
|
202 |
debug.log("enter send binary %s payload=%s last=%s", |
|
203 |
id, message, last); |
|
204 |
} |
|
205 |
CompletableFuture<WebSocket> result; |
|
206 |
if (!setPendingTextOrBinary()) { |
|
207 |
result = failedFuture(new IllegalStateException("Send pending")); |
|
208 |
} else { |
|
209 |
result = transport.sendBinary(message, last, this, |
|
210 |
(r, e) -> clearPendingTextOrBinary()); |
|
211 |
} |
|
212 |
if (debug.on()) { |
|
213 |
debug.log("exit send binary %s returned %s", id, result); |
|
214 |
} |
|
215 |
return replaceNull(result); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
|
49765 | 218 |
private void clearPendingTextOrBinary() { |
219 |
pendingTextOrBinary.set(false); |
|
220 |
} |
|
221 |
||
222 |
private boolean setPendingTextOrBinary() { |
|
223 |
return pendingTextOrBinary.compareAndSet(false, true); |
|
224 |
} |
|
225 |
||
226 |
private CompletableFuture<WebSocket> replaceNull( |
|
227 |
CompletableFuture<WebSocket> cf) |
|
228 |
{ |
|
229 |
if (cf == null) { |
|
230 |
return DONE; |
|
231 |
} else { |
|
232 |
return cf; |
|
233 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
public CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
49765 | 238 |
Objects.requireNonNull(message); |
239 |
long id = 0; |
|
240 |
if (debug.on()) { |
|
241 |
id = sendCounter.incrementAndGet(); |
|
242 |
debug.log("enter send ping %s payload=%s", id, message); |
|
243 |
} |
|
244 |
CompletableFuture<WebSocket> result; |
|
245 |
if (!setPendingPingOrPong()) { |
|
246 |
result = failedFuture(new IllegalStateException("Send pending")); |
|
247 |
} else { |
|
248 |
result = transport.sendPing(message, this, |
|
249 |
(r, e) -> clearPendingPingOrPong()); |
|
250 |
} |
|
251 |
if (debug.on()) { |
|
252 |
debug.log("exit send ping %s returned %s", id, result); |
|
253 |
} |
|
254 |
return replaceNull(result); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
public CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
49765 | 259 |
Objects.requireNonNull(message); |
260 |
long id = 0; |
|
261 |
if (debug.on()) { |
|
262 |
id = sendCounter.incrementAndGet(); |
|
263 |
debug.log("enter send pong %s payload=%s", id, message); |
|
264 |
} |
|
265 |
CompletableFuture<WebSocket> result; |
|
266 |
if (!setPendingPingOrPong()) { |
|
267 |
result = failedFuture(new IllegalStateException("Send pending")); |
|
268 |
} else { |
|
269 |
result = transport.sendPong(message, this, |
|
270 |
(r, e) -> clearPendingPingOrPong()); |
|
271 |
} |
|
272 |
if (debug.on()) { |
|
273 |
debug.log("exit send pong %s returned %s", id, result); |
|
274 |
} |
|
275 |
return replaceNull(result); |
|
276 |
} |
|
277 |
||
278 |
private boolean setPendingPingOrPong() { |
|
279 |
return pendingPingOrPong.compareAndSet(false, true); |
|
280 |
} |
|
281 |
||
282 |
private void clearPendingPingOrPong() { |
|
283 |
pendingPingOrPong.set(false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
@Override |
49765 | 287 |
public CompletableFuture<WebSocket> sendClose(int statusCode, |
288 |
String reason) { |
|
289 |
Objects.requireNonNull(reason); |
|
290 |
long id = 0; |
|
291 |
if (debug.on()) { |
|
292 |
id = sendCounter.incrementAndGet(); |
|
293 |
debug.log("enter send close %s statusCode=%s reason.length=%s", |
|
294 |
id, statusCode, reason.length()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
} |
49765 | 296 |
CompletableFuture<WebSocket> result; |
297 |
// Close message is the only type of message whose validity is checked |
|
298 |
// in the corresponding send method. This is made in order to close the |
|
299 |
// output in place. Otherwise the number of Close messages in queue |
|
300 |
// would not be bounded. |
|
301 |
if (!isLegalToSendFromClient(statusCode)) { |
|
302 |
result = failedFuture(new IllegalArgumentException("statusCode")); |
|
303 |
} else if (!isLegalReason(reason)) { |
|
304 |
result = failedFuture(new IllegalArgumentException("reason")); |
|
305 |
} else if (!outputClosed.compareAndSet(false, true)){ |
|
306 |
result = failedFuture(new IOException("Output closed")); |
|
307 |
} else { |
|
308 |
result = sendClose0(statusCode, reason); |
|
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
309 |
} |
49765 | 310 |
if (debug.on()) { |
311 |
debug.log("exit send close %s returned %s", id, result); |
|
312 |
} |
|
313 |
return replaceNull(result); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
|
49765 | 316 |
private static boolean isLegalReason(String reason) { |
317 |
if (reason.length() > 123) { // quick check |
|
318 |
return false; |
|
319 |
} |
|
320 |
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder() |
|
321 |
.onMalformedInput(CodingErrorAction.REPORT) |
|
322 |
.onUnmappableCharacter(CodingErrorAction.REPORT); |
|
323 |
ByteBuffer bytes; |
|
324 |
try { |
|
325 |
bytes = encoder.encode(CharBuffer.wrap(reason)); |
|
326 |
} catch (CharacterCodingException ignored) { |
|
327 |
return false; |
|
328 |
} |
|
329 |
return bytes.remaining() <= 123; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
/* |
49765 | 333 |
* The implementation uses this method internally to send Close messages |
334 |
* with codes that are not allowed to be sent through the API. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
*/ |
49765 | 336 |
private CompletableFuture<WebSocket> sendClose0(int statusCode, |
337 |
String reason) { |
|
338 |
return transport.sendClose(statusCode, reason, this, |
|
339 |
(r, e) -> processCloseError(e)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
|
49765 | 342 |
private void processCloseError(Throwable e) { |
343 |
if (e == null) { |
|
344 |
debug.log("send close completed successfully"); |
|
345 |
} else { |
|
346 |
debug.log("send close completed with error", e); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
} |
49765 | 348 |
outputClosed.set(true); |
349 |
try { |
|
350 |
transport.closeOutput(); |
|
351 |
} catch (IOException ignored) { } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
public void request(long n) { |
49765 | 356 |
if (debug.on()) { |
357 |
debug.log("request %s", n); |
|
358 |
} |
|
48083 | 359 |
if (demand.increase(n)) { |
360 |
receiveScheduler.runOrSchedule(); |
|
361 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
public String getSubprotocol() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
return subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
@Override |
48083 | 370 |
public boolean isOutputClosed() { |
49765 | 371 |
return outputClosed.get(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
372 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
@Override |
48083 | 375 |
public boolean isInputClosed() { |
376 |
return inputClosed; |
|
377 |
} |
|
378 |
||
379 |
@Override |
|
380 |
public void abort() { |
|
49765 | 381 |
if (debug.on()) { |
382 |
debug.log("abort"); |
|
383 |
} |
|
48083 | 384 |
inputClosed = true; |
49765 | 385 |
outputClosed.set(true); |
48083 | 386 |
receiveScheduler.stop(); |
387 |
close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
return super.toString() |
48083 | 393 |
+ "[uri=" + uri |
394 |
+ (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "") |
|
395 |
+ "]"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
|
48083 | 398 |
/* |
399 |
* The assumptions about order is as follows: |
|
400 |
* |
|
401 |
* - state is never changed more than twice inside the `run` method: |
|
402 |
* x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or |
|
403 |
* overwriting parts of messages creating a mess since there's no |
|
404 |
* queueing) |
|
405 |
* - OPEN is always the first state |
|
406 |
* - no messages are requested/delivered before onOpen is called (this |
|
407 |
* is implemented by making WebSocket instance accessible first in |
|
408 |
* onOpen) |
|
409 |
* - after the state has been observed as CLOSE/ERROR, the scheduler |
|
410 |
* is stopped |
|
411 |
*/ |
|
412 |
private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask { |
|
413 |
||
49765 | 414 |
// Transport only asked here and nowhere else because we must make sure |
48083 | 415 |
// onOpen is invoked first and no messages become pending before onOpen |
416 |
// finishes |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
417 |
|
48083 | 418 |
@Override |
419 |
public void run() { |
|
49765 | 420 |
if (debug.on()) { |
421 |
debug.log("enter receive task"); |
|
422 |
} |
|
423 |
loop: |
|
424 |
while (!receiveScheduler.isStopped()) { |
|
48083 | 425 |
State s = state.get(); |
49765 | 426 |
if (debug.on()) { |
427 |
debug.log("receive state: %s", s); |
|
428 |
} |
|
48083 | 429 |
try { |
430 |
switch (s) { |
|
431 |
case OPEN: |
|
432 |
processOpen(); |
|
433 |
tryChangeState(OPEN, IDLE); |
|
434 |
break; |
|
435 |
case TEXT: |
|
436 |
processText(); |
|
437 |
tryChangeState(TEXT, IDLE); |
|
438 |
break; |
|
439 |
case BINARY: |
|
440 |
processBinary(); |
|
441 |
tryChangeState(BINARY, IDLE); |
|
442 |
break; |
|
443 |
case PING: |
|
444 |
processPing(); |
|
445 |
tryChangeState(PING, IDLE); |
|
446 |
break; |
|
447 |
case PONG: |
|
448 |
processPong(); |
|
449 |
tryChangeState(PONG, IDLE); |
|
450 |
break; |
|
451 |
case CLOSE: |
|
452 |
processClose(); |
|
49765 | 453 |
break loop; |
48083 | 454 |
case ERROR: |
455 |
processError(); |
|
49765 | 456 |
break loop; |
48083 | 457 |
case IDLE: |
458 |
if (demand.tryDecrement() |
|
459 |
&& tryChangeState(IDLE, WAITING)) { |
|
49765 | 460 |
transport.request(1); |
48083 | 461 |
} |
49765 | 462 |
break loop; |
48083 | 463 |
case WAITING: |
49765 | 464 |
// For debugging spurious signalling: when there was |
465 |
// a signal, but apparently nothing has changed |
|
466 |
break loop; |
|
48083 | 467 |
default: |
468 |
throw new InternalError(String.valueOf(s)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
469 |
} |
48083 | 470 |
} catch (Throwable t) { |
471 |
signalError(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
} |
49765 | 474 |
if (debug.on()) { |
475 |
debug.log("exit receive task"); |
|
476 |
} |
|
48083 | 477 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
|
48083 | 479 |
private void processError() throws IOException { |
49765 | 480 |
if (debug.on()) { |
481 |
debug.log("processError"); |
|
482 |
} |
|
483 |
transport.closeInput(); |
|
48083 | 484 |
receiveScheduler.stop(); |
485 |
Throwable err = error.get(); |
|
486 |
if (err instanceof FailWebSocketException) { |
|
487 |
int code1 = ((FailWebSocketException) err).getStatusCode(); |
|
488 |
err = new ProtocolException().initCause(err); |
|
49765 | 489 |
if (debug.on()) { |
490 |
debug.log("failing %s with error=%s statusCode=%s", |
|
491 |
WebSocketImpl.this, err, code1); |
|
492 |
} |
|
493 |
sendCloseSilently(code1); |
|
48083 | 494 |
} |
49765 | 495 |
long id = 0; |
496 |
if (debug.on()) { |
|
497 |
id = receiveCounter.incrementAndGet(); |
|
498 |
debug.log("enter onError %s error=%s", id, err); |
|
499 |
} |
|
500 |
try { |
|
501 |
listener.onError(WebSocketImpl.this, err); |
|
502 |
} finally { |
|
503 |
if (debug.on()) { |
|
504 |
debug.log("exit onError %s", id); |
|
505 |
} |
|
506 |
} |
|
48083 | 507 |
} |
508 |
||
509 |
private void processClose() throws IOException { |
|
49765 | 510 |
debug.log("processClose"); |
511 |
transport.closeInput(); |
|
48083 | 512 |
receiveScheduler.stop(); |
49765 | 513 |
CompletionStage<?> cs = null; // when the listener is ready to close |
514 |
long id = 0; |
|
515 |
if (debug.on()) { |
|
516 |
id = receiveCounter.incrementAndGet(); |
|
517 |
debug.log("enter onClose %s statusCode=%s reason.length=%s", |
|
518 |
id, statusCode, reason.length()); |
|
519 |
} |
|
520 |
try { |
|
521 |
cs = listener.onClose(WebSocketImpl.this, statusCode, reason); |
|
522 |
} finally { |
|
523 |
debug.log("exit onClose %s returned %s", id, cs); |
|
524 |
} |
|
525 |
if (cs == null) { |
|
526 |
cs = DONE; |
|
48083 | 527 |
} |
528 |
int code; |
|
529 |
if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) { |
|
530 |
code = NORMAL_CLOSURE; |
|
49765 | 531 |
debug.log("using statusCode %s instead of %s", |
532 |
statusCode, code); |
|
533 |
||
48083 | 534 |
} else { |
535 |
code = statusCode; |
|
536 |
} |
|
49765 | 537 |
cs.whenComplete((r, e) -> { |
538 |
if (debug.on()) { |
|
539 |
debug.log("CompletionStage returned by onClose completed result=%s error=%s", |
|
540 |
r, e); |
|
541 |
} |
|
542 |
sendCloseSilently(code); |
|
48083 | 543 |
}); |
544 |
} |
|
545 |
||
546 |
private void processPong() { |
|
49765 | 547 |
long id = 0; |
548 |
if (debug.on()) { |
|
549 |
id = receiveCounter.incrementAndGet(); |
|
550 |
debug.log("enter onPong %s payload=%s", |
|
551 |
id, binaryData); |
|
552 |
} |
|
553 |
CompletionStage<?> cs = null; |
|
554 |
try { |
|
555 |
cs = listener.onPong(WebSocketImpl.this, binaryData); |
|
556 |
} finally { |
|
557 |
if (debug.on()) { |
|
558 |
debug.log("exit onPong %s returned %s", id, cs); |
|
559 |
} |
|
560 |
} |
|
48083 | 561 |
} |
562 |
||
563 |
private void processPing() { |
|
49765 | 564 |
if (debug.on()) { |
565 |
debug.log("processPing"); |
|
566 |
} |
|
567 |
// A full copy of this (small) data is made. This way sending a |
|
568 |
// replying Pong could be done in parallel with the listener |
|
569 |
// handling this Ping. |
|
48083 | 570 |
ByteBuffer slice = binaryData.slice(); |
49765 | 571 |
if (!outputClosed.get()) { |
572 |
ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining()) |
|
573 |
.put(binaryData) |
|
574 |
.flip(); |
|
575 |
if (!trySwapAutomaticPong(copy)) { |
|
576 |
// Non-exclusive send; |
|
577 |
BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> { |
|
578 |
if (e != null) { // TODO: better error handing. What if already closed? |
|
48083 | 579 |
signalError(Utils.getCompletionCause(e)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
580 |
} |
49765 | 581 |
}; |
582 |
transport.sendPong(WebSocketImpl.this::clearAutomaticPong, |
|
583 |
WebSocketImpl.this, |
|
584 |
reporter); |
|
585 |
} |
|
586 |
} |
|
587 |
long id = 0; |
|
588 |
if (debug.on()) { |
|
589 |
id = receiveCounter.incrementAndGet(); |
|
590 |
debug.log("enter onPing %s payload=%s", id, slice); |
|
591 |
} |
|
592 |
CompletionStage<?> cs = null; |
|
593 |
try { |
|
594 |
cs = listener.onPing(WebSocketImpl.this, slice); |
|
595 |
} finally { |
|
596 |
if (debug.on()) { |
|
597 |
debug.log("exit onPing %s returned %s", id, cs); |
|
598 |
} |
|
599 |
} |
|
48083 | 600 |
} |
601 |
||
602 |
private void processBinary() { |
|
49765 | 603 |
long id = 0; |
604 |
if (debug.on()) { |
|
605 |
id = receiveCounter.incrementAndGet(); |
|
606 |
debug.log("enter onBinary %s payload=%s last=%s", |
|
607 |
id, binaryData, last); |
|
608 |
} |
|
609 |
CompletionStage<?> cs = null; |
|
610 |
try { |
|
611 |
cs = listener.onBinary(WebSocketImpl.this, binaryData, last); |
|
612 |
} finally { |
|
613 |
if (debug.on()) { |
|
614 |
debug.log("exit onBinary %s returned %s", id, cs); |
|
615 |
} |
|
616 |
} |
|
48083 | 617 |
} |
618 |
||
619 |
private void processText() { |
|
49765 | 620 |
long id = 0; |
621 |
if (debug.on()) { |
|
622 |
id = receiveCounter.incrementAndGet(); |
|
623 |
debug.log("enter onText %s payload.length=%s last=%s", |
|
624 |
id, text.length(), last); |
|
625 |
} |
|
626 |
CompletionStage<?> cs = null; |
|
627 |
try { |
|
628 |
cs = listener.onText(WebSocketImpl.this, text, last); |
|
629 |
} finally { |
|
630 |
if (debug.on()) { |
|
631 |
debug.log("exit onText %s returned %s", id, cs); |
|
632 |
} |
|
633 |
} |
|
48083 | 634 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
635 |
|
48083 | 636 |
private void processOpen() { |
49765 | 637 |
long id = 0; |
638 |
if (debug.on()) { |
|
639 |
id = receiveCounter.incrementAndGet(); |
|
640 |
debug.log("enter onOpen %s", id); |
|
641 |
} |
|
642 |
try { |
|
643 |
listener.onOpen(WebSocketImpl.this); |
|
644 |
} finally { |
|
645 |
if (debug.on()) { |
|
646 |
debug.log("exit onOpen %s", id); |
|
647 |
} |
|
648 |
} |
|
48083 | 649 |
} |
650 |
} |
|
651 |
||
49765 | 652 |
private void sendCloseSilently(int statusCode) { |
653 |
sendClose0(statusCode, "").whenComplete((r, e) -> { |
|
654 |
if (e != null) { |
|
655 |
if (debug.on()) { |
|
656 |
debug.log("automatic closure completed with error", |
|
657 |
(Object) e); |
|
658 |
} |
|
659 |
} |
|
660 |
}); |
|
661 |
} |
|
662 |
||
663 |
private ByteBuffer clearAutomaticPong() { |
|
664 |
ByteBuffer data; |
|
665 |
do { |
|
666 |
data = lastAutomaticPong.get(); |
|
667 |
if (data == null) { |
|
668 |
// This method must never be called unless a message that is |
|
669 |
// using it has been added previously |
|
670 |
throw new InternalError(); |
|
671 |
} |
|
672 |
} while (!lastAutomaticPong.compareAndSet(data, null)); |
|
673 |
return data; |
|
674 |
} |
|
675 |
||
676 |
// bound pings |
|
677 |
private boolean trySwapAutomaticPong(ByteBuffer copy) { |
|
678 |
ByteBuffer message; |
|
679 |
boolean swapped; |
|
680 |
while (true) { |
|
681 |
message = lastAutomaticPong.get(); |
|
682 |
if (message == null) { |
|
683 |
if (!lastAutomaticPong.compareAndSet(null, copy)) { |
|
684 |
// It's only this method that can change null to ByteBuffer, |
|
685 |
// and this method is invoked at most by one thread at a |
|
686 |
// time. Thus no failure in the atomic operation above is |
|
687 |
// expected. |
|
688 |
throw new InternalError(); |
|
689 |
} |
|
690 |
swapped = false; |
|
691 |
break; |
|
692 |
} else if (lastAutomaticPong.compareAndSet(message, copy)) { |
|
693 |
swapped = true; |
|
694 |
break; |
|
695 |
} |
|
696 |
} |
|
697 |
if (debug.on()) { |
|
698 |
debug.log("swapped automatic pong from %s to %s", |
|
699 |
message, copy); |
|
700 |
} |
|
701 |
return swapped; |
|
702 |
} |
|
703 |
||
48083 | 704 |
private void signalOpen() { |
49765 | 705 |
debug.log("signalOpen"); |
48083 | 706 |
receiveScheduler.runOrSchedule(); |
707 |
} |
|
708 |
||
709 |
private void signalError(Throwable error) { |
|
49765 | 710 |
if (debug.on()) { |
711 |
debug.log("signalError %s", (Object) error); |
|
712 |
} |
|
48083 | 713 |
inputClosed = true; |
49765 | 714 |
outputClosed.set(true); |
48083 | 715 |
if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) { |
49765 | 716 |
if (debug.on()) { |
717 |
debug.log("signalError", error); |
|
718 |
} |
|
48083 | 719 |
Log.logError(error); |
720 |
} else { |
|
721 |
close(); |
|
722 |
} |
|
723 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
724 |
|
48083 | 725 |
private void close() { |
49765 | 726 |
if (debug.on()) { |
727 |
debug.log("close"); |
|
728 |
} |
|
729 |
Throwable first = null; |
|
48083 | 730 |
try { |
49765 | 731 |
transport.closeInput(); |
732 |
} catch (Throwable t1) { |
|
733 |
first = t1; |
|
734 |
} finally { |
|
735 |
Throwable second = null; |
|
48083 | 736 |
try { |
49765 | 737 |
transport.closeOutput(); |
738 |
} catch (Throwable t2) { |
|
739 |
second = t2; |
|
48083 | 740 |
} finally { |
49765 | 741 |
Throwable e = null; |
742 |
if (first != null && second != null) { |
|
743 |
first.addSuppressed(second); |
|
744 |
e = first; |
|
745 |
} else if (first != null) { |
|
746 |
e = first; |
|
747 |
} else if (second != null) { |
|
748 |
e = second; |
|
749 |
} |
|
750 |
if (e != null) { |
|
751 |
if (debug.on()) { |
|
752 |
debug.log("exception in close", e); |
|
753 |
} |
|
754 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
755 |
} |
48083 | 756 |
} |
757 |
} |
|
758 |
||
759 |
private void signalClose(int statusCode, String reason) { |
|
49765 | 760 |
// FIXME: make sure no race reason & close are not intermixed |
48083 | 761 |
inputClosed = true; |
762 |
this.statusCode = statusCode; |
|
763 |
this.reason = reason; |
|
49765 | 764 |
boolean managed = trySetState(CLOSE); |
765 |
if (debug.on()) { |
|
766 |
debug.log("signalClose statusCode=%s reason.length=%s: %s", |
|
767 |
statusCode, reason.length(), managed); |
|
768 |
} |
|
769 |
if (managed) { |
|
48083 | 770 |
try { |
49765 | 771 |
transport.closeInput(); |
48083 | 772 |
} catch (Throwable t) { |
49765 | 773 |
if (debug.on()) { |
774 |
debug.log("exception closing input", (Object) t); |
|
775 |
} |
|
48083 | 776 |
} |
777 |
} |
|
778 |
} |
|
779 |
||
780 |
private class SignallingMessageConsumer implements MessageStreamConsumer { |
|
781 |
||
782 |
@Override |
|
49765 | 783 |
public void onText(CharSequence data, boolean last) { |
784 |
transport.acknowledgeReception(); |
|
48083 | 785 |
text = data; |
49765 | 786 |
WebSocketImpl.this.last = last; |
48083 | 787 |
tryChangeState(WAITING, TEXT); |
788 |
} |
|
789 |
||
790 |
@Override |
|
49765 | 791 |
public void onBinary(ByteBuffer data, boolean last) { |
792 |
transport.acknowledgeReception(); |
|
48083 | 793 |
binaryData = data; |
49765 | 794 |
WebSocketImpl.this.last = last; |
48083 | 795 |
tryChangeState(WAITING, BINARY); |
796 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
797 |
|
48083 | 798 |
@Override |
799 |
public void onPing(ByteBuffer data) { |
|
49765 | 800 |
transport.acknowledgeReception(); |
48083 | 801 |
binaryData = data; |
802 |
tryChangeState(WAITING, PING); |
|
803 |
} |
|
804 |
||
805 |
@Override |
|
806 |
public void onPong(ByteBuffer data) { |
|
49765 | 807 |
transport.acknowledgeReception(); |
48083 | 808 |
binaryData = data; |
809 |
tryChangeState(WAITING, PONG); |
|
810 |
} |
|
811 |
||
812 |
@Override |
|
813 |
public void onClose(int statusCode, CharSequence reason) { |
|
49765 | 814 |
transport.acknowledgeReception(); |
48083 | 815 |
signalClose(statusCode, reason.toString()); |
816 |
} |
|
817 |
||
818 |
@Override |
|
819 |
public void onComplete() { |
|
49765 | 820 |
transport.acknowledgeReception(); |
48083 | 821 |
signalClose(CLOSED_ABNORMALLY, ""); |
822 |
} |
|
823 |
||
824 |
@Override |
|
825 |
public void onError(Throwable error) { |
|
826 |
signalError(error); |
|
827 |
} |
|
828 |
} |
|
829 |
||
830 |
private boolean trySetState(State newState) { |
|
49765 | 831 |
State currentState; |
832 |
boolean success = false; |
|
48083 | 833 |
while (true) { |
49765 | 834 |
currentState = state.get(); |
48083 | 835 |
if (currentState == ERROR || currentState == CLOSE) { |
49765 | 836 |
break; |
48083 | 837 |
} else if (state.compareAndSet(currentState, newState)) { |
838 |
receiveScheduler.runOrSchedule(); |
|
49765 | 839 |
success = true; |
840 |
break; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
841 |
} |
48083 | 842 |
} |
49765 | 843 |
if (debug.on()) { |
844 |
debug.log("set state %s (previous %s) %s", |
|
845 |
newState, currentState, success); |
|
846 |
} |
|
847 |
return success; |
|
48083 | 848 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
849 |
|
48083 | 850 |
private boolean tryChangeState(State expectedState, State newState) { |
851 |
State witness = state.compareAndExchange(expectedState, newState); |
|
49765 | 852 |
boolean success = false; |
48083 | 853 |
if (witness == expectedState) { |
854 |
receiveScheduler.runOrSchedule(); |
|
49765 | 855 |
success = true; |
856 |
} else if (witness != ERROR && witness != CLOSE) { |
|
857 |
// This should be the only reason for inability to change the state |
|
858 |
// from IDLE to WAITING: the state has changed to terminal |
|
48083 | 859 |
throw new InternalError(); |
860 |
} |
|
49765 | 861 |
if (debug.on()) { |
862 |
debug.log("change state from %s to %s %s", |
|
863 |
expectedState, newState, success); |
|
864 |
} |
|
865 |
return success; |
|
866 |
} |
|
867 |
||
868 |
/* Exposed for testing purposes */ |
|
869 |
protected Transport transport() { |
|
870 |
return transport; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
871 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
872 |
} |