-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathWebSocketConnection.java
766 lines (635 loc) · 25.7 KB
/
WebSocketConnection.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
///////////////////////////////////////////////////////////////////////////////
//
// AutobahnJava - http://crossbar.io/autobahn
//
// Copyright (c) Crossbar.io Technologies GmbH and contributors
//
// Licensed under the MIT License.
// http://www.opensource.org/licenses/mit-license.php
//
///////////////////////////////////////////////////////////////////////////////
package io.crossbar.autobahn.websocket;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import io.crossbar.autobahn.utils.ABLogger;
import io.crossbar.autobahn.utils.IABLogger;
import io.crossbar.autobahn.websocket.exceptions.ParseFailed;
import io.crossbar.autobahn.websocket.exceptions.WebSocketException;
import io.crossbar.autobahn.websocket.interfaces.IThreadMessenger;
import io.crossbar.autobahn.websocket.interfaces.IWebSocket;
import io.crossbar.autobahn.websocket.interfaces.IWebSocketConnectionHandler;
import io.crossbar.autobahn.websocket.messages.BinaryMessage;
import io.crossbar.autobahn.websocket.messages.CannotConnect;
import io.crossbar.autobahn.websocket.messages.ClientHandshake;
import io.crossbar.autobahn.websocket.messages.Close;
import io.crossbar.autobahn.websocket.messages.ConnectionLost;
import io.crossbar.autobahn.websocket.messages.Error;
import io.crossbar.autobahn.websocket.messages.Ping;
import io.crossbar.autobahn.websocket.messages.Pong;
import io.crossbar.autobahn.websocket.messages.ProtocolViolation;
import io.crossbar.autobahn.websocket.messages.RawTextMessage;
import io.crossbar.autobahn.websocket.messages.ServerError;
import io.crossbar.autobahn.websocket.messages.ServerHandshake;
import io.crossbar.autobahn.websocket.messages.TextMessage;
import io.crossbar.autobahn.websocket.types.ConnectionResponse;
import io.crossbar.autobahn.websocket.types.WebSocketOptions;
import static io.crossbar.autobahn.websocket.utils.Platform.selectThreadMessenger;
public class WebSocketConnection implements IWebSocket {
private static final IABLogger LOGGER = ABLogger.getLogger(WebSocketConnection.class.getName());
private IThreadMessenger mMessenger;
private WebSocketReader mReader;
private ExecutorService mWriterThread;
private Connection mWebSocket;
private BufferedOutputStream mBufferedOutputStream;
private Socket mSocket;
private URI mWsUri;
private String mWsScheme;
private String mWsHost;
private int mWsPort;
private String mWsPath;
private String mWsQuery;
private String[] mWsSubprotocols;
private Map<String, String> mWsHeaders;
private IWebSocketConnectionHandler mWsHandler;
private WebSocketOptions mOptions;
private boolean mActive;
private boolean mPrevConnected;
private boolean onCloseCalled;
private ScheduledExecutorService mExecutor;
private ScheduledFuture<?> mPingerTask;
private ScheduledFuture<?> mTimeoutTask;
private final Runnable mAutoPinger = new Runnable() {
@Override
public void run() {
if (!isConnected()) {
return;
}
if (mReader.getTimeSinceLastRead() >= mOptions.getAutoPingInterval()) {
sendPing();
mTimeoutTask = mExecutor.schedule(() -> {
if (!isConnected()) {
return;
}
// We didn't receive a WebSocket read, something is likely wrong there.
if (mReader.getTimeSinceLastRead() >= mOptions.getAutoPingTimeout()) {
mMessenger.notify(new ConnectionLost("WebSocket ping timed out."));
}
}, mOptions.getAutoPingTimeout(), TimeUnit.SECONDS);
}
}
};
/**
* Asynchronous socket connector.
*/
private class WebSocketConnector extends Thread {
public void run() {
Thread.currentThread().setName("WebSocketConnector");
/*
* connect TCP socket
*/
try {
if (mWsScheme.equals("wss")) {
mSocket = SSLSocketFactory.getDefault().createSocket();
} else {
mSocket = SocketFactory.getDefault().createSocket();
}
if (mOptions.getTLSEnabledProtocols() != null){
setEnabledProtocolsOnSSLSocket(mSocket, mOptions.getTLSEnabledProtocols());
}
// the following will block until connection was established or
// an error occurred!
mSocket.connect(new InetSocketAddress(mWsHost, mWsPort),
mOptions.getSocketConnectTimeout());
// before doing any data transfer on the socket, set socket
// options
mSocket.setSoTimeout(mOptions.getSocketReceiveTimeout());
mSocket.setTcpNoDelay(mOptions.getTcpNoDelay());
} catch (IOException e) {
mMessenger.notify(new CannotConnect(e.getMessage()));
return;
}
if (mExecutor == null || mExecutor.isShutdown()) {
mExecutor = Executors.newSingleThreadScheduledExecutor();
}
if (isConnected()) {
try {
// create & start WebSocket reader
createReader();
// create & start WebSocket writer
createWriter();
// start WebSocket handshake
ClientHandshake hs = new ClientHandshake(mWsHost + ":" + mWsPort);
hs.mPath = mWsPath;
hs.mQuery = mWsQuery;
hs.mSubprotocols = mWsSubprotocols;
hs.mHeaderList = mWsHeaders;
sendMessage(hs);
mPrevConnected = true;
} catch (Exception e) {
mMessenger.notify(new Error(e));
}
} else {
mMessenger.notify(new CannotConnect("Could not connect to WebSocket server"));
}
}
}
public WebSocketConnection() {
LOGGER.d("Created");
// create WebSocket master handler
createHandler();
// set initial values
mActive = false;
mPrevConnected = false;
}
@Override
public void sendMessage(String payload) {
sendMessage(new TextMessage(payload));
}
@Override
public void sendMessage(byte[] payload, boolean isBinary) {
if (isBinary) {
sendMessage(new BinaryMessage(payload));
} else {
sendMessage(new RawTextMessage(payload));
}
}
@Override
public void sendPing() {
sendMessage(new Ping());
LOGGER.d("WebSocket Ping sent");
}
@Override
public void sendPing(byte[] payload) {
sendMessage(new Ping(payload));
}
@Override
public void sendPong() {
sendMessage(new Pong());
LOGGER.d("WebSocket Pong sent");
}
@Override
public void sendPong(byte[] payload) {
sendMessage(new Pong(payload));
LOGGER.d("WebSocket Pong sent");
}
@Override
public boolean isConnected() {
return mSocket != null && mSocket.isConnected() && !mSocket.isClosed();
}
private void closeReaderThread(boolean waitForQuit) {
if (mReader != null) {
mReader.quit();
if (waitForQuit) {
try {
mReader.join();
} catch (InterruptedException e) {
LOGGER.v(e.getMessage(), e);
}
}
} else {
LOGGER.d("mReader already NULL");
}
}
private void closeUnderlyingSocket() throws InterruptedException {
Thread cleaner = new Thread(() -> {
if (isConnected()) {
try {
mSocket.close();
} catch (IOException e) {
LOGGER.v(e.getMessage(), e);
}
}
});
cleaner.start();
cleaner.join();
}
private void closeWriterThread() {
if (mWriterThread != null) {
try {
mWriterThread.shutdown();
mWriterThread.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.v(e.getMessage(), e);
}
}
}
private void failConnection(int code, String reason) {
LOGGER.d("fail connection [code = " + code + ", reason = " + reason);
closeReaderThread(false);
closeWriterThread();
if (isConnected()) {
try {
closeUnderlyingSocket();
} catch (InterruptedException e) {
LOGGER.v(e.getMessage(), e);
}
} else {
LOGGER.d("Socket already closed");
}
closeReaderThread(true);
onClose(code, reason);
LOGGER.d("Worker threads stopped");
}
@Override
public void connect(String wsUri, IWebSocketConnectionHandler wsHandler)
throws WebSocketException {
connect(wsUri, null, wsHandler, null, null);
}
@Override
public void connect(String wsUri, IWebSocketConnectionHandler wsHandler,
WebSocketOptions options) throws WebSocketException {
connect(wsUri, null, wsHandler, options, null);
}
@Override
public void connect(String wsUri, String[] wsSubprotocols,
IWebSocketConnectionHandler wsHandler) throws WebSocketException {
connect(wsUri, wsSubprotocols, wsHandler, new WebSocketOptions(), null);
}
@Override
public void connect(String wsUri, String[] wsSubprotocols,
IWebSocketConnectionHandler wsHandler, WebSocketOptions options,
Map<String, String> headers) throws WebSocketException {
// don't connect if already connected .. user needs to disconnect first
//
if (isConnected()) {
throw new WebSocketException("already connected");
}
// parse WebSocket URI
//
try {
mWsUri = new URI(wsUri);
mWsScheme = mWsUri.getScheme();
if (mWsScheme == null || (!mWsScheme.equals("ws") && !mWsScheme.equals("wss"))) {
throw new WebSocketException("unsupported scheme for WebSocket URI");
}
if (mWsUri.getPort() == -1) {
if (mWsScheme.equals("ws")) {
mWsPort = 80;
} else {
mWsPort = 443;
}
} else {
mWsPort = mWsUri.getPort();
}
if (mWsUri.getHost() == null) {
throw new WebSocketException("no host specified in WebSocket URI");
} else {
mWsHost = mWsUri.getHost();
}
if (mWsUri.getRawPath() == null || mWsUri.getRawPath().equals("")) {
mWsPath = "/";
} else {
mWsPath = mWsUri.getRawPath();
}
if (mWsUri.getRawQuery() == null || mWsUri.getRawQuery().equals("")) {
mWsQuery = null;
} else {
mWsQuery = mWsUri.getRawQuery();
}
} catch (URISyntaxException e) {
throw new WebSocketException("invalid WebSocket URI");
}
mWsSubprotocols = wsSubprotocols;
mWsHeaders = headers;
mWsHandler = wsHandler;
if (mOptions == null) {
if (options == null) {
mOptions = new WebSocketOptions();
} else {
// make copy of options!
mOptions = new WebSocketOptions(options);
}
// WebSocketOptions were already, replace them if an instance was provided
// with connect() as well.
} else if (options != null) {
mOptions = new WebSocketOptions(options);
}
// set connection active
mActive = true;
// reset value
onCloseCalled = false;
// use async connector on short-lived background thread
new WebSocketConnector().start();
}
@Override
public void sendClose() {
sendClose(1000);
}
@Override
public void sendClose(int code) {
sendClose(code, null);
}
@Override
public void sendClose(int code, String reason) {
// Close the writer thread here but delay the closing of reader thread
// as we need to have active connection to be able to process the response
// of this close request.
sendMessage(new Close(code, reason));
onCloseCalled = false;
mActive = false;
mPrevConnected = false;
}
/**
* Reconnect to the server with the latest options
*
* @return true if reconnection performed
*/
public boolean reconnect() {
if (!isConnected() && (mWsUri != null)) {
onCloseCalled = false;
new WebSocketConnector().start();
return true;
}
return false;
}
/**
* Perform reconnection
*
* @return true if reconnection was scheduled
*/
private boolean scheduleReconnect() {
/**
* Reconnect only if:
* - connection active (connected but not disconnected)
* - has previous success connections
* - reconnect interval is set
*/
int interval = mOptions.getReconnectInterval();
boolean need = mActive && mPrevConnected && (interval > 0);
if (need) {
LOGGER.d("Reconnection scheduled");
mMessenger.postDelayed(() -> {
LOGGER.d("Reconnecting...");
reconnect();
}, interval);
}
return need;
}
/**
* Common close handler
*
* @param code Close code.
* @param reason Close reason (human-readable).
*/
private void onClose(int code, String reason) {
boolean reconnecting = false;
if ((code == IWebSocketConnectionHandler.CLOSE_CANNOT_CONNECT) ||
(code == IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST)) {
reconnecting = scheduleReconnect();
}
// Shutdown the executor so that it stops attempting to send pings.
if (mExecutor != null) {
mExecutor.shutdown();
}
mMessenger.cleanup();
if (mWsHandler != null) {
try {
if (reconnecting) {
mWsHandler.onClose(IWebSocketConnectionHandler.CLOSE_RECONNECT, reason);
} else {
mWsHandler.onClose(code, reason);
}
} catch (Exception e) {
LOGGER.v(e.getMessage(), e);
}
//mWsHandler = null;
} else {
LOGGER.d("mWsHandler already NULL");
}
onCloseCalled = true;
}
private void closeAndCleanup() {
// Close the reader thread but don't wait for it to quit because
// the blocking call to BufferedInputStream.read() can take a
// a few seconds in some cases to unblock. We call this method later
// a few lines below *after* we close the socket, because as soon as
// the Socket.close() is called, BufferedInputStream.read() throws.
// So this gives us quick cleaning of resources.
closeReaderThread(false);
closeWriterThread();
if (isConnected()) {
try {
closeUnderlyingSocket();
} catch (InterruptedException e) {
LOGGER.v(e.getMessage(), e);
}
}
closeReaderThread(true);
onCloseCalled = false;
}
private <T> T getOrDefault(Map<?, ?> obj, Object key, T default_value) {
if (obj.containsKey(key)) {
return (T) obj.get(key);
}
return default_value;
}
public void setOptions(WebSocketOptions options) {
boolean optionsWasNull = mOptions == null;
if (mOptions == null) {
mOptions = new WebSocketOptions(options);
} else {
mOptions.setAutoPingInterval(options.getAutoPingInterval());
mOptions.setAutoPingTimeout(options.getAutoPingTimeout());
}
if (!optionsWasNull) {
// Now do the magic here.
if (mPingerTask != null) {
mPingerTask.cancel(true);
}
if (mExecutor == null) {
mExecutor = Executors.newSingleThreadScheduledExecutor();
}
if (isConnected() && mOptions.getAutoPingInterval() > 0) {
mPingerTask = mExecutor.scheduleAtFixedRate(
mAutoPinger, mOptions.getAutoPingInterval(),
mOptions.getAutoPingInterval(), TimeUnit.SECONDS);
}
}
}
/**
* Create master message handler.
*/
private void createHandler() {
mMessenger = selectThreadMessenger();
mMessenger.setOnMessageListener(new IThreadMessenger.OnMessageListener() {
@Override
public void onMessage(Object message) {
// We have received the closing handshake and replied to it, discard
// anything received after that.
if (onCloseCalled) {
LOGGER.d("onClose called already, ignore message.");
return;
}
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
if (mWsHandler != null) {
mWsHandler.onMessage(textMessage.mPayload);
} else {
LOGGER.d("could not call onTextMessage() .. handler already NULL");
}
} else if (message instanceof RawTextMessage) {
RawTextMessage rawTextMessage = (RawTextMessage) message;
if (mWsHandler != null) {
mWsHandler.onMessage(rawTextMessage.mPayload, false);
} else {
LOGGER.d("could not call onRawTextMessage() .. handler already NULL");
}
} else if (message instanceof BinaryMessage) {
BinaryMessage binaryMessage = (BinaryMessage) message;
if (mWsHandler != null) {
mWsHandler.onMessage(binaryMessage.mPayload, true);
} else {
LOGGER.d("could not call onBinaryMessage() .. handler already NULL");
}
} else if (message instanceof Ping) {
Ping ping = (Ping) message;
LOGGER.d("WebSocket Ping received");
if (ping.mPayload == null) {
mWsHandler.onPing();
} else {
mWsHandler.onPing(ping.mPayload);
}
} else if (message instanceof Pong) {
Pong pong = (Pong) message;
if (pong.mPayload == null) {
mWsHandler.onPong();
} else {
mWsHandler.onPong(pong.mPayload);
}
// We already received a pong, cancel the timeout executor
if (mTimeoutTask != null && !mTimeoutTask.isDone() && !mTimeoutTask.isCancelled()) {
mTimeoutTask.cancel(true);
}
LOGGER.d("WebSocket Pong received");
} else if (message instanceof Close) {
Close close = (Close) message;
final int crossbarCloseCode = (close.mCode == 1000) ? IWebSocketConnectionHandler.CLOSE_NORMAL : IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST;
if (close.mIsReply) {
LOGGER.d("WebSocket Close received (" + close.mCode + " - " + close.mReason + ")");
closeAndCleanup();
onClose(crossbarCloseCode, close.mReason);
} else if (mActive) {
// We have received a close frame, lets clean.
closeReaderThread(false);
WebSocketConnection.this.sendMessage(new Close(1000, true));
mActive = false;
} else {
LOGGER.d("WebSocket Close received (" + close.mCode + " - " + close.mReason + ")");
// we've initiated disconnect, so ready to close the channel
closeAndCleanup();
onClose(crossbarCloseCode, close.mReason);
}
} else if (message instanceof ServerHandshake) {
ServerHandshake serverHandshake = (ServerHandshake) message;
LOGGER.d("opening handshake received");
if (mWsHandler != null) {
String protocol = getOrDefault(serverHandshake.headers,
"sec-websocket-protocol", null);
mWsHandler.setConnection(WebSocketConnection.this);
mWsHandler.onConnect(new ConnectionResponse(protocol));
mWsHandler.onOpen();
LOGGER.d("onOpen() called, ready to rock.");
if (mOptions.getAutoPingInterval() > 0) {
mPingerTask = mExecutor.scheduleAtFixedRate(
mAutoPinger, mOptions.getAutoPingInterval(),
mOptions.getAutoPingInterval(), TimeUnit.SECONDS);
}
} else {
LOGGER.d("could not call onOpen() .. handler already NULL");
}
} else if (message instanceof CannotConnect) {
CannotConnect cannotConnect = (CannotConnect) message;
failConnection(IWebSocketConnectionHandler.CLOSE_CANNOT_CONNECT,
cannotConnect.reason);
} else if (message instanceof ConnectionLost) {
ConnectionLost connnectionLost = (ConnectionLost) message;
failConnection(IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST,
connnectionLost.reason);
} else if (message instanceof ProtocolViolation) {
failConnection(IWebSocketConnectionHandler.CLOSE_PROTOCOL_ERROR,
"WebSocket protocol violation");
} else if (message instanceof Error) {
Error error = (Error) message;
failConnection(IWebSocketConnectionHandler.CLOSE_INTERNAL_ERROR,
"WebSocket internal error (" + error.mException.toString() + ")");
} else if (message instanceof ServerError) {
ServerError error = (ServerError) message;
failConnection(IWebSocketConnectionHandler.CLOSE_SERVER_ERROR,
"Server error " + error.mStatusMessage);
} else {
processAppMessage(message);
}
}
});
}
private void processAppMessage(Object message) {
}
/**
* Create WebSocket background writer.
*/
private void createWriter() throws IOException {
mWriterThread = Executors.newSingleThreadExecutor();
mBufferedOutputStream = new BufferedOutputStream(mSocket.getOutputStream(),
mOptions.getMaxFramePayloadSize() + 14);
mWebSocket = new Connection(mOptions);
LOGGER.d("WS writer created and started");
}
private void sendMessage(io.crossbar.autobahn.websocket.messages.Message message) {
if (mWriterThread == null || mWriterThread.isShutdown()) {
return;
}
mWriterThread.submit(new Runnable() {
@Override
public void run() {
try {
mBufferedOutputStream.write(mWebSocket.send(message));
mBufferedOutputStream.flush();
if (message instanceof Close) {
Close msg = (Close) message;
if (msg.mIsReply) {
mMessenger.notify(message);
}
}
} catch (SocketException e) {
LOGGER.d("run() : SocketException (" + e.toString() + ")");
mMessenger.notify(new ConnectionLost(null));
} catch (ParseFailed | IOException e) {
LOGGER.w(e.getMessage(), e);
mMessenger.notify(new Error(e));
}
}
});
}
/**
* Create WebSocket background reader.
*/
private void createReader() throws IOException {
mReader = new WebSocketReader(mMessenger, mSocket, mOptions, "WebSocketReader");
mReader.start();
LOGGER.d("WS reader created and started");
}
/**
* Enable protocols on SSLSocket.
* @param socket
* @param protocols
*/
private void setEnabledProtocolsOnSSLSocket(Socket socket, String[] protocols) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(protocols);
}
}
}