author | prappo |
Thu, 30 Nov 2017 13:35:26 +0300 | |
branch | http-client-branch |
changeset 55922 | 77feac3903d9 |
parent 55907 | f6a3a657416e |
child 55973 | 4d9b002587db |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http.internal.websocket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
28 |
import jdk.incubator.http.WebSocket; |
55922 | 29 |
import jdk.incubator.http.internal.common.Demand; |
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
30 |
import jdk.incubator.http.internal.common.Log; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
31 |
import jdk.incubator.http.internal.common.MinimalFuture; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
32 |
import jdk.incubator.http.internal.common.Pair; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
33 |
import jdk.incubator.http.internal.common.SequentialScheduler; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
34 |
import jdk.incubator.http.internal.common.SequentialScheduler.DeferredCompleter; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
35 |
import jdk.incubator.http.internal.common.Utils; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
36 |
import jdk.incubator.http.internal.websocket.OpeningHandshake.Result; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
37 |
import jdk.incubator.http.internal.websocket.OutgoingMessage.Binary; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
38 |
import jdk.incubator.http.internal.websocket.OutgoingMessage.Close; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
39 |
import jdk.incubator.http.internal.websocket.OutgoingMessage.Context; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
40 |
import jdk.incubator.http.internal.websocket.OutgoingMessage.Ping; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
41 |
import jdk.incubator.http.internal.websocket.OutgoingMessage.Pong; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
42 |
import jdk.incubator.http.internal.websocket.OutgoingMessage.Text; |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
43 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.io.IOException; |
55789
b3562d42f3b1
http-client-impl: use Reference.reachabilityFence to make clear the websokect BuilderImpl must stay strongly referenced until after the WebSocketImpl is created.
dfuchs
parents:
55768
diff
changeset
|
45 |
import java.lang.ref.Reference; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.net.ProtocolException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import java.util.Queue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.util.concurrent.CompletionStage; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import java.util.concurrent.ConcurrentLinkedQueue; |
55842 | 53 |
import java.util.concurrent.TimeUnit; |
54 |
import java.util.concurrent.TimeoutException; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
import java.util.concurrent.atomic.AtomicBoolean; |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
56 |
import java.util.concurrent.atomic.AtomicReference; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
import java.util.function.Consumer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
import java.util.function.Function; |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
55763
diff
changeset
|
59 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
import static java.util.Objects.requireNonNull; |
55850
1e2fa7c503c1
http-client-branch: (WebSocket) CompletableFuture -> MinimalFuture
prappo
parents:
55842
diff
changeset
|
61 |
import static jdk.incubator.http.internal.common.MinimalFuture.failedFuture; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
import static jdk.incubator.http.internal.common.Pair.pair; |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
63 |
import static jdk.incubator.http.internal.websocket.StatusCodes.CLOSED_ABNORMALLY; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
import static jdk.incubator.http.internal.websocket.StatusCodes.NO_STATUS_CODE; |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
65 |
import static jdk.incubator.http.internal.websocket.StatusCodes.isLegalToSendFromClient; |
55922 | 66 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.BINARY; |
67 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.CLOSE; |
|
68 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.ERROR; |
|
69 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.IDLE; |
|
70 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.OPEN; |
|
71 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.PING; |
|
72 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.PONG; |
|
73 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.TEXT; |
|
74 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.WAITING; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
* A WebSocket client. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
*/ |
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
79 |
public final class WebSocketImpl implements WebSocket { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
|
55922 | 81 |
enum State { |
82 |
OPEN, |
|
83 |
IDLE, |
|
84 |
WAITING, |
|
85 |
TEXT, |
|
86 |
BINARY, |
|
87 |
PING, |
|
88 |
PONG, |
|
89 |
CLOSE, |
|
90 |
ERROR; |
|
91 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
|
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
93 |
private volatile boolean inputClosed; |
55842 | 94 |
private volatile boolean outputClosed; |
95 |
||
55922 | 96 |
private final AtomicReference<State> state = new AtomicReference<>(OPEN); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
97 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
98 |
/* Components of calls to Listener's methods */ |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
99 |
private MessagePart part; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
100 |
private ByteBuffer binaryData; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
101 |
private CharSequence text; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
102 |
private int statusCode; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
103 |
private String reason; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
104 |
private final AtomicReference<Throwable> error = new AtomicReference<>(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
105 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
106 |
private final URI uri; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
107 |
private final String subprotocol; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
108 |
private final Listener listener; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
109 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
private final AtomicBoolean outstandingSend = new AtomicBoolean(); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
111 |
private final SequentialScheduler sendScheduler = new SequentialScheduler(new SendTask()); |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
112 |
private final Queue<Pair<OutgoingMessage, CompletableFuture<WebSocket>>> |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
113 |
queue = new ConcurrentLinkedQueue<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
private final Context context = new OutgoingMessage.Context(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
private final Transmitter transmitter; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
private final Receiver receiver; |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
117 |
private final SequentialScheduler receiveScheduler = new SequentialScheduler(new ReceiveTask()); |
55922 | 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 |
|
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
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 -> { |
55907 | 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 |
|
55789
b3562d42f3b1
http-client-impl: use Reference.reachabilityFence to make clear the websokect BuilderImpl must stay strongly referenced until after the WebSocketImpl is created.
dfuchs
parents:
55768
diff
changeset
|
127 |
// has been executed. The builder has a strong reference to |
b3562d42f3b1
http-client-impl: use Reference.reachabilityFence to make clear the websokect BuilderImpl must stay strongly referenced until after the WebSocketImpl is created.
dfuchs
parents:
55768
diff
changeset
|
128 |
// the HttpClientFacade, and we want to keep that live until |
b3562d42f3b1
http-client-impl: use Reference.reachabilityFence to make clear the websokect BuilderImpl must stay strongly referenced until after the WebSocketImpl is created.
dfuchs
parents:
55768
diff
changeset
|
129 |
// after the raw channel is created and passed to WebSocketImpl. |
b3562d42f3b1
http-client-impl: use Reference.reachabilityFence to make clear the websokect BuilderImpl must stay strongly referenced until after the WebSocketImpl is created.
dfuchs
parents:
55768
diff
changeset
|
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 { |
55853
937985fc3c45
http-client-branch: (WebSocket) moving http proxy-related code to OpeningHandshake
prappo
parents:
55850
diff
changeset
|
135 |
h = new OpeningHandshake(b); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
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 |
|
55907 | 142 |
/* Exposed for testing purposes */ |
143 |
static WebSocket newInstance(URI uri, |
|
144 |
String subprotocol, |
|
145 |
Listener listener, |
|
146 |
TransportSupplier transport) { |
|
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; |
|
152 |
} |
|
153 |
||
154 |
private WebSocketImpl(URI uri, |
|
155 |
String subprotocol, |
|
156 |
Listener listener, |
|
55922 | 157 |
TransportSupplier transport) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
this.uri = requireNonNull(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
this.subprotocol = requireNonNull(subprotocol); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
this.listener = requireNonNull(listener); |
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
161 |
this.transmitter = transport.transmitter(); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
162 |
this.receiver = transport.receiver(new SignallingMessageConsumer()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
163 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
@Override |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
166 |
public CompletableFuture<WebSocket> sendText(CharSequence message, boolean isLast) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
return enqueueExclusively(new Text(message, isLast)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
@Override |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
171 |
public CompletableFuture<WebSocket> sendBinary(ByteBuffer message, boolean isLast) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
return enqueueExclusively(new Binary(message, isLast)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
public CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
55842 | 177 |
return enqueue(new Ping(message)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
public CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
55842 | 182 |
return enqueue(new Pong(message)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
@Override |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
186 |
public CompletableFuture<WebSocket> sendClose(int statusCode, String reason) { |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
187 |
if (!isLegalToSendFromClient(statusCode)) { |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
188 |
return failedFuture( |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
189 |
new IllegalArgumentException("statusCode: " + statusCode)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
} |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
191 |
Close msg; |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
192 |
try { |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
193 |
msg = new Close(statusCode, reason); |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
194 |
} catch (IllegalArgumentException e) { |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
195 |
return failedFuture(e); |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
196 |
} |
55853
937985fc3c45
http-client-branch: (WebSocket) moving http proxy-related code to OpeningHandshake
prappo
parents:
55850
diff
changeset
|
197 |
outputClosed = true; |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
198 |
return enqueueClose(msg); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
/* |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
202 |
* Sends a Close message, then shuts down the transmitter since no more |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
203 |
* messages are expected to be sent after this. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
private CompletableFuture<WebSocket> enqueueClose(Close m) { |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
206 |
// TODO: MUST be a CF created once and shared across sendClose, otherwise |
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
207 |
// a second sendClose may prematurely close the channel |
55842 | 208 |
return enqueue(m) |
209 |
.orTimeout(60, TimeUnit.SECONDS) |
|
210 |
.whenComplete((r, error) -> { |
|
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
211 |
try { |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
212 |
transmitter.close(); |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
213 |
} catch (IOException e) { |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
214 |
Log.logError(e); |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
215 |
} |
55842 | 216 |
if (error instanceof TimeoutException) { |
217 |
try { |
|
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
218 |
receiver.close(); |
55842 | 219 |
} catch (IOException e) { |
220 |
Log.logError(e); |
|
221 |
} |
|
222 |
} |
|
223 |
}); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
* Accepts the given message into the outgoing queue in a mutually-exclusive |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
* fashion in respect to other messages accepted through this method. No |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
* further messages will be accepted until the returned CompletableFuture |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
* completes. This method is used to enforce "one outstanding send |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
* operation" policy. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
*/ |
55922 | 233 |
private CompletableFuture<WebSocket> enqueueExclusively(OutgoingMessage m) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
if (!outstandingSend.compareAndSet(false, true)) { |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
235 |
return failedFuture(new IllegalStateException("Send pending")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
return enqueue(m).whenComplete((r, e) -> outstandingSend.set(false)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
private CompletableFuture<WebSocket> enqueue(OutgoingMessage m) { |
55768
8674257c75ce
http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents:
55764
diff
changeset
|
241 |
CompletableFuture<WebSocket> cf = new MinimalFuture<>(); |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
242 |
boolean added = queue.add(pair(m, cf)); |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
243 |
if (!added) { |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
244 |
// The queue is supposed to be unbounded |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
245 |
throw new InternalError(); |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
246 |
} |
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
247 |
sendScheduler.runOrSchedule(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
return cf; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
|
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
251 |
/* |
55853
937985fc3c45
http-client-branch: (WebSocket) moving http proxy-related code to OpeningHandshake
prappo
parents:
55850
diff
changeset
|
252 |
* This is a message sending task. It pulls messages from the queue one by |
937985fc3c45
http-client-branch: (WebSocket) moving http proxy-related code to OpeningHandshake
prappo
parents:
55850
diff
changeset
|
253 |
* one and sends them. It may be run in different threads, but never |
937985fc3c45
http-client-branch: (WebSocket) moving http proxy-related code to OpeningHandshake
prappo
parents:
55850
diff
changeset
|
254 |
* concurrently. |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
255 |
*/ |
55853
937985fc3c45
http-client-branch: (WebSocket) moving http proxy-related code to OpeningHandshake
prappo
parents:
55850
diff
changeset
|
256 |
private class SendTask implements SequentialScheduler.RestartableTask { |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
257 |
|
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
258 |
@Override |
55842 | 259 |
public void run(DeferredCompleter taskCompleter) { |
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
260 |
Pair<OutgoingMessage, CompletableFuture<WebSocket>> p = queue.poll(); |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
261 |
if (p == null) { |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
262 |
taskCompleter.complete(); |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
263 |
return; |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
264 |
} |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
265 |
OutgoingMessage message = p.first; |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
266 |
CompletableFuture<WebSocket> cf = p.second; |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
267 |
try { |
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
268 |
if (!message.contextualize(context)) { // Do not send the message |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
269 |
cf.complete(null); |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
270 |
repeat(taskCompleter); |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
271 |
return; |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
272 |
} |
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
273 |
Consumer<Exception> h = e -> { |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
274 |
if (e == null) { |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
275 |
cf.complete(WebSocketImpl.this); |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
276 |
} else { |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
277 |
cf.completeExceptionally(e); |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
278 |
} |
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
279 |
repeat(taskCompleter); |
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
280 |
}; |
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
281 |
transmitter.send(message, h); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
282 |
} catch (Throwable t) { |
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
283 |
cf.completeExceptionally(t); |
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
284 |
repeat(taskCompleter); |
55763
634d8e14c172
http-client-branch: intial load from jdk10/sandbox
chegar
parents:
47216
diff
changeset
|
285 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
} |
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
287 |
|
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
288 |
private void repeat(DeferredCompleter taskCompleter) { |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
289 |
taskCompleter.complete(); |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
290 |
// More than a single message may have been enqueued while |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
291 |
// the task has been busy with the current message, but |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
292 |
// there is only a single signal recorded |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
293 |
sendScheduler.runOrSchedule(); |
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
294 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
297 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
298 |
public void request(long n) { |
55922 | 299 |
if (demand.increase(n)) { |
300 |
receiveScheduler.runOrSchedule(); |
|
301 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
public String getSubprotocol() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
return subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
@Override |
55842 | 310 |
public boolean isOutputClosed() { |
311 |
return outputClosed; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
@Override |
55842 | 315 |
public boolean isInputClosed() { |
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
316 |
return inputClosed; |
55842 | 317 |
} |
318 |
||
319 |
@Override |
|
320 |
public void abort() { |
|
55862
faa39b5ec8e1
http-client-branch: (WebSocket) testing sending messages
prappo
parents:
55853
diff
changeset
|
321 |
inputClosed = true; |
55842 | 322 |
outputClosed = true; |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
323 |
receiveScheduler.stop(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
324 |
close(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
return super.toString() |
55867
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
330 |
+ "[uri=" + uri |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
331 |
+ (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "") |
1b8734a5c696
http-client-branch: (WebSocket) cleanup & simplifications
prappo
parents:
55863
diff
changeset
|
332 |
+ "]"; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
|
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
335 |
/* |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
336 |
* The assumptions about order is as follows: |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
337 |
* |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
338 |
* - state is never changed more than twice inside the `run` method: |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
339 |
* x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
340 |
* overwriting parts of messages creating a mess since there's no |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
341 |
* queueing) |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
342 |
* - OPEN is always the first state |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
343 |
* - no messages are requested/delivered before onOpen is called (this |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
344 |
* is implemented by making WebSocket instance accessible first in |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
345 |
* onOpen) |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
346 |
* - after the state has been observed as CLOSE/ERROR, the scheduler |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
347 |
* is stopped |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
348 |
*/ |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
349 |
private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
|
55922 | 351 |
// Receiver only asked here and nowhere else because we must make sure |
352 |
// onOpen is invoked first and no messages become pending before onOpen |
|
353 |
// finishes |
|
354 |
||
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
355 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
356 |
public void run() { |
55922 | 357 |
while (true) { |
358 |
State s = state.get(); |
|
359 |
try { |
|
360 |
switch (s) { |
|
361 |
case OPEN: |
|
362 |
processOpen(); |
|
363 |
tryChangeState(OPEN, IDLE); |
|
364 |
break; |
|
365 |
case TEXT: |
|
366 |
processText(); |
|
367 |
tryChangeState(TEXT, IDLE); |
|
368 |
break; |
|
369 |
case BINARY: |
|
370 |
processBinary(); |
|
371 |
tryChangeState(BINARY, IDLE); |
|
372 |
break; |
|
373 |
case PING: |
|
374 |
processPing(); |
|
375 |
tryChangeState(PING, IDLE); |
|
376 |
break; |
|
377 |
case PONG: |
|
378 |
processPong(); |
|
379 |
tryChangeState(PONG, IDLE); |
|
380 |
break; |
|
381 |
case CLOSE: |
|
382 |
processClose(); |
|
383 |
return; |
|
384 |
case ERROR: |
|
385 |
processError(); |
|
386 |
return; |
|
387 |
case IDLE: |
|
388 |
if (demand.tryDecrement() |
|
389 |
&& tryChangeState(IDLE, WAITING)) { |
|
390 |
receiver.request(1); |
|
391 |
} |
|
392 |
return; |
|
393 |
case WAITING: |
|
394 |
// For debugging spurious signalling: when there was a |
|
395 |
// signal, but apparently nothing has changed |
|
396 |
return; |
|
397 |
default: |
|
398 |
throw new InternalError(String.valueOf(s)); |
|
399 |
} |
|
400 |
} catch (Throwable t) { |
|
401 |
signalError(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
404 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
405 |
|
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
406 |
private void processError() throws IOException { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
407 |
receiver.close(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
408 |
receiveScheduler.stop(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
409 |
Throwable err = error.get(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
410 |
if (err instanceof FailWebSocketException) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
411 |
int code1 = ((FailWebSocketException) err).getStatusCode(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
412 |
err = new ProtocolException().initCause(err); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
413 |
enqueueClose(new Close(code1, "")) |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
414 |
.whenComplete( |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
415 |
(r, e) -> { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
416 |
if (e != null) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
417 |
Log.logError(e); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
418 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
419 |
}); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
420 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
421 |
listener.onError(WebSocketImpl.this, err); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
422 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
423 |
|
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
424 |
private void processClose() throws IOException { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
425 |
receiver.close(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
426 |
receiveScheduler.stop(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
427 |
CompletionStage<?> readyToClose; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
428 |
readyToClose = listener.onClose(WebSocketImpl.this, statusCode, reason); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
429 |
if (readyToClose == null) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
430 |
readyToClose = MinimalFuture.completedFuture(null); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
431 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
432 |
int code; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
433 |
if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
434 |
code = NORMAL_CLOSURE; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
435 |
} else { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
436 |
code = statusCode; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
437 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
438 |
readyToClose.whenComplete((r, e) -> { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
439 |
enqueueClose(new Close(code, "")) |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
440 |
.whenComplete((r1, e1) -> { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
441 |
if (e1 != null) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
442 |
Log.logError(e1); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
443 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
444 |
}); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
445 |
}); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
446 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
447 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
448 |
private void processPong() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
449 |
listener.onPong(WebSocketImpl.this, binaryData); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
450 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
451 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
452 |
private void processPing() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
453 |
// Let's make a full copy of this tiny data. What we want here |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
454 |
// is to rule out a possibility the shared data we send might be |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
455 |
// corrupted by processing in the listener. |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
456 |
ByteBuffer slice = binaryData.slice(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
457 |
ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining()) |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
458 |
.put(binaryData) |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
459 |
.flip(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
460 |
// Non-exclusive send; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
461 |
CompletableFuture<WebSocket> pongSent = enqueue(new Pong(copy)); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
462 |
pongSent.whenComplete( |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
463 |
(r, e) -> { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
464 |
if (e != null) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
465 |
signalError(Utils.getCompletionCause(e)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
466 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
467 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
468 |
); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
469 |
listener.onPing(WebSocketImpl.this, slice); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
470 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
471 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
472 |
private void processBinary() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
473 |
listener.onBinary(WebSocketImpl.this, binaryData, part); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
474 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
475 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
476 |
private void processText() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
477 |
listener.onText(WebSocketImpl.this, text, part); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
478 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
479 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
480 |
private void processOpen() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
481 |
listener.onOpen(WebSocketImpl.this); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
482 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
483 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
484 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
485 |
private void signalOpen() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
486 |
receiveScheduler.runOrSchedule(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
487 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
488 |
|
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
489 |
private void signalError(Throwable error) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
490 |
inputClosed = true; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
491 |
outputClosed = true; |
55922 | 492 |
if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) { |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
493 |
Log.logError(error); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
494 |
} else { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
495 |
close(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
496 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
497 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
498 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
499 |
private void close() { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
500 |
try { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
501 |
try { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
502 |
receiver.close(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
503 |
} finally { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
504 |
transmitter.close(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
505 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
506 |
} catch (Throwable t) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
507 |
Log.logError(t); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
508 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
509 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
|
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
511 |
/* |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
512 |
* Signals a Close event (might not correspond to anything happened on the |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
513 |
* channel, i.e. might be synthetic). |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
514 |
*/ |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
515 |
private void signalClose(int statusCode, String reason) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
516 |
inputClosed = true; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
517 |
this.statusCode = statusCode; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
518 |
this.reason = reason; |
55922 | 519 |
if (!trySetState(CLOSE)) { |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
520 |
Log.logTrace("Close: {0}, ''{1}''", statusCode, reason); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
521 |
} else { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
522 |
try { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
523 |
receiver.close(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
524 |
} catch (Throwable t) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
525 |
Log.logError(t); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
526 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
527 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
528 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
529 |
|
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
530 |
private class SignallingMessageConsumer implements MessageStreamConsumer { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
531 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
532 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
533 |
public void onText(CharSequence data, MessagePart part) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
534 |
receiver.acknowledge(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
535 |
text = data; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
536 |
WebSocketImpl.this.part = part; |
55922 | 537 |
tryChangeState(WAITING, TEXT); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
538 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
539 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
540 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
541 |
public void onBinary(ByteBuffer data, MessagePart part) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
542 |
receiver.acknowledge(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
543 |
binaryData = data; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
544 |
WebSocketImpl.this.part = part; |
55922 | 545 |
tryChangeState(WAITING, BINARY); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
546 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
547 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
548 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
549 |
public void onPing(ByteBuffer data) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
550 |
receiver.acknowledge(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
551 |
binaryData = data; |
55922 | 552 |
tryChangeState(WAITING, PING); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
553 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
554 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
555 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
556 |
public void onPong(ByteBuffer data) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
557 |
receiver.acknowledge(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
558 |
binaryData = data; |
55922 | 559 |
tryChangeState(WAITING, PONG); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
560 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
561 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
562 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
563 |
public void onClose(int statusCode, CharSequence reason) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
564 |
receiver.acknowledge(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
565 |
signalClose(statusCode, reason.toString()); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
566 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
567 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
568 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
569 |
public void onComplete() { |
55922 | 570 |
receiver.acknowledge(); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
571 |
signalClose(CLOSED_ABNORMALLY, ""); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
572 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
573 |
|
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
574 |
@Override |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
575 |
public void onError(Throwable error) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
576 |
signalError(error); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
577 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
578 |
} |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
579 |
|
55922 | 580 |
private boolean trySetState(State newState) { |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
581 |
while (true) { |
55922 | 582 |
State currentState = state.get(); |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
583 |
if (currentState == ERROR || currentState == CLOSE) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
584 |
return false; |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
585 |
} else if (state.compareAndSet(currentState, newState)) { |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
586 |
receiveScheduler.runOrSchedule(); |
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
587 |
return true; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
588 |
} |
55902
071affa55c69
http-client-branch: (WebSocket) removed locking from the impl
prappo
parents:
55867
diff
changeset
|
589 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
} |
55922 | 591 |
|
592 |
private boolean tryChangeState(State expectedState, State newState) { |
|
593 |
State witness = state.compareAndExchange(expectedState, newState); |
|
594 |
if (witness == expectedState) { |
|
595 |
receiveScheduler.runOrSchedule(); |
|
596 |
return true; |
|
597 |
} |
|
598 |
// This should be the only reason for inability to change the state from |
|
599 |
// IDLE to WAITING: the state has changed to terminal |
|
600 |
if (witness != ERROR && witness != CLOSE) { |
|
601 |
throw new InternalError(); |
|
602 |
} |
|
603 |
return false; |
|
604 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
605 |
} |