Skip to content

Commit 9e762ad

Browse files
committed
the WebSocketFactory's channel wrapping method now takes a (Socketchannel's) SelectionKey instead of the channel itself
1 parent aa8ed15 commit 9e762ad

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/org/java_websocket/WebSocketFactory.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
import java.io.IOException;
44
import java.net.Socket;
55
import java.nio.channels.ByteChannel;
6-
import java.nio.channels.SocketChannel;
6+
import java.nio.channels.SelectionKey;
77
import java.util.List;
88

99
import org.java_websocket.drafts.Draft;
1010

1111
public interface WebSocketFactory {
1212
public WebSocket createWebSocket( WebSocketAdapter a, Draft d, Socket s );
1313
public WebSocket createWebSocket( WebSocketAdapter a, List<Draft> drafts, Socket s );
14-
public ByteChannel wrapChannel( SocketChannel c ) throws IOException;
14+
/**
15+
* Allows to wrap the Socketchannel( key.channel() ) to insert a protocol layer( like ssl or proxy authentication) beyond the ws layer.
16+
*
17+
* @param key
18+
* a SelectionKey of an open SocketChannel.
19+
* @return The channel on which the read and write operations will be performed.<br>
20+
*/
21+
public ByteChannel wrapChannel( SelectionKey key ) throws IOException;
1522
}

src/org/java_websocket/client/WebSocketClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public WebSocket createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s )
8383
}
8484

8585
@Override
86-
public SocketChannel wrapChannel( SocketChannel c ) {
87-
return c;
86+
public ByteChannel wrapChannel( SelectionKey c ) {
87+
return (ByteChannel) c.channel();
8888
}
8989
};
9090

@@ -247,7 +247,7 @@ private final void interruptableRun() {
247247
}
248248
if( key.isConnectable() ) {
249249
try {
250-
finishConnect();
250+
finishConnect( key );
251251
} catch ( InterruptedException e ) {
252252
conn.close( CloseFrame.NEVERCONNECTED );// report error to only
253253
break;
@@ -272,13 +272,13 @@ private int getPort() {
272272
return port == -1 ? WebSocket.DEFAULT_PORT : port;
273273
}
274274

275-
private void finishConnect() throws IOException , InvalidHandshakeException , InterruptedException {
275+
private void finishConnect( SelectionKey key ) throws IOException , InvalidHandshakeException , InterruptedException {
276276
if( channel.isConnectionPending() ) {
277277
channel.finishConnect();
278278
}
279-
wrappedchannel = wf.wrapChannel( channel );
279+
wrappedchannel = wf.wrapChannel( key );
280280
// Now that we're connected, re-register for only 'READ' keys.
281-
channel.register( selector, SelectionKey.OP_READ );
281+
key.interestOps( SelectionKey.OP_READ );
282282

283283
sendHandshake();
284284
}

src/org/java_websocket/server/WebSocketServer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket
8686
}
8787

8888
@Override
89-
public SocketChannel wrapChannel( SocketChannel c ) {
90-
return c;
89+
public SocketChannel wrapChannel( SelectionKey c ) {
90+
return (SocketChannel) c.channel();
9191
}
9292
};
9393

@@ -262,11 +262,11 @@ public void run() {
262262
if( key.isAcceptable() ) {
263263
SocketChannel channel = server.accept();
264264
channel.configureBlocking( false );
265-
WebSocketImpl c = wsf.createWebSocket( this, drafts, channel.socket() );
266-
c.key = channel.register( selector, SelectionKey.OP_READ, c );
267-
c.channel = wsf.wrapChannel( channel );
265+
WebSocketImpl w = wsf.createWebSocket( this, drafts, channel.socket() );
266+
w.key = channel.register( selector, SelectionKey.OP_READ, w );
267+
w.channel = wsf.wrapChannel( w.key );
268268
i.remove();
269-
allocateBuffers( c );
269+
allocateBuffers( w );
270270
continue;
271271
}
272272

@@ -293,7 +293,7 @@ public void run() {
293293
conn = (WebSocketImpl) key.attachment();
294294
if( SocketChannelIOHelper.batch( conn, (ByteChannel) conn.channel ) ) {
295295
if( key.isValid() )
296-
key.channel().register( selector, SelectionKey.OP_READ, key.attachment() );
296+
key.interestOps( SelectionKey.OP_READ );
297297
}
298298
}
299299
}

0 commit comments

Comments
 (0)