5050 */
5151public class WebSocketImpl extends WebSocket {
5252
53+ public SelectionKey key ;
54+
55+ /* only used to optain the socket addresses*/
56+ public final Socket socket ;
57+ /** the possibly wrapped channel object whose selection is controlled by {@link #key} */
58+ public ByteChannel channel ;
59+ /**
60+ * Queue of buffers that need to be sent to the client.
61+ */
62+ public final BlockingQueue <ByteBuffer > outQueue ;
63+ /**
64+ * Queue of buffers that need to be processed
65+ */
66+ public final BlockingQueue <ByteBuffer > inQueue ;
67+
68+ /**
69+ * Helper variable ment to store the thread which ( exclusively ) triggers this objects decode method.
70+ **/
71+ public volatile WebSocketWorker workerThread ; // TODO reset worker?
72+
73+
5374 /**
5475 * Determines whether to receive data as part of the
5576 * handshake, or as part of text/data frame transmitted over the websocket.
@@ -60,40 +81,31 @@ public class WebSocketImpl extends WebSocket {
6081 */
6182 private volatile boolean closeHandshakeSent = false ;
6283 /**
63- * Determines wheter the connection is open or not
84+ * Determines whether the connection is open or not
6485 */
6586 private volatile boolean connectionClosed = false ;
6687
88+
6789 /**
68- * The listener to notify of WebSocketImpl events.
90+ * The listener to notify of WebSocket events.
6991 */
7092 private final WebSocketListener wsl ;
71- /**
72- * Queue of buffers that need to be sent to the client.
73- */
74- public final BlockingQueue <ByteBuffer > outQueue ;
93+
94+ private List <Draft > knownDrafts ;
7595
7696 private Draft draft = null ;
7797
7898 private Role role ;
7999
80- private Framedata currentframe ;
81-
82- private ClientHandshake handshakerequest = null ;
83-
84- private List <Draft > known_drafts ;
100+ /** used to join continuous frames */
101+ private Framedata tempContiniousFrame ;// FIXME out of mem risk
85102
103+ /** the bytes of an incomplete received handshake */
86104 private ByteBuffer tmpHandshakeBytes ;
87105
88- public final BlockingQueue <ByteBuffer > in ;
89-
90- public volatile WebSocketWorker worker ;
91-
92- public SelectionKey key ;
93-
94- public final Socket socket ;
106+ /** stores the handshake sent by this websocket ( Role.CLIENT only ) */
107+ private ClientHandshake handshakerequest = null ;
95108
96- public ByteChannel ioobject ;
97109
98110 // CONSTRUCTOR /////////////////////////////////////////////////////////////
99111 /**
@@ -111,54 +123,26 @@ public class WebSocketImpl extends WebSocket {
111123 public WebSocketImpl ( WebSocketListener listener , List <Draft > drafts , Socket sock ) {
112124 this ( listener , (Draft ) null , sock );
113125 this .role = Role .SERVER ;
114- if ( known_drafts == null || known_drafts .isEmpty () ) {
115- known_drafts = new ArrayList <Draft >( 1 );
116- known_drafts .add ( new Draft_17 () );
117- known_drafts .add ( new Draft_10 () );
118- known_drafts .add ( new Draft_76 () );
119- known_drafts .add ( new Draft_75 () );
126+ if ( knownDrafts == null || knownDrafts .isEmpty () ) {
127+ knownDrafts = new ArrayList <Draft >( 1 );
128+ knownDrafts .add ( new Draft_17 () );
129+ knownDrafts .add ( new Draft_10 () );
130+ knownDrafts .add ( new Draft_76 () );
131+ knownDrafts .add ( new Draft_75 () );
120132 } else {
121- known_drafts = drafts ;
133+ knownDrafts = drafts ;
122134 }
123135 }
124136
125137 public WebSocketImpl ( WebSocketListener listener , Draft draft , Socket sock ) {
126138 this .outQueue = new LinkedBlockingQueue <ByteBuffer >();
127- in = new LinkedBlockingQueue <ByteBuffer >();
139+ inQueue = new LinkedBlockingQueue <ByteBuffer >();
128140 this .wsl = listener ;
129141 this .role = Role .CLIENT ;
130142 this .draft = draft ;
131143 this .socket = sock ;
132144 }
133145
134- /**
135- * Returns whether the buffer has been filled.
136- *
137- * @throws IOException
138- **/
139- /*public boolean read( final ByteBuffer buf ) throws IOException {
140- buf.clear();
141- int read;
142- if(sockchannel != null) read = sockchannel.read( buf );
143- else read = sockchannel2.read(buf);
144- buf.flip();
145- if( read == -1 ) {
146- if( draft == null ) {
147- closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
148- } else if( draft.getCloseHandshakeType() == CloseHandshakeType.NONE ) {
149- closeConnection( CloseFrame.NORMAL, true );
150- } else if( draft.getCloseHandshakeType() == CloseHandshakeType.ONEWAY ) {
151- if( role == Role.SERVER )
152- closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
153- else
154- closeConnection( CloseFrame.NORMAL, true );
155- } else {
156- closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
157- }
158- return false;
159- }
160- return read != 0;
161- }*/
162146 /**
163147 * Should be called when a Selector has a key that is writable for this
164148 * WebSocketImpl's SocketChannel connection.
@@ -219,7 +203,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException
219203 try {
220204 if ( role == Role .SERVER ) {
221205 if ( draft == null ) {
222- for ( Draft d : known_drafts ) {
206+ for ( Draft d : knownDrafts ) {
223207 try {
224208 d .setParseMode ( role );
225209 socketBuffer .reset ();
@@ -349,21 +333,21 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
349333 continue ;
350334 } else {
351335 // process non control frames
352- if ( currentframe == null ) {
353- if ( f .getOpcode () == Opcode .CONTINIOUS ) {
336+ if ( tempContiniousFrame == null ) {
337+ if ( f .getOpcode () == Opcode .CONTINUOUS ) {
354338 throw new InvalidFrameException ( "unexpected continious frame" );
355339 } else if ( f .isFin () ) {
356340 // receive normal onframe message
357341 deliverMessage ( f );
358342 } else {
359343 // remember the frame whose payload is about to be continued
360- currentframe = f ;
344+ tempContiniousFrame = f ;
361345 }
362- } else if ( f .getOpcode () == Opcode .CONTINIOUS ) {
363- currentframe .append ( f );
346+ } else if ( f .getOpcode () == Opcode .CONTINUOUS ) {
347+ tempContiniousFrame .append ( f );
364348 if ( f .isFin () ) {
365- deliverMessage ( currentframe );
366- currentframe = null ;
349+ deliverMessage ( tempContiniousFrame );
350+ tempContiniousFrame = null ;
367351 }
368352 } else {
369353 throw new InvalidDataException ( CloseFrame .PROTOCOL_ERROR , "non control or continious frame expected" );
@@ -435,7 +419,7 @@ protected synchronized void closeConnection( int code, String message, boolean r
435419 this .wsl .onWebsocketClose ( this , code , message , remote );
436420 if ( draft != null )
437421 draft .reset ();
438- currentframe = null ;
422+ tempContiniousFrame = null ;
439423 handshakerequest = null ;
440424 }
441425
0 commit comments