37874
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package java.net.http;
|
|
26 |
|
|
27 |
import java.net.http.WSOutgoingMessage.Binary;
|
|
28 |
import java.net.http.WSOutgoingMessage.Close;
|
|
29 |
import java.net.http.WSOutgoingMessage.Ping;
|
|
30 |
import java.net.http.WSOutgoingMessage.Pong;
|
|
31 |
import java.net.http.WSOutgoingMessage.StreamedText;
|
|
32 |
import java.net.http.WSOutgoingMessage.Text;
|
|
33 |
import java.nio.ByteBuffer;
|
|
34 |
import java.nio.CharBuffer;
|
|
35 |
import java.nio.charset.CharacterCodingException;
|
|
36 |
import java.nio.charset.CoderResult;
|
|
37 |
import java.nio.charset.StandardCharsets;
|
|
38 |
import java.util.concurrent.BlockingQueue;
|
|
39 |
import java.util.concurrent.CompletableFuture;
|
|
40 |
import java.util.concurrent.Executor;
|
|
41 |
import java.util.concurrent.LinkedBlockingQueue;
|
|
42 |
import java.util.function.Consumer;
|
|
43 |
import java.util.stream.Stream;
|
|
44 |
|
|
45 |
import static java.lang.String.format;
|
|
46 |
import static java.net.http.Pair.pair;
|
|
47 |
|
|
48 |
/*
|
|
49 |
* Prepares outgoing messages for transmission. Verifies the WebSocket state,
|
|
50 |
* places the message on the outbound queue, and notifies the signal handler.
|
|
51 |
*/
|
|
52 |
final class WSTransmitter {
|
|
53 |
|
|
54 |
private final BlockingQueue<Pair<WSOutgoingMessage, CompletableFuture<Void>>>
|
|
55 |
backlog = new LinkedBlockingQueue<>();
|
|
56 |
private final WSMessageSender sender;
|
|
57 |
private final WSSignalHandler handler;
|
|
58 |
private boolean previousMessageSent = true;
|
|
59 |
private boolean canSendBinary = true;
|
|
60 |
private boolean canSendText = true;
|
|
61 |
|
|
62 |
WSTransmitter(Executor executor, RawChannel channel, Consumer<Throwable> errorHandler) {
|
|
63 |
this.handler = new WSSignalHandler(executor, this::handleSignal);
|
|
64 |
Consumer<Throwable> sendCompletion = (error) -> {
|
|
65 |
synchronized (this) {
|
|
66 |
if (error == null) {
|
|
67 |
previousMessageSent = true;
|
|
68 |
handler.signal();
|
|
69 |
} else {
|
|
70 |
errorHandler.accept(error);
|
|
71 |
backlog.forEach(p -> p.second.completeExceptionally(error));
|
|
72 |
backlog.clear();
|
|
73 |
}
|
|
74 |
}
|
|
75 |
};
|
|
76 |
this.sender = new WSMessageSender(channel, sendCompletion);
|
|
77 |
}
|
|
78 |
|
|
79 |
CompletableFuture<Void> sendText(CharSequence message, boolean isLast) {
|
|
80 |
checkAndUpdateText(isLast);
|
|
81 |
return acceptMessage(new Text(isLast, message));
|
|
82 |
}
|
|
83 |
|
|
84 |
CompletableFuture<Void> sendText(Stream<? extends CharSequence> message) {
|
|
85 |
checkAndUpdateText(true);
|
|
86 |
return acceptMessage(new StreamedText(message));
|
|
87 |
}
|
|
88 |
|
|
89 |
CompletableFuture<Void> sendBinary(ByteBuffer message, boolean isLast) {
|
|
90 |
checkAndUpdateBinary(isLast);
|
|
91 |
return acceptMessage(new Binary(isLast, message));
|
|
92 |
}
|
|
93 |
|
|
94 |
CompletableFuture<Void> sendPing(ByteBuffer message) {
|
|
95 |
checkSize(message.remaining(), 125);
|
|
96 |
return acceptMessage(new Ping(message));
|
|
97 |
}
|
|
98 |
|
|
99 |
CompletableFuture<Void> sendPong(ByteBuffer message) {
|
|
100 |
checkSize(message.remaining(), 125);
|
|
101 |
return acceptMessage(new Pong(message));
|
|
102 |
}
|
|
103 |
|
|
104 |
CompletableFuture<Void> sendClose(WebSocket.CloseCode code, CharSequence reason) {
|
|
105 |
return acceptMessage(createCloseMessage(code, reason));
|
|
106 |
}
|
|
107 |
|
|
108 |
CompletableFuture<Void> sendClose() {
|
|
109 |
return acceptMessage(new Close(ByteBuffer.allocate(0)));
|
|
110 |
}
|
|
111 |
|
|
112 |
private CompletableFuture<Void> acceptMessage(WSOutgoingMessage m) {
|
|
113 |
CompletableFuture<Void> cf = new CompletableFuture<>();
|
|
114 |
synchronized (this) {
|
|
115 |
backlog.offer(pair(m, cf));
|
|
116 |
}
|
|
117 |
handler.signal();
|
|
118 |
return cf;
|
|
119 |
}
|
|
120 |
|
|
121 |
/* Callback for pulling messages from the queue, and initiating the send. */
|
|
122 |
private void handleSignal() {
|
|
123 |
synchronized (this) {
|
|
124 |
while (!backlog.isEmpty() && previousMessageSent) {
|
|
125 |
previousMessageSent = false;
|
|
126 |
Pair<WSOutgoingMessage, CompletableFuture<Void>> p = backlog.peek();
|
|
127 |
boolean sent = sender.trySendFully(p.first);
|
|
128 |
if (sent) {
|
|
129 |
backlog.remove();
|
|
130 |
p.second.complete(null);
|
|
131 |
previousMessageSent = true;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
private Close createCloseMessage(WebSocket.CloseCode code, CharSequence reason) {
|
|
138 |
// TODO: move to construction of CloseDetail (JDK-8155621)
|
|
139 |
ByteBuffer b = ByteBuffer.allocateDirect(125).putChar((char) code.getCode());
|
|
140 |
CoderResult result = StandardCharsets.UTF_8.newEncoder()
|
|
141 |
.encode(CharBuffer.wrap(reason), b, true);
|
|
142 |
if (result.isError()) {
|
|
143 |
try {
|
|
144 |
result.throwException();
|
|
145 |
} catch (CharacterCodingException e) {
|
|
146 |
throw new IllegalArgumentException("Reason is a malformed UTF-16 sequence", e);
|
|
147 |
}
|
|
148 |
} else if (result.isOverflow()) {
|
|
149 |
throw new IllegalArgumentException("Reason is too long");
|
|
150 |
}
|
|
151 |
return new Close(b.flip());
|
|
152 |
}
|
|
153 |
|
|
154 |
private void checkSize(int size, int maxSize) {
|
|
155 |
if (size > maxSize) {
|
|
156 |
throw new IllegalArgumentException(
|
|
157 |
format("The message is too long: %s;" +
|
|
158 |
" expected not longer than %s", size, maxSize)
|
|
159 |
);
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
private void checkAndUpdateText(boolean isLast) {
|
|
164 |
if (!canSendText) {
|
|
165 |
throw new IllegalStateException("Unexpected text message");
|
|
166 |
}
|
|
167 |
canSendBinary = isLast;
|
|
168 |
}
|
|
169 |
|
|
170 |
private void checkAndUpdateBinary(boolean isLast) {
|
|
171 |
if (!canSendBinary) {
|
|
172 |
throw new IllegalStateException("Unexpected binary message");
|
|
173 |
}
|
|
174 |
canSendText = isLast;
|
|
175 |
}
|
|
176 |
}
|