99import java .nio .channels .ServerSocketChannel ;
1010import java .nio .channels .SocketChannel ;
1111import java .util .Collections ;
12+ import java .util .HashSet ;
1213import java .util .Iterator ;
1314import java .util .Set ;
14- import java .util .concurrent .CopyOnWriteArraySet ;
1515
1616import org .java_websocket .drafts .Draft ;
1717import org .java_websocket .framing .CloseFrame ;
2323 * HTTP handshake portion of WebSockets. It's up to a subclass to add
2424 * functionality/purpose to the server.
2525 *
26- * @author Nathan Rajlich
2726 */
2827public abstract class WebSocketServer extends WebSocketAdapter implements Runnable {
2928
@@ -32,7 +31,7 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
3231 * Holds the list of active WebSocket connections. "Active" means WebSocket
3332 * handshake is complete and socket can be written to, or read from.
3433 */
35- private final CopyOnWriteArraySet <WebSocket > connections ;
34+ private final Set <WebSocket > connections = new HashSet < WebSocket >() ;
3635 /**
3736 * The port number that this WebSocket server should listen on. Default is
3837 * WebSocket.DEFAULT_PORT.
@@ -83,7 +82,6 @@ public WebSocketServer( InetSocketAddress address ) {
8382 * instance should comply to.
8483 */
8584 public WebSocketServer ( InetSocketAddress address , Draft draft ) {
86- this .connections = new CopyOnWriteArraySet <WebSocket >();
8785 this .draft = draft ;
8886 setAddress ( address );
8987 }
@@ -109,8 +107,10 @@ public void start() {
109107 * When socket related I/O errors occur.
110108 */
111109 public void stop () throws IOException {
112- for ( WebSocket ws : connections ) {
113- ws .close ( CloseFrame .NORMAL );
110+ synchronized ( connections ) {
111+ for ( WebSocket ws : connections ) {
112+ ws .close ( CloseFrame .NORMAL );
113+ }
114114 }
115115 thread .interrupt ();
116116 this .server .close ();
@@ -119,11 +119,13 @@ public void stop() throws IOException {
119119
120120 /**
121121 * Returns a WebSocket[] of currently connected clients.
122+ * Its iterators will be failfast and its not judicious
123+ * to modify it.
122124 *
123- * @return The currently connected clients in a WebSocket[] .
125+ * @return The currently connected clients.
124126 */
125127 public Set <WebSocket > connections () {
126- return Collections . unmodifiableSet ( this .connections ) ;
128+ return this .connections ;
127129 }
128130
129131 /**
@@ -208,16 +210,18 @@ public void run() {
208210 key .channel ().register ( selector , SelectionKey .OP_READ , conn );
209211 }
210212 }
211- Iterator <WebSocket > it = this .connections .iterator ();
212- while ( it .hasNext () ) {
213- // We have to do this check here, and not in the thread that
214- // adds the buffered data to the WebSocket, because the
215- // Selector is not thread-safe, and can only be accessed
216- // by this thread.
217- conn = it .next ();
218- if ( conn .hasBufferedData () ) {
219- conn .flush ();
220- // key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
213+ synchronized ( connections ) {
214+ Iterator <WebSocket > it = this .connections .iterator ();
215+ while ( it .hasNext () ) {
216+ // We have to do this check here, and not in the thread that
217+ // adds the buffered data to the WebSocket, because the
218+ // Selector is not thread-safe, and can only be accessed
219+ // by this thread.
220+ conn = it .next ();
221+ if ( conn .hasBufferedData () ) {
222+ conn .flush ();
223+ // key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
224+ }
221225 }
222226 }
223227 } catch ( IOException ex ) {
@@ -260,15 +264,19 @@ public final void onWebsocketMessage( WebSocket conn, byte[] blob ) {
260264
261265 @ Override
262266 public final void onWebsocketOpen ( WebSocket conn , Handshakedata handshake ) {
263- if ( this .connections .add ( conn ) ) {
264- onOpen ( conn , (ClientHandshake ) handshake );
267+ synchronized ( connections ) {
268+ if ( this .connections .add ( conn ) ) {
269+ onOpen ( conn , (ClientHandshake ) handshake );
270+ }
265271 }
266272 }
267273
268274 @ Override
269275 public final void onWebsocketClose ( WebSocket conn , int code , String reason , boolean remote ) {
270- if ( this .connections .remove ( conn ) ) {
271- onClose ( conn , code , reason , remote );
276+ synchronized ( connections ) {
277+ if ( this .connections .remove ( conn ) ) {
278+ onClose ( conn , code , reason , remote );
279+ }
272280 }
273281 }
274282
0 commit comments