55import java .nio .ByteBuffer ;
66import java .nio .channels .NotYetConnectedException ;
77import java .nio .channels .SocketChannel ;
8- import java .nio .charset .CharacterCodingException ;
98import java .util .ArrayList ;
109import java .util .Collection ;
1110import java .util .List ;
@@ -171,7 +170,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
171170 HandshakeBuilder response = wsl .onHandshakeRecievedAsServer ( this , d , handshake );
172171 writeDirect ( d .createHandshake ( d .postProcessHandshakeResponseAsServer ( handshake , response ), role ) );
173172 draft = d ;
174- open ();
173+ open ( handshake );
175174 handleRead ();
176175 return ;
177176 } else if ( handshakestate == HandshakeState .MATCHING ) {
@@ -202,7 +201,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
202201 handshakestate = draft .acceptHandshakeAsServer ( handshake );
203202
204203 if ( handshakestate == HandshakeState .MATCHED ) {
205- open ();
204+ open ( handshake );
206205 handleRead ();
207206 } else if ( handshakestate != HandshakeState .MATCHING ) {
208207 close ( CloseFrame .PROTOCOL_ERROR , "the handshake did finaly not match" );
@@ -214,7 +213,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
214213 handshake = draft .translateHandshake ( socketBuffer );
215214 handshakestate = draft .acceptHandshakeAsClient ( handshakerequest , handshake );
216215 if ( handshakestate == HandshakeState .MATCHED ) {
217- open ();
216+ open ( handshake );
218217 handleRead ();
219218 } else if ( handshakestate == HandshakeState .MATCHING ) {
220219 return ;
@@ -313,6 +312,8 @@ public void closeDirect( int code, String message ) throws IOException {
313312 wsl .onError ( this , e );
314313 closeConnection ( CloseFrame .ABNROMAL_CLOSE , "generated frame is invalid" , false );
315314 }
315+ } else {
316+ closeConnection ( CloseFrame .NEVERCONNECTED , false );
316317 }
317318 if ( code == CloseFrame .PROTOCOL_ERROR )// this endpoint found a PROTOCOL_ERROR
318319 closeConnection ( code , false );
@@ -342,7 +343,7 @@ public void closeConnection( int code, String message, boolean remote ) {
342343 } catch ( IOException e ) {
343344 wsl .onError ( this , e );
344345 }
345- this .wsl .onClose ( this , code , message );
346+ this .wsl .onClose ( this , code , message , remote );
346347 if ( draft != null )
347348 draft .reset ();
348349 currentframe = null ;
@@ -380,11 +381,11 @@ public void send( byte[] bytes ) throws IllegalArgumentException , NotYetConnect
380381 send ( draft .createFrames ( bytes , role == Role .CLIENT ) );
381382 }
382383
383- private void send ( Collection <Framedata > frames ) throws InterruptedException { // TODO instead of throwing or returning an error this method maybe should block on queue jams
384+ private void send ( Collection <Framedata > frames ) throws InterruptedException {
384385 if ( !this .handshakeComplete )
385386 throw new NotYetConnectedException ();
386387 for ( Framedata f : frames ) {
387- sendFrame ( f ); // TODO high frequently calls to sendFrame are inefficient.
388+ sendFrame ( f );
388389 }
389390 }
390391
@@ -464,7 +465,7 @@ private void writeDirect( List<ByteBuffer> bufs ) throws IOException {
464465 channelWriteDirect ( b );
465466 }
466467 }
467- private void deliverMessage ( Framedata d ) throws CharacterCodingException {
468+ private void deliverMessage ( Framedata d ) throws InvalidDataException {
468469 if ( d .getOpcode () == Opcode .TEXT ) {
469470 wsl .onMessage ( this , Charsetfunctions .stringUtf8 ( d .getPayloadData () ) );
470471 } else if ( d .getOpcode () == Opcode .BINARY ) {
@@ -476,11 +477,11 @@ private void deliverMessage( Framedata d ) throws CharacterCodingException {
476477 }
477478 }
478479
479- private void open () throws IOException {
480+ private void open ( Handshakedata d ) throws IOException {
480481 if ( DEBUG )
481482 System .out .println ( "open using draft: " + draft .getClass ().getSimpleName () );
482483 handshakeComplete = true ;
483- wsl .onOpen ( this );
484+ wsl .onOpen ( this , d );
484485 }
485486
486487 public InetSocketAddress getRemoteSocketAddress () {
0 commit comments