/
WebSocket.java
387 lines (340 loc) · 13.8 KB
/
WebSocket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package com.firebase.tubesock;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import javax.net.SocketFactory;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This is the main class used to create a websocket connection. Create a new instance, set an event handler, and then
* call connect(). Once the event handler's onOpen method has been called, call send() on the websocket to transmit
* data.
*/
public class WebSocket {
private static final String THREAD_BASE_NAME = "TubeSock";
private static final AtomicInteger clientCount = new AtomicInteger(0);
private enum State {NONE, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED};
private static final Charset UTF8 = Charset.forName("UTF-8");
static final byte OPCODE_NONE = 0x0;
static final byte OPCODE_TEXT = 0x1;
static final byte OPCODE_BINARY = 0x2;
static final byte OPCODE_CLOSE = 0x8;
static final byte OPCODE_PING = 0x9;
static final byte OPCODE_PONG = 0xA;
private volatile State state = State.NONE;
private volatile Socket socket = null;
private WebSocketEventHandler eventHandler = null;
private final URI url;
private final WebSocketReceiver receiver;
private final WebSocketWriter writer;
private final WebSocketHandshake handshake;
private final int clientId = clientCount.incrementAndGet();
private final Thread innerThread;
private static ThreadFactory threadFactory = Executors.defaultThreadFactory();
private static ThreadInitializer intializer = new ThreadInitializer() {
@Override
public void setName(Thread t, String name) {
t.setName(name);
}
};
static ThreadFactory getThreadFactory() {
return threadFactory;
}
static ThreadInitializer getIntializer() {
return intializer;
}
public static void setThreadFactory(ThreadFactory threadFactory, ThreadInitializer intializer) {
WebSocket.threadFactory = threadFactory;
WebSocket.intializer = intializer;
}
/**
* Create a websocket to connect to a given server
* @param url The URL of a websocket server
*/
public WebSocket(URI url) {
this(url, null);
}
/**
* Create a websocket to connect to a given server. Include protocol in websocket handshake
* @param url The URL of a websocket server
* @param protocol The protocol to include in the handshake. If null, it will be omitted
*/
public WebSocket(URI url, String protocol) {
this(url, protocol, null);
}
/**
* Create a websocket to connect to a given server. Include the given protocol in the handshake, as well as any
* extra HTTP headers specified. Useful if you would like to include a User-Agent or other header
* @param url The URL of a websocket server
* @param protocol The protocol to include in the handshake. If null, it will be omitted
* @param extraHeaders Any extra HTTP headers to be included with the initial request. Pass null if not extra headers
* are requested
*/
public WebSocket(URI url, String protocol, Map<String, String> extraHeaders) {
innerThread = getThreadFactory().newThread(new Runnable() {
@Override
public void run() {
runReader();
}
});
this.url = url;
handshake = new WebSocketHandshake(url, protocol, extraHeaders);
receiver = new WebSocketReceiver(this);
writer = new WebSocketWriter(this, THREAD_BASE_NAME, clientId);
}
/**
* Must be called before connect(). Set the handler for all websocket-related events.
* @param eventHandler The handler to be triggered with relevant events
*/
public void setEventHandler(WebSocketEventHandler eventHandler) {
this.eventHandler = eventHandler;
}
WebSocketEventHandler getEventHandler() {
return this.eventHandler;
}
/**
* Start up the socket. This is non-blocking, it will fire up the threads used by the library and then trigger the
* onOpen handler once the connection is established.
*/
public synchronized void connect() {
if (state != State.NONE) {
eventHandler.onError(new WebSocketException("connect() already called"));
close();
return;
}
getIntializer().setName(getInnerThread(), THREAD_BASE_NAME + "Reader-" + clientId);
state = State.CONNECTING;
getInnerThread().start();
}
/**
* Send a TEXT message over the socket
* @param data The text payload to be sent
*/
public synchronized void send(String data) {
send(OPCODE_TEXT, data.getBytes(UTF8));
}
/**
* Send a BINARY message over the socket
* @param data The binary payload to be sent
*/
public synchronized void send(byte[] data) {
send(OPCODE_BINARY, data);
}
synchronized void pong(byte[] data) {
send(OPCODE_PONG, data);
}
private synchronized void send(byte opcode, byte[] data) {
if (state != State.CONNECTED) {
// We might have been disconnected on another thread, just report an error
eventHandler.onError(new WebSocketException("error while sending data: not connected"));
} else {
try {
writer.send(opcode, true, data);
} catch (IOException e) {
eventHandler.onError(new WebSocketException("Failed to send frame", e));
close();
}
}
}
void handleReceiverError(WebSocketException e) {
eventHandler.onError(e);
if (state == State.CONNECTED) {
close();
}
closeSocket();
}
/**
* Close down the socket. Will trigger the onClose handler if the socket has not been previously closed.
*/
public synchronized void close()
{
switch (state) {
case NONE:
state = State.DISCONNECTED;
return;
case CONNECTING:
// don't wait for an established connection, just close the tcp socket
closeSocket();
return;
case CONNECTED:
// This method also shuts down the writer
// the socket will be closed once the ack for the close was received
sendCloseHandshake();
return;
case DISCONNECTING:
return; // no-op;
case DISCONNECTED:
return; // No-op
}
}
void onCloseOpReceived() {
closeSocket();
}
private synchronized void closeSocket() {
if (state == State.DISCONNECTED) {
return;
}
receiver.stopit();
writer.stopIt();
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
state = State.DISCONNECTED;
eventHandler.onClose();
}
private void sendCloseHandshake() {
try {
state = State.DISCONNECTING;
// Set the stop flag then queue up a message. This ensures that the writer thread
// will wake up, and since we set the stop flag, it will exit its run loop.
writer.stopIt();
writer.send(OPCODE_CLOSE, true, new byte[0]);
} catch (IOException e) {
eventHandler.onError(new WebSocketException("Failed to send close frame", e));
}
}
private Socket createSocket() {
String scheme = url.getScheme();
String host = url.getHost();
int port = url.getPort();
Socket socket;
if (scheme != null && scheme.equals("ws")) {
if (port == -1) {
port = 80;
}
try {
socket = new Socket(host, port);
} catch (UnknownHostException uhe) {
throw new WebSocketException("unknown host: " + host, uhe);
} catch (IOException ioe) {
throw new WebSocketException("error while creating socket to " + url, ioe);
}
} else if (scheme != null && scheme.equals("wss")) {
if (port == -1) {
port = 443;
}
try {
SocketFactory factory = SSLSocketFactory.getDefault();
socket = factory.createSocket(host, port);
// Make sure the cert we got is for the hostname we're expecting
verifyHost((SSLSocket)socket, host);
} catch (UnknownHostException uhe) {
throw new WebSocketException("unknown host: " + host, uhe);
} catch (IOException ioe) {
throw new WebSocketException("error while creating secure socket to " + url, ioe);
}
} else {
throw new WebSocketException("unsupported protocol: " + scheme);
}
return socket;
}
private void verifyHost(SSLSocket socket, String host) throws SSLException {
Certificate[] certs = socket.getSession().getPeerCertificates();
X509Certificate peerCert = (X509Certificate)certs[0];
StrictHostnameVerifier verifier = new StrictHostnameVerifier();
verifier.verify(host, peerCert);
}
/**
* Blocks until both threads exit. The actual close must be triggered separately. This is just a convenience
* method to make sure everything shuts down, if desired.
* @throws InterruptedException
*/
public void blockClose() throws InterruptedException {
// If the thread is new, it will never run, since we closed the connection before we actually connected
if (writer.getInnerThread().getState() != Thread.State.NEW) {
writer.getInnerThread().join();
}
getInnerThread().join();
}
private void runReader() {
try {
Socket socket = createSocket();
synchronized (this) {
WebSocket.this.socket = socket;
if (WebSocket.this.state == WebSocket.State.DISCONNECTED) {
// The connection has been closed while creating the socket, close it immediately and return
try {
WebSocket.this.socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
WebSocket.this.socket = null;
return;
}
}
DataInputStream input = new DataInputStream(socket.getInputStream());
OutputStream output = socket.getOutputStream();
output.write(handshake.getHandshake());
boolean handshakeComplete = false;
int len = 1000;
byte[] buffer = new byte[len];
int pos = 0;
ArrayList<String> handshakeLines = new ArrayList<String>();
while (!handshakeComplete) {
int b = input.read();
if (b == -1) {
throw new WebSocketException("Connection closed before handshake was complete");
}
buffer[pos] = (byte) b;
pos += 1;
if (buffer[pos - 1] == 0x0A && buffer[pos - 2] == 0x0D) {
String line = new String(buffer, UTF8);
if (line.trim().equals("")) {
handshakeComplete = true;
} else {
handshakeLines.add(line.trim());
}
buffer = new byte[len];
pos = 0;
} else if (pos == 1000) {
// This really shouldn't happen, handshake lines are short, but just to be safe...
String line = new String(buffer, UTF8);
throw new WebSocketException("Unexpected long line in handshake: " + line);
}
}
handshake.verifyServerStatusLine(handshakeLines.get(0));
handshakeLines.remove(0);
HashMap<String, String> headers = new HashMap<String, String>();
for (String line : handshakeLines) {
String[] keyValue = line.split(": ", 2);
headers.put(keyValue[0].toLowerCase(Locale.US), keyValue[1]);
}
handshake.verifyServerHandshakeHeaders(headers);
writer.setOutput(output);
receiver.setInput(input);
state = WebSocket.State.CONNECTED;
writer.getInnerThread().start();
eventHandler.onOpen();
receiver.run();
} catch (WebSocketException wse) {
eventHandler.onError(wse);
} catch (IOException ioe) {
eventHandler
.onError(new WebSocketException("error while connecting: " + ioe.getMessage(), ioe));
} finally {
close();
}
}
Thread getInnerThread() {
return innerThread;
}
}