@@ -55,7 +55,7 @@ public class WebSocketImpl extends WebSocket {
5555
5656 public SelectionKey key ;
5757
58- /* only used to optain the socket addresses*/
58+ /* only used to obtain the socket addresses*/
5959 public final Socket socket ;
6060 /** the possibly wrapped channel object whose selection is controlled by {@link #key} */
6161 public ByteChannel channel ;
@@ -69,25 +69,14 @@ public class WebSocketImpl extends WebSocket {
6969 public final BlockingQueue <ByteBuffer > inQueue ;
7070
7171 /**
72- * Helper variable ment to store the thread which ( exclusively ) triggers this objects decode method.
72+ * Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
7373 **/
7474 public volatile WebSocketWorker workerThread ; // TODO reset worker?
7575
76- /**
77- * Determines whether to receive data as part of the
78- * handshake, or as part of text/data frame transmitted over the websocket.
79- */
80- private volatile boolean handshakeComplete = false ;
81- /**
82- * Determines whether we sent already a request to Close the connection or not.
83- */
84- private volatile boolean closeHandshakeSubmitted = false ;
85-
8676 /** When true no further frames may be submitted to be sent */
8777 private volatile boolean flushandclosestate = false ;
8878
89- /** When true the socket has been closed */
90- private volatile boolean isclosed = false ;
79+ private READYSTATE readystate = READYSTATE .NOTYETCONNECTED ;
9180
9281 /**
9382 * The listener to notify of WebSocket events.
@@ -135,25 +124,22 @@ public WebSocketImpl( WebSocketListener listener , Draft draft , Socket sock ) {
135124 inQueue = new LinkedBlockingQueue <ByteBuffer >();
136125 this .wsl = listener ;
137126 this .role = Role .CLIENT ;
138- this .draft = draft .copyInstance ();
127+ if ( draft != null )
128+ this .draft = draft .copyInstance ();
139129 this .socket = sock ;
140130 }
141131
142132 /**
143- * Should be called when a Selector has a key that is writable for this
144- * WebSocketImpl's SocketChannel connection.
145133 *
146- * @throws IOException
147- * When socket related I/O errors occur.
148134 */
149- public void decode ( ByteBuffer socketBuffer ) throws IOException {
135+ public void decode ( ByteBuffer socketBuffer ) {
150136 if ( !socketBuffer .hasRemaining () || flushandclosestate )
151137 return ;
152138
153139 if ( DEBUG )
154140 System .out .println ( "process(" + socketBuffer .remaining () + "): {" + ( socketBuffer .remaining () > 1000 ? "too big to display" : new String ( socketBuffer .array (), socketBuffer .position (), socketBuffer .remaining () ) ) + "}" );
155141
156- if ( handshakeComplete ) {
142+ if ( readystate == READYSTATE . OPEN ) {
157143 decodeFrames ( socketBuffer );
158144 } else {
159145 if ( decodeHandshake ( socketBuffer ) ) {
@@ -167,7 +153,7 @@ public void decode( ByteBuffer socketBuffer ) throws IOException {
167153 * Returns whether the handshake phase has is completed.
168154 * In case of a broken handshake this will be never the case.
169155 **/
170- private boolean decodeHandshake ( ByteBuffer socketBufferNew ) throws IOException {
156+ private boolean decodeHandshake ( ByteBuffer socketBufferNew ) {
171157 ByteBuffer socketBuffer ;
172158 if ( tmpHandshakeBytes == null ) {
173159 socketBuffer = socketBufferNew ;
@@ -324,7 +310,7 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
324310 code = cf .getCloseCode ();
325311 reason = cf .getMessage ();
326312 }
327- if ( closeHandshakeSubmitted ) {
313+ if ( readystate == READYSTATE . CLOSING ) {
328314 // complete the close handshake by disconnecting
329315 closeConnection ( code , reason , true );
330316 } else {
@@ -372,12 +358,12 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
372358 }
373359
374360 private void close ( int code , String message , boolean remote ) {
375- if ( ! closeHandshakeSubmitted ) {
376- if ( handshakeComplete ) {
361+ if ( readystate != READYSTATE . CLOSING && readystate != READYSTATE . CLOSED ) {
362+ if ( readystate == READYSTATE . OPEN ) {
377363 if ( code == CloseFrame .ABNORMAL_CLOSE ) {
378364 assert ( remote == false );
365+ readystate = READYSTATE .CLOSING ;
379366 flushAndClose ( code , message , false );
380- closeHandshakeSubmitted = true ;
381367 return ;
382368 }
383369 if ( draft .getCloseHandshakeType () != CloseHandshakeType .NONE ) {
@@ -404,7 +390,7 @@ private void close( int code, String message, boolean remote ) {
404390 }
405391 if ( code == CloseFrame .PROTOCOL_ERROR )// this endpoint found a PROTOCOL_ERROR
406392 flushAndClose ( code , message , remote );
407- closeHandshakeSubmitted = true ;
393+ readystate = READYSTATE . CLOSING ;
408394 tmpHandshakeBytes = null ;
409395 return ;
410396 }
@@ -425,7 +411,7 @@ public void close( int code, String message ) {
425411 **/
426412
427413 protected synchronized void closeConnection ( int code , String message , boolean remote ) {
428- if ( isclosed ) {
414+ if ( readystate == READYSTATE . CLOSED ) {
429415 return ;
430416 }
431417
@@ -448,7 +434,7 @@ protected synchronized void closeConnection( int code, String message, boolean r
448434 tempContiniousFrame = null ;
449435 handshakerequest = null ;
450436
451- isclosed = true ;
437+ readystate = READYSTATE . CLOSED ;
452438
453439 }
454440
@@ -583,10 +569,8 @@ private HandshakeState isFlashEdgeCase( ByteBuffer request ) throws IncompleteHa
583569 }
584570 }
585571
586- @ Override
587572 public void startHandshake ( ClientHandshakeBuilder handshakedata ) throws InvalidHandshakeException {
588- if ( handshakeComplete )
589- throw new IllegalStateException ( "Handshake has already been sent." );
573+ assert ( readystate != READYSTATE .CONNECTING ) : "shall only be called once" ;
590574
591575 // Store the Handshake Request we are about to send
592576 this .handshakerequest = draft .postProcessHandshakeRequestAsClient ( handshakedata );
@@ -643,10 +627,10 @@ private void deliverMessage( Framedata d ) throws InvalidDataException {
643627 }
644628 }
645629
646- private void open ( Handshakedata d ) throws IOException {
630+ private void open ( Handshakedata d ) {
647631 if ( DEBUG )
648632 System .out .println ( "open using draft: " + draft .getClass ().getSimpleName () );
649- handshakeComplete = true ;
633+ readystate = READYSTATE . OPEN ;
650634 try {
651635 wsl .onWebsocketOpen ( this , d );
652636 } catch ( RuntimeException e ) {
@@ -656,17 +640,19 @@ private void open( Handshakedata d ) throws IOException {
656640
657641 @ Override
658642 public boolean isConnecting () {
659- return ( !flushandclosestate && !closeHandshakeSubmitted && !handshakeComplete );
643+ assert ( flushandclosestate ? readystate == READYSTATE .CONNECTING : true );
644+ return readystate == READYSTATE .CONNECTING ; // ifflushandclosestate
660645 }
661646
662647 @ Override
663648 public boolean isOpen () {
664- return ( !flushandclosestate && !isclosed && !closeHandshakeSubmitted && handshakeComplete );
649+ assert ( readystate == READYSTATE .OPEN ? !flushandclosestate : true );
650+ return readystate == READYSTATE .OPEN ;
665651 }
666652
667653 @ Override
668654 public boolean isClosing () {
669- return ( ! isclosed && closeHandshakeSubmitted ) ;
655+ return readystate == READYSTATE . CLOSING ;
670656 }
671657
672658 @ Override
@@ -676,29 +662,12 @@ public boolean isFlushAndClose() {
676662
677663 @ Override
678664 public boolean isClosed () {
679- return isclosed ;
665+ return readystate == READYSTATE . CLOSED ;
680666 }
681667
682- /**
683- * Retrieve the WebSocketImpl 'readyState'.
684- * This represents the state of the connection.
685- * It returns a numerical value, as per W3C WebSockets specs.
686- *
687- * @return Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
688- */
689668 @ Override
690- public int getReadyState () {
691- if ( isConnecting () ) {
692- return READY_STATE_CONNECTING ;
693- } else if ( isOpen () ) {
694- return READY_STATE_OPEN ;
695- } else if ( isClosing () ) {
696- return READY_STATE_CLOSING ;
697- } else if ( isFlushAndClose () ) {
698- return READY_STATE_CLOSED ;
699- }
700- assert ( false );
701- return -1 ; // < This can't happen, by design!
669+ public READYSTATE getReadyState () {
670+ return readystate ;
702671 }
703672
704673 @ Override
0 commit comments