@@ -93,6 +93,10 @@ public class WebSocketImpl implements WebSocket {
9393 * When true no further frames may be submitted to be sent
9494 */
9595 private volatile boolean flushandclosestate = false ;
96+
97+ /**
98+ * The current state of the connection
99+ */
96100 private READYSTATE readystate = READYSTATE .NOT_YET_CONNECTED ;
97101
98102 /**
@@ -197,8 +201,8 @@ public void decode( ByteBuffer socketBuffer ) {
197201 if ( DEBUG )
198202 System .out .println ( "process(" + socketBuffer .remaining () + "): {" + ( socketBuffer .remaining () > 1000 ? "too big to display" : new String ( socketBuffer .array (), socketBuffer .position (), socketBuffer .remaining () ) ) + "}" );
199203
200- if ( readystate != READYSTATE .NOT_YET_CONNECTED ) {
201- if ( readystate == READYSTATE .OPEN ) {
204+ if ( getReadyState () != READYSTATE .NOT_YET_CONNECTED ) {
205+ if ( getReadyState () == READYSTATE .OPEN ) {
202206 decodeFrames ( socketBuffer );
203207 }
204208 } else {
@@ -416,11 +420,11 @@ private ByteBuffer generateHttpResponseDueToError( int errorCode ) {
416420 }
417421
418422 public void close ( int code , String message , boolean remote ) {
419- if ( readystate != READYSTATE .CLOSING && readystate != READYSTATE .CLOSED ) {
420- if ( readystate == READYSTATE .OPEN ) {
423+ if ( getReadyState () != READYSTATE .CLOSING && readystate != READYSTATE .CLOSED ) {
424+ if ( getReadyState () == READYSTATE .OPEN ) {
421425 if ( code == CloseFrame .ABNORMAL_CLOSE ) {
422426 assert ( !remote );
423- readystate = READYSTATE .CLOSING ;
427+ setReadyState ( READYSTATE .CLOSING ) ;
424428 flushAndClose ( code , message , false );
425429 return ;
426430 }
@@ -452,7 +456,7 @@ public void close( int code, String message, boolean remote ) {
452456 } else {
453457 flushAndClose ( CloseFrame .NEVER_CONNECTED , message , false );
454458 }
455- readystate = READYSTATE .CLOSING ;
459+ setReadyState ( READYSTATE .CLOSING ) ;
456460 tmpHandshakeBytes = null ;
457461 return ;
458462 }
@@ -475,7 +479,7 @@ public void close( int code, String message ) {
475479 * <code>remote</code> may also be true if this endpoint started the closing handshake since the other endpoint may not simply echo the <code>code</code> but close the connection the same time this endpoint does do but with an other <code>code</code>. <br>
476480 **/
477481 public synchronized void closeConnection ( int code , String message , boolean remote ) {
478- if ( readystate == READYSTATE .CLOSED ) {
482+ if ( getReadyState () == READYSTATE .CLOSED ) {
479483 return ;
480484 }
481485
@@ -505,8 +509,7 @@ public synchronized void closeConnection( int code, String message, boolean remo
505509 draft .reset ();
506510 handshakerequest = null ;
507511
508- readystate = READYSTATE .CLOSED ;
509- this .outQueue .clear ();
512+ setReadyState (READYSTATE .CLOSED );
510513 }
511514
512515 protected void closeConnection ( int code , boolean remote ) {
@@ -663,7 +666,7 @@ private HandshakeState isFlashEdgeCase( ByteBuffer request ) throws IncompleteHa
663666 }
664667
665668 public void startHandshake ( ClientHandshakeBuilder handshakedata ) throws InvalidHandshakeException {
666- assert ( readystate != READYSTATE .CONNECTING ) : "shall only be called once" ;
669+ assert ( getReadyState () != READYSTATE .CONNECTING ) : "shall only be called once" ;
667670
668671 // Store the Handshake Request we are about to send
669672 this .handshakerequest = draft .postProcessHandshakeRequestAsClient ( handshakedata );
@@ -717,7 +720,7 @@ private void write( List<ByteBuffer> bufs ) {
717720 private void open ( Handshakedata d ) {
718721 if ( DEBUG )
719722 System .out .println ( "open using draft: " + draft );
720- readystate = READYSTATE .OPEN ;
723+ setReadyState ( READYSTATE .OPEN ) ;
721724 try {
722725 wsl .onWebsocketOpen ( this , d );
723726 } catch ( RuntimeException e ) {
@@ -727,19 +730,19 @@ private void open( Handshakedata d ) {
727730
728731 @ Override
729732 public boolean isConnecting () {
730- assert ( !flushandclosestate || readystate == READYSTATE .CONNECTING );
731- return readystate == READYSTATE .CONNECTING ; // ifflushandclosestate
733+ assert ( !flushandclosestate || getReadyState () == READYSTATE .CONNECTING );
734+ return getReadyState () == READYSTATE .CONNECTING ; // ifflushandclosestate
732735 }
733736
734737 @ Override
735738 public boolean isOpen () {
736- assert ( readystate != READYSTATE .OPEN || !flushandclosestate );
737- return readystate == READYSTATE .OPEN ;
739+ assert ( getReadyState () != READYSTATE .OPEN || !flushandclosestate );
740+ return getReadyState () == READYSTATE .OPEN ;
738741 }
739742
740743 @ Override
741744 public boolean isClosing () {
742- return readystate == READYSTATE .CLOSING ;
745+ return getReadyState () == READYSTATE .CLOSING ;
743746 }
744747
745748 @ Override
@@ -749,14 +752,18 @@ public boolean isFlushAndClose() {
749752
750753 @ Override
751754 public boolean isClosed () {
752- return readystate == READYSTATE .CLOSED ;
755+ return getReadyState () == READYSTATE .CLOSED ;
753756 }
754757
755758 @ Override
756759 public READYSTATE getReadyState () {
757760 return readystate ;
758761 }
759762
763+ private void setReadyState ( READYSTATE readystate ) {
764+ this .readystate = readystate ;
765+ }
766+
760767 @ Override
761768 public int hashCode () {
762769 return super .hashCode ();
@@ -816,4 +823,5 @@ public void updateLastPong() {
816823 public WebSocketListener getWebSocketListener () {
817824 return wsl ;
818825 }
826+
819827}
0 commit comments