Skip to content

Commit 7b4cdff

Browse files
authored
Merge pull request TooTallNate#578 from marci4/master
Refactoring and improved onClose behaviour
2 parents 3ff3a20 + 4057d17 commit 7b4cdff

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,6 @@ public final void onWebsocketClose( WebSocket conn, int code, String reason, boo
370370
stopConnectionLostTimer();
371371
if( writeThread != null )
372372
writeThread.interrupt();
373-
try {
374-
if( socket != null )
375-
socket.close();
376-
} catch ( IOException e ) {
377-
onWebsocketError( this, e );
378-
}
379373
onClose( code, reason, remote );
380374
connectLatch.countDown();
381375
closeLatch.countDown();
@@ -464,16 +458,34 @@ private class WebsocketWriteThread implements Runnable {
464458
public void run() {
465459
Thread.currentThread().setName( "WebsocketWriteThread" );
466460
try {
467-
while( !Thread.interrupted() ) {
468-
ByteBuffer buffer = engine.outQueue.take();
469-
ostream.write( buffer.array(), 0, buffer.limit() );
470-
ostream.flush();
461+
try {
462+
while( !Thread.interrupted() ) {
463+
ByteBuffer buffer = engine.outQueue.take();
464+
ostream.write( buffer.array(), 0, buffer.limit() );
465+
ostream.flush();
466+
}
467+
} catch ( InterruptedException e ) {
468+
for (ByteBuffer buffer : engine.outQueue) {
469+
ostream.write( buffer.array(), 0, buffer.limit() );
470+
ostream.flush();
471+
}
471472
}
472473
} catch ( IOException e ) {
473-
handleIOException(e);
474-
} catch ( InterruptedException e ) {
475-
// this thread is regularly terminated via an interrupt
474+
handleIOException( e );
475+
} finally {
476+
closeOutputAndSocket();
477+
}
478+
}
479+
}
480+
481+
private void closeOutputAndSocket() {
482+
try {
483+
if( socket != null ) {
484+
socket.shutdownOutput();
485+
socket.close();
476486
}
487+
} catch ( IOException ex ) {
488+
onWebsocketError( this, ex );
477489
}
478490
}
479491

src/main/java/org/java_websocket/framing/CloseFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public String toString() {
210210
public void isValid() throws InvalidDataException {
211211
super.isValid();
212212
if (code == CloseFrame.NO_UTF8 && reason == null) {
213-
throw new InvalidDataException( CloseFrame.NO_UTF8 );
213+
throw new InvalidDataException( CloseFrame.NO_UTF8, "Received text is no valid utf8 string!");
214214
}
215215
if (code == CloseFrame.NOCODE && 0 < reason.length()) {
216216
throw new InvalidDataException(PROTOCOL_ERROR, "A close frame must have a closecode if it has a reason");

src/main/java/org/java_websocket/framing/TextFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public TextFrame() {
4444
public void isValid() throws InvalidDataException {
4545
super.isValid();
4646
if (!Charsetfunctions.isValidUTF8( getPayloadData() )) {
47-
throw new InvalidDataException(CloseFrame.NO_UTF8);
47+
throw new InvalidDataException(CloseFrame.NO_UTF8, "Received text is no valid utf8 string!");
4848
}
4949
}
5050
}

0 commit comments

Comments
 (0)