55import java .nio .ByteBuffer ;
66import java .nio .channels .NotYetConnectedException ;
77import java .nio .channels .SocketChannel ;
8+ import java .nio .charset .CharacterCodingException ;
89import java .util .ArrayList ;
910import java .util .Collection ;
1011import java .util .List ;
@@ -229,65 +230,60 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
229230 List <Framedata > frames ;
230231 try {
231232 frames = draft .translateFrame ( socketBuffer );
232- } catch ( InvalidDataException e1 ) {
233- close ( e1 );
234- return ;
235- }
236- for ( Framedata f : frames ) {
237- if ( DEBUG )
238- System .out .println ( "matched frame: " + f );
239- Opcode curop = f .getOpcode ();
240- if ( curop == null )// Ignore undefined opcodes
241- continue ;
242- else if ( curop == Opcode .CLOSING ) {
243- int code = CloseFrame .NOCODE ;
244- String reason = "" ;
245- if ( f instanceof CloseFrame ) {
246- CloseFrame cf = (CloseFrame ) f ;
247- code = cf .getCloseCode ();
248- reason = cf .getMessage ();
249- }
250- if ( closeHandshakeSent ) {
251- // complete the close handshake by disconnecting
252- closeConnection ( code , reason , true );
253- } else {
254- // echo close handshake
255- close ( code , reason );
256- closeConnection ( code , reason , false );
257- }
258- continue ;
259- } else if ( curop == Opcode .PING ) {
260- wsl .onPing ( this , f );
261- continue ;
262- } else if ( curop == Opcode .PONG ) {
263- wsl .onPong ( this , f );
264- continue ;
265- }
266- if ( currentframe == null ) {
267- if ( f .isFin () ) {
268- if ( f .getOpcode () == Opcode .TEXT ) {
269- wsl .onMessage ( this , Charsetfunctions .stringUtf8 ( f .getPayloadData () ) );
270- } else if ( f .getOpcode () == Opcode .BINARY ) {
271- wsl .onMessage ( this , f .getPayloadData () );
233+ for ( Framedata f : frames ) {
234+ if ( DEBUG )
235+ System .out .println ( "matched frame: " + f );
236+ Opcode curop = f .getOpcode ();
237+ if ( curop == Opcode .CLOSING ) {
238+ int code = CloseFrame .NOCODE ;
239+ String reason = "" ;
240+ if ( f instanceof CloseFrame ) {
241+ CloseFrame cf = (CloseFrame ) f ;
242+ code = cf .getCloseCode ();
243+ reason = cf .getMessage ();
244+ }
245+ if ( closeHandshakeSent ) {
246+ // complete the close handshake by disconnecting
247+ closeConnection ( code , reason , true );
272248 } else {
273- if ( DEBUG )
274- System .out .println ( "Ignoring frame:" + f .toString () );
249+ // echo close handshake
250+ close ( code , reason );
251+ closeConnection ( code , reason , false );
275252 }
253+ continue ;
254+ } else if ( curop == Opcode .PING ) {
255+ wsl .onPing ( this , f );
256+ continue ;
257+ } else if ( curop == Opcode .PONG ) {
258+ wsl .onPong ( this , f );
259+ continue ;
276260 } else {
277- currentframe = f ;
278- }
279- } else if ( f .getOpcode () == Opcode .CONTINIOUS ) {
280- try {
281- currentframe .append ( f );
282- } catch ( InvalidFrameException e ) {
283- wsl .onError ( this , e );
284- close ( e );
285- }
286- if ( f .isFin () ) {
287- wsl .onMessage ( this , Charsetfunctions .stringUtf8 ( currentframe .getPayloadData () ) );
288- currentframe = null ;
261+ // process non control frames
262+ if ( currentframe == null ) {
263+ if ( f .getOpcode () == Opcode .CONTINIOUS ) {
264+ throw new InvalidFrameException ( "unexpected continious frame" );
265+ } else if ( f .isFin () ) {
266+ // receive normal onframe message
267+ deliverMessage ( f );
268+ } else {
269+ // remember the frame whose payload is about to be continued
270+ currentframe = f ;
271+ }
272+ } else if ( f .getOpcode () == Opcode .CONTINIOUS ) {
273+ currentframe .append ( f );
274+ if ( f .isFin () ) {
275+ deliverMessage ( currentframe );
276+ currentframe = null ;
277+ }
278+ } else {
279+ throw new InvalidDataException ( CloseFrame .PROTOCOL_ERROR , "non control or continious frame expected" );
280+ }
289281 }
290282 }
283+ } catch ( InvalidDataException e1 ) {
284+ wsl .onError ( this , e1 );
285+ close ( e1 );
286+ return ;
291287 }
292288 }
293289 }
@@ -303,7 +299,7 @@ public void close( int code, String message ) {
303299 try {
304300 closeDirect ( code , message );
305301 } catch ( IOException e ) {
306- closeConnection ( CloseFrame .ABNROMAL_CLOSE ,true );
302+ closeConnection ( CloseFrame .ABNROMAL_CLOSE , true );
307303 }
308304 }
309305
@@ -463,6 +459,17 @@ private void writeDirect( List<ByteBuffer> bufs ) throws IOException {
463459 channelWriteDirect ( b );
464460 }
465461 }
462+ private void deliverMessage ( Framedata d ) throws CharacterCodingException {
463+ if ( d .getOpcode () == Opcode .TEXT ) {
464+ wsl .onMessage ( this , Charsetfunctions .stringUtf8 ( d .getPayloadData () ) );
465+ } else if ( d .getOpcode () == Opcode .BINARY ) {
466+ wsl .onMessage ( this , d .getPayloadData () );
467+ } else {
468+ if ( DEBUG )
469+ System .out .println ( "Ignoring frame:" + d .toString () );
470+ assert ( false );
471+ }
472+ }
466473
467474 private void open () throws IOException {
468475 if ( DEBUG )
0 commit comments