Skip to content

Commit 0b4964f

Browse files
committed
made WebSocketServer::stop also terminate its worker threads & a few minor changes (TooTallNate#129)
1 parent 5c453ef commit 0b4964f

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,15 @@ public void connect() {
140140
}
141141

142142
public void close() {
143-
if( thread != null && conn != null ) {
143+
if( thread != null ) {
144144
conn.close( CloseFrame.NORMAL );
145+
/*closelock.lock();
146+
try {
147+
if( selector != null )
148+
selector.wakeup();
149+
} finally {
150+
closelock.unlock();
151+
}*/
145152
}
146153

147154
}
@@ -383,7 +390,7 @@ public final void onWriteDemand( WebSocket conn ) {
383390
key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
384391
selector.wakeup();
385392
} catch ( CancelledKeyException e ) {
386-
// since such an exception/event will also occur on the selector there is no need to do anything herec
393+
// since such an exception/event will also occur on the selector there is no need to do anything here
387394
}
388395
}
389396

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf
161161
*/
162162
public void start() {
163163
if( selectorthread != null )
164-
throw new IllegalStateException( "Already started" );
165-
new Thread( this ).start();
164+
throw new IllegalStateException( getClass().getName() + " can only be started once." );
165+
new Thread( this ).start();;
166166
}
167167

168168
/**
@@ -171,15 +171,20 @@ public void start() {
171171
* freeing the port the server was bound to.
172172
*
173173
* @throws IOException
174-
* When socket related I/O errors occur.
174+
* When {@link ServerSocketChannel}.close throws an IOException
175+
* @throws InterruptedException
175176
*/
176-
public void stop() throws IOException {
177+
public void stop() throws IOException , InterruptedException {
177178
synchronized ( connections ) {
178179
for( WebSocket ws : connections ) {
179180
ws.close( CloseFrame.NORMAL );
180181
}
181182
}
182183
selectorthread.interrupt();
184+
selectorthread.join();
185+
for( WebSocketWorker w : decoders ) {
186+
w.interrupt();
187+
}
183188
this.server.close();
184189

185190
}
@@ -228,9 +233,11 @@ public List<Draft> getDraft() {
228233

229234
// Runnable IMPLEMENTATION /////////////////////////////////////////////////
230235
public void run() {
231-
if( selectorthread != null )
232-
throw new IllegalStateException( "This instance of " + getClass().getSimpleName() + " can only be started once the same time." );
233-
selectorthread = Thread.currentThread();
236+
synchronized ( this ) {
237+
if( selectorthread != null )
238+
throw new IllegalStateException( getClass().getName() + " can only be started once." );
239+
selectorthread = Thread.currentThread();
240+
}
234241
selectorthread.setName( "WebsocketSelector" + selectorthread.getId() );
235242
try {
236243
server = ServerSocketChannel.open();
@@ -388,12 +395,11 @@ private void handleIOException( WebSocket conn, IOException ex ) {
388395
private void handleFatal( WebSocket conn, RuntimeException e ) {
389396
onError( conn, e );
390397
try {
391-
selector.close();
398+
stop();
392399
} catch ( IOException e1 ) {
393400
onError( null, e1 );
394-
}
395-
for( WebSocketWorker w : decoders ) {
396-
w.interrupt();
401+
} catch ( InterruptedException e1 ) {
402+
onError( null, e1 );
397403
}
398404
}
399405

0 commit comments

Comments
 (0)