@@ -194,8 +194,8 @@ public void run() {
194194 while ( i .hasNext () ) {
195195 key = i .next ();
196196
197- i .remove ();
198197 if ( !key .isValid () ) {
198+ // Object o = key.attachment();
199199 continue ;
200200 }
201201
@@ -204,38 +204,14 @@ public void run() {
204204 client .configureBlocking ( false );
205205 WebSocket c = new WebSocket ( this , Collections .singletonList ( draft ), client );
206206 client .register ( selector , SelectionKey .OP_READ , c );
207- }
208-
209- // if isReadable == true
210- // then the server is ready to read
211- if ( key .isReadable () ) {
212- conn = (WebSocket ) key .attachment ();
213- asyncQueueRead ( conn );
214- // conn.handleRead();
215- }
216-
217- // if isWritable == true
218- // then we need to send the rest of the data to the client
219- /*if( key.isValid() && key.isWritable() ) {
207+ i .remove ();
208+ } else if ( key .isReadable () ) {
220209 conn = (WebSocket ) key .attachment ();
221- conn.flush();
222- key.channel().register( selector, SelectionKey.OP_READ, conn );
223- }*/
224- }
225- /*synchronized ( connections ) {
226- Iterator<WebSocket> it = this.connections.iterator();
227- while ( it.hasNext() ) {
228- // We have to do this check here, and not in the selectorthread that
229- // adds the buffered data to the WebSocket, because the
230- // Selector is not selectorthread-safe, and can only be accessed
231- // by this selectorthread.
232- conn = it.next();
233- if( conn.hasBufferedData() ) {
234- conn.flush();
235- // key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
210+ if ( asyncQueueRead ( conn ) ) {
211+ i .remove ();
236212 }
237213 }
238- }*/
214+ }
239215 } catch ( IOException ex ) {
240216 if ( key != null )
241217 key .cancel ();
@@ -244,13 +220,7 @@ public void run() {
244220 }
245221 } catch ( RuntimeException e ) {
246222 // should hopefully never occur
247- onError ( null , e );
248- try {
249- selector .close ();
250- } catch ( IOException e1 ) {
251- onError ( null , e1 );
252- }
253- decoders .shutdown ();
223+ handleFatal ( null , e );
254224 }
255225 }
256226
@@ -261,6 +231,16 @@ private void handleIOException( WebSocket conn, IOException ex ) {
261231 }
262232 }
263233
234+ private void handleFatal ( WebSocket conn , RuntimeException e ) {
235+ onError ( conn , e );
236+ try {
237+ selector .close ();
238+ } catch ( IOException e1 ) {
239+ onError ( null , e1 );
240+ }
241+ decoders .shutdown ();
242+ }
243+
264244 /**
265245 * Gets the XML string that should be returned if a client requests a Flash
266246 * security policy.
@@ -361,8 +341,9 @@ public Boolean call() throws Exception {
361341 try {
362342 ws .handleRead ();
363343 } catch ( IOException e ) {
364- e .printStackTrace ();
365- return false ;
344+ handleIOException ( ws , e );
345+ } catch ( RuntimeException e ) {
346+ handleFatal ( ws , e );
366347 } finally {
367348 synchronized ( active_websocktes ) {
368349 active_websocktes .remove ( ws );
0 commit comments