1212import java .util .concurrent .LinkedBlockingQueue ;
1313
1414import org .java_websocket .drafts .Draft ;
15+ import org .java_websocket .drafts .Draft .CloseHandshakeType ;
1516import org .java_websocket .drafts .Draft .HandshakeState ;
1617import org .java_websocket .drafts .Draft_10 ;
1718import org .java_websocket .drafts .Draft_17 ;
@@ -139,7 +140,7 @@ public WebSocket( WebSocketListener listener , List<Draft> drafts , SocketChanne
139140
140141 private void init ( WebSocketListener listener , Draft draft , SocketChannel socketchannel ) {
141142 this .sockchannel = socketchannel ;
142- this .bufferQueue = new LinkedBlockingQueue <ByteBuffer >( 10 );
143+ this .bufferQueue = new LinkedBlockingQueue <ByteBuffer >( 3 );
143144 this .socketBuffer = ByteBuffer .allocate ( 65558 );
144145 socketBuffer .flip ();
145146 this .wsl = listener ;
@@ -160,7 +161,19 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel socket
160161 socketBuffer .rewind ();
161162 socketBuffer .limit ( socketBuffer .capacity () );
162163 if ( sockchannel .read ( socketBuffer ) == -1 ) {
163- close ( CloseFrame .ABNROMAL_CLOSE );
164+ if ( draft == null ) {
165+ closeConnection ( CloseFrame .ABNROMAL_CLOSE , true );
166+ } else if ( draft .getCloseHandshakeType () == CloseHandshakeType .NONE ) {
167+ closeConnection ( CloseFrame .NORMAL , true );
168+ } else if ( draft .getCloseHandshakeType () == CloseHandshakeType .ONEWAY ) {
169+ if ( role == Role .SERVER )
170+ closeConnection ( CloseFrame .ABNROMAL_CLOSE , true );
171+ else
172+ closeConnection ( CloseFrame .NORMAL , true );
173+ } else {
174+ closeConnection ( CloseFrame .ABNROMAL_CLOSE , true );
175+ }
176+
164177 }
165178
166179 socketBuffer .flip ();
@@ -298,7 +311,8 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel socket
298311 closeConnection ( code , reason , true );
299312 } else {
300313 // echo close handshake
301- close ( code , reason );
314+ if ( draft .getCloseHandshakeType () == CloseHandshakeType .TWOWAY )
315+ close ( code , reason );
302316 closeConnection ( code , reason , false );
303317 }
304318 continue ;
@@ -363,7 +377,7 @@ public void closeDirect( int code, String message ) throws IOException {
363377 return ;
364378 }
365379 flush ();
366- if ( draft .hasCloseHandshake () ) {
380+ if ( draft .getCloseHandshakeType () != CloseHandshakeType . NONE ) {
367381 try {
368382 sendFrameDirect ( new CloseFrameBuilder ( code , message ) );
369383 } catch ( InvalidDataException e ) {
@@ -477,7 +491,7 @@ boolean hasBufferedData() {
477491
478492 /**
479493 * The amount of data in Queue, ready to be sent.
480- *
494+ *
481495 * @return Amount of Data still in Queue and not sent yet of the socket
482496 */
483497 long bufferedDataAmount () {
@@ -494,7 +508,7 @@ public void flush() throws IOException {
494508 if ( buffer .remaining () > 0 ) {
495509 continue ;
496510 } else {
497- synchronized (bufferQueueTotalAmount ) {
511+ synchronized ( bufferQueueTotalAmount ) {
498512 // subtract this amount of data from the total queued (synchronized over this object)
499513 bufferQueueTotalAmount -= buffer .limit ();
500514 }
@@ -545,7 +559,7 @@ private void channelWrite( ByteBuffer buf ) throws InterruptedException {
545559 if ( DEBUG )
546560 System .out .println ( "write(" + buf .limit () + "): {" + ( buf .limit () > 1000 ? "too big to display" : new String ( buf .array () ) ) + "}" );
547561 buf .rewind (); // TODO rewinding should not be nessesary
548- synchronized (bufferQueueTotalAmount ) {
562+ synchronized ( bufferQueueTotalAmount ) {
549563 // add up the number of bytes to the total queued (synchronized over this object)
550564 bufferQueueTotalAmount += buf .limit ();
551565 }
0 commit comments