Skip to content

Commit bfc9fbf

Browse files
committed
Further worked on "better" close handshaking( 3 instead of one callback).
Instead of a java.nio.channels.NotYetConnectedException a new org.java_websocket.exceptions.WebsocketNotConnectedException will be thrown. Formatted code and started to renaming the inexpressive conn variables. The state getter methods still have to be touched.
1 parent ea0d816 commit bfc9fbf

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.java_websocket.exceptions.InvalidDataException;
2525
import org.java_websocket.exceptions.InvalidFrameException;
2626
import org.java_websocket.exceptions.InvalidHandshakeException;
27+
import org.java_websocket.exceptions.WebsocketNotConnectedException;
2728
import org.java_websocket.framing.CloseFrame;
2829
import org.java_websocket.framing.CloseFrameBuilder;
2930
import org.java_websocket.framing.Framedata;
@@ -291,7 +292,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException
291292
private void decodeFrames( ByteBuffer socketBuffer ) {
292293
if( flushandclosestate )
293294
return;
294-
assert ( isOpen() );
295+
assert ( !isClosed() );
295296
List<Framedata> frames;
296297
try {
297298
frames = draft.translateFrame( socketBuffer );
@@ -316,7 +317,8 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
316317
// echo close handshake
317318
if( draft.getCloseHandshakeType() == CloseHandshakeType.TWOWAY )
318319
close( code, reason, true );
319-
flushAndClose( code, reason, false );
320+
else
321+
flushAndClose( code, reason, false );
320322
}
321323
continue;
322324
} else if( curop == Opcode.PING ) {
@@ -355,7 +357,6 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
355357
}
356358
}
357359

358-
359360
private void close( int code, String message, boolean remote ) {
360361
if( !closeHandshakeSubmitted ) {
361362
if( handshakeComplete ) {
@@ -367,15 +368,15 @@ private void close( int code, String message, boolean remote ) {
367368
}
368369
if( draft.getCloseHandshakeType() != CloseHandshakeType.NONE ) {
369370
try {
371+
if( !remote )
372+
wsl.onWebsocketCloseInitiated( this, code, message );
370373
sendFrame( new CloseFrameBuilder( code, message ) );
371-
wsl.onWebsocketCloseInitiated( this, code, message );
372374
} catch ( InvalidDataException e ) {
373375
wsl.onWebsocketError( this, e );
374376
flushAndClose( CloseFrame.ABNORMAL_CLOSE, "generated frame is invalid", false );
375377
}
376-
} else {
377-
flushAndClose( code, message, false );
378378
}
379+
flushAndClose( code, message, remote );
379380
} else if( code == CloseFrame.FLASHPOLICY ) {
380381
assert ( remote );
381382
flushAndClose( CloseFrame.FLASHPOLICY, message, true );
@@ -425,13 +426,13 @@ protected synchronized void closeConnection( int code, String message, boolean r
425426
handshakerequest = null;
426427

427428
isclosed = true;
428-
429+
429430
}
430431

431432
protected void closeConnection( int code, boolean remote ) {
432433
closeConnection( code, "", remote );
433434
}
434-
435+
435436
public void closeConnection() {
436437
if( closedremotely == null ) {
437438
throw new IllegalStateException( "this method must be used in conjuction with flushAndClose" );
@@ -455,7 +456,7 @@ protected synchronized void flushAndClose( int code, String message, boolean rem
455456

456457
wsl.onWriteDemand( this ); // ensures that all outgoing frames are flushed before closing the connection
457458

458-
this.wsl.onWebsocketCloseInitiated( this, code, message );
459+
this.wsl.onWebsocketClosing( this, code, message, remote );
459460
if( draft != null )
460461
draft.reset();
461462
tempContiniousFrame = null;
@@ -494,7 +495,7 @@ public void close( InvalidDataException e ) {
494495
* @throws NotYetConnectedException
495496
*/
496497
@Override
497-
public void send( String text ) throws NotYetConnectedException {
498+
public void send( String text ) throws WebsocketNotConnectedException {
498499
if( text == null )
499500
throw new IllegalArgumentException( "Cannot send 'null' data to a WebSocketImpl." );
500501
send( draft.createFrames( text, role == Role.CLIENT ) );
@@ -507,20 +508,20 @@ public void send( String text ) throws NotYetConnectedException {
507508
* @throws NotYetConnectedException
508509
*/
509510
@Override
510-
public void send( ByteBuffer bytes ) throws IllegalArgumentException , NotYetConnectedException {
511+
public void send( ByteBuffer bytes ) throws IllegalArgumentException , WebsocketNotConnectedException {
511512
if( bytes == null )
512513
throw new IllegalArgumentException( "Cannot send 'null' data to a WebSocketImpl." );
513514
send( draft.createFrames( bytes, role == Role.CLIENT ) );
514515
}
515516

516517
@Override
517-
public void send( byte[] bytes ) throws IllegalArgumentException , NotYetConnectedException {
518+
public void send( byte[] bytes ) throws IllegalArgumentException , WebsocketNotConnectedException {
518519
send( ByteBuffer.wrap( bytes ) );
519520
}
520521

521522
private void send( Collection<Framedata> frames ) {
522-
if( !this.handshakeComplete )
523-
throw new NotYetConnectedException();
523+
if( !isOpen() )
524+
throw new WebsocketNotConnectedException();
524525
for( Framedata f : frames ) {
525526
sendFrame( f );
526527
}

src/main/java/org/java_websocket/WebSocketListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public interface WebSocketListener {
101101
* @param conn
102102
* The <tt>WebSocket</tt> instance this event is occuring on.
103103
*/
104-
public void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote );
104+
public void onWebsocketClose( WebSocket ws, int code, String reason, boolean remote );
105105

106106
/** called as soon as no further frames are accepted */
107-
public void onWebsocketClosing( WebSocket conn, int code, String reason, boolean remote );
107+
public void onWebsocketClosing( WebSocket ws, int code, String reason, boolean remote );
108108

109109
/** send when this peer sends a close handshake */
110-
public void onWebsocketCloseInitiated( WebSocket conn, int code, String reason );
110+
public void onWebsocketCloseInitiated( WebSocket ws, int code, String reason );
111111

112112
/**
113113
* Called if an exception worth noting occurred.

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.nio.channels.SelectionKey;
1313
import java.nio.channels.Selector;
1414
import java.nio.channels.SocketChannel;
15-
import java.nio.channels.UnresolvedAddressException;
1615
import java.util.Iterator;
1716
import java.util.List;
1817
import java.util.Map;
@@ -220,16 +219,11 @@ private final void interruptableRun() {
220219
} catch ( ClosedByInterruptException e ) {
221220
onWebsocketError( null, e );
222221
return;
223-
} catch ( IOException e ) {//
222+
} catch ( /*IOException | SecurityException | UnresolvedAddressException*/Exception e ) {//
224223
onWebsocketError( conn, e );
224+
conn.closeConnection( CloseFrame.NEVERCONNECTED, e.getMessage() );
225225
return;
226-
} catch ( SecurityException e ) {
227-
onWebsocketError( conn, e );
228-
return;
229-
} catch ( UnresolvedAddressException e ) {
230-
onWebsocketError( conn, e );
231-
return;
232-
}
226+
}
233227
conn = (WebSocketImpl) wf.createWebSocket( this, draft, channel.socket() );
234228
ByteBuffer buff = ByteBuffer.allocate( WebSocket.RCVBUF );
235229
try/*IO*/{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.java_websocket.exceptions;
2+
3+
public class WebsocketNotConnectedException extends RuntimeException {
4+
5+
}

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ private void registerWrite() throws CancelledKeyException {
420420
private void handleIOException( WebSocket conn, IOException ex ) {
421421
onWebsocketError( conn, ex );// conn may be null here
422422
if( conn != null ) {
423-
conn.close( CloseFrame.ABNORMAL_CLOSE );
423+
conn.closeConnection( CloseFrame.ABNORMAL_CLOSE, ex.getMessage() );
424424
}
425425
}
426426

0 commit comments

Comments
 (0)