|
44 | 44 | * |
45 | 45 | */ |
46 | 46 | public class WebSocketImpl extends WebSocket { |
47 | | - |
| 47 | + |
48 | 48 | public static final List<Draft> defaultdraftlist = new ArrayList<Draft>( 4 ); |
49 | 49 | static { |
50 | 50 | defaultdraftlist.add( new Draft_17() ); |
@@ -217,6 +217,10 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException |
217 | 217 | } catch ( InvalidDataException e ) { |
218 | 218 | flushAndClose( e.getCloseCode(), e.getMessage(), false ); |
219 | 219 | return false; |
| 220 | + } catch ( RuntimeException e ) { |
| 221 | + wsl.onWebsocketError( this, e ); |
| 222 | + flushAndClose( CloseFrame.NEVERCONNECTED, e.getMessage(), false ); |
| 223 | + return false; |
220 | 224 | } |
221 | 225 | write( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ), role ) ); |
222 | 226 | draft = d; |
@@ -264,6 +268,10 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException |
264 | 268 | } catch ( InvalidDataException e ) { |
265 | 269 | flushAndClose( e.getCloseCode(), e.getMessage(), false ); |
266 | 270 | return false; |
| 271 | + } catch ( RuntimeException e ) { |
| 272 | + wsl.onWebsocketError( this, e ); |
| 273 | + flushAndClose( CloseFrame.NEVERCONNECTED, e.getMessage(), false ); |
| 274 | + return false; |
267 | 275 | } |
268 | 276 | open( handshake ); |
269 | 277 | return true; |
@@ -374,8 +382,13 @@ private void close( int code, String message, boolean remote ) { |
374 | 382 | } |
375 | 383 | if( draft.getCloseHandshakeType() != CloseHandshakeType.NONE ) { |
376 | 384 | try { |
377 | | - if( !remote ) |
378 | | - wsl.onWebsocketCloseInitiated( this, code, message ); |
| 385 | + if( !remote ) { |
| 386 | + try { |
| 387 | + wsl.onWebsocketCloseInitiated( this, code, message ); |
| 388 | + } catch ( RuntimeException e ) { |
| 389 | + wsl.onWebsocketError( this, e ); |
| 390 | + } |
| 391 | + } |
379 | 392 | sendFrame( new CloseFrameBuilder( code, message ) ); |
380 | 393 | } catch ( InvalidDataException e ) { |
381 | 394 | wsl.onWebsocketError( this, e ); |
@@ -425,7 +438,11 @@ protected synchronized void closeConnection( int code, String message, boolean r |
425 | 438 | wsl.onWebsocketError( this, e ); |
426 | 439 | } |
427 | 440 | } |
428 | | - this.wsl.onWebsocketClose( this, code, message, remote ); |
| 441 | + try { |
| 442 | + this.wsl.onWebsocketClose( this, code, message, remote ); |
| 443 | + } catch ( RuntimeException e ) { |
| 444 | + wsl.onWebsocketError( this, e ); |
| 445 | + } |
429 | 446 | if( draft != null ) |
430 | 447 | draft.reset(); |
431 | 448 | tempContiniousFrame = null; |
@@ -461,8 +478,11 @@ protected synchronized void flushAndClose( int code, String message, boolean rem |
461 | 478 | flushandclosestate = true; |
462 | 479 |
|
463 | 480 | wsl.onWriteDemand( this ); // ensures that all outgoing frames are flushed before closing the connection |
464 | | - |
465 | | - this.wsl.onWebsocketClosing( this, code, message, remote ); |
| 481 | + try { |
| 482 | + wsl.onWebsocketClosing( this, code, message, remote ); |
| 483 | + } catch ( RuntimeException e ) { |
| 484 | + wsl.onWebsocketError( this, e ); |
| 485 | + } |
466 | 486 | if( draft != null ) |
467 | 487 | draft.reset(); |
468 | 488 | tempContiniousFrame = null; |
@@ -577,6 +597,9 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali |
577 | 597 | } catch ( InvalidDataException e ) { |
578 | 598 | // Stop if the client code throws an exception |
579 | 599 | throw new InvalidHandshakeException( "Handshake data rejected by client." ); |
| 600 | + } catch ( RuntimeException e ) { |
| 601 | + wsl.onWebsocketError( this, e ); |
| 602 | + throw new InvalidHandshakeException( "rejected because of" + e ); |
580 | 603 | } |
581 | 604 |
|
582 | 605 | // Send |
@@ -624,7 +647,11 @@ private void open( Handshakedata d ) throws IOException { |
624 | 647 | if( DEBUG ) |
625 | 648 | System.out.println( "open using draft: " + draft.getClass().getSimpleName() ); |
626 | 649 | handshakeComplete = true; |
627 | | - wsl.onWebsocketOpen( this, d ); |
| 650 | + try { |
| 651 | + wsl.onWebsocketOpen( this, d ); |
| 652 | + } catch ( RuntimeException e ) { |
| 653 | + wsl.onWebsocketError( this, e ); |
| 654 | + } |
628 | 655 | } |
629 | 656 |
|
630 | 657 | @Override |
|
0 commit comments