Skip to content

Commit a0ebef9

Browse files
committed
did a little refactoring to get rid of WebSocketImpl.socket and because of TooTallNate#159
1 parent f5791ec commit a0ebef9

File tree

6 files changed

+73
-32
lines changed

6 files changed

+73
-32
lines changed

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ public class WebSocketImpl implements WebSocket {
5858

5959
public SelectionKey key;
6060

61-
private final Socket socket;
62-
6361
/** the possibly wrapped channel object whose selection is controlled by {@link #key} */
6462
public ByteChannel channel;
6563
/**
@@ -107,8 +105,8 @@ public class WebSocketImpl implements WebSocket {
107105
/**
108106
* crates a websocket with server role
109107
*/
110-
public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket sock ) {
111-
this( listener, (Draft) null, sock );
108+
public WebSocketImpl( WebSocketListener listener , List<Draft> drafts ) {
109+
this( listener, (Draft) null );
112110
this.role = Role.SERVER;
113111
// draft.copyInstance will be called when the draft is first needed
114112
if( drafts == null || drafts.isEmpty() ) {
@@ -121,20 +119,28 @@ public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket s
121119
/**
122120
* crates a websocket with client role
123121
*
124-
* @param sock
122+
* @param socket
125123
* may be unbound
126124
*/
127-
public WebSocketImpl( WebSocketListener listener , Draft draft , Socket sock ) {
128-
if( listener == null || sock == null || ( draft == null && role == Role.SERVER ) )
125+
public WebSocketImpl( WebSocketListener listener , Draft draft ) {
126+
if( listener == null || ( draft == null && role == Role.SERVER ) )// socket can be null because we want do be able to create the object without already having a bound channel
129127
throw new IllegalArgumentException( "parameters must not be null" );
130128
this.outQueue = new LinkedBlockingQueue<ByteBuffer>();
131129
inQueue = new LinkedBlockingQueue<ByteBuffer>();
132130
this.wsl = listener;
133131
this.role = Role.CLIENT;
134132
if( draft != null )
135133
this.draft = draft.copyInstance();
134+
}
135+
136+
@Deprecated
137+
public WebSocketImpl( WebSocketListener listener , Draft draft , Socket socket ) {
138+
this( listener, draft );
139+
}
136140

137-
socket = sock;
141+
@Deprecated
142+
public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket socket ) {
143+
this( listener, drafts );
138144
}
139145

140146
/**
@@ -688,12 +694,12 @@ public String toString() {
688694

689695
@Override
690696
public InetSocketAddress getRemoteSocketAddress() {
691-
return (InetSocketAddress) socket.getRemoteSocketAddress();
697+
return wsl.getRemoteSocketAddress( this );
692698
}
693699

694700
@Override
695701
public InetSocketAddress getLocalSocketAddress() {
696-
return (InetSocketAddress) socket.getLocalSocketAddress();
702+
return wsl.getLocalSocketAddress( this );
697703
}
698704

699705
@Override

src/main/java/org/java_websocket/WebSocketListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.java_websocket;
22

3+
import java.net.InetSocketAddress;
34
import java.nio.ByteBuffer;
45

56
import org.java_websocket.drafts.Draft;
@@ -143,4 +144,7 @@ public interface WebSocketListener {
143144

144145
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
145146
public void onWriteDemand( WebSocket conn );
147+
148+
public InetSocketAddress getLocalSocketAddress( WebSocket conn );
149+
public InetSocketAddress getRemoteSocketAddress( WebSocket conn );
146150
}

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
7070

7171
private int timeout = 0;
7272

73-
WebSocketClientFactory wf = new WebSocketClientFactory() {
73+
WebSocketClientFactory wsfactory = new WebSocketClientFactory() {
7474
@Override
7575
public WebSocket createWebSocket( WebSocketAdapter a, Draft d, Socket s ) {
76-
return new WebSocketImpl( WebSocketClient.this, d, s );
76+
return new WebSocketImpl( WebSocketClient.this, d );
7777
}
7878

7979
@Override
8080
public WebSocket createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
81-
return new WebSocketImpl( WebSocketClient.this, d, s );
81+
return new WebSocketImpl( WebSocketClient.this, d );
8282
}
8383

8484
@Override
@@ -117,16 +117,18 @@ public WebSocketClient( URI serverUri , Draft draft , Map<String,String> headers
117117
try {
118118
channel = SelectorProvider.provider().openSocketChannel();
119119
channel.configureBlocking( true );
120-
conn = (WebSocketImpl) wf.createWebSocket( this, draft, channel.socket() );
121120
} catch ( IOException e ) {
121+
channel = null;
122122
onWebsocketError( null, e );
123-
124-
if(conn != null) {
125-
conn.closeConnection( CloseFrame.NEVER_CONNECTED, e.getMessage() );
126-
}
127-
128-
return;
129123
}
124+
if(channel == null){
125+
conn = (WebSocketImpl) wsfactory.createWebSocket( this, draft, null );
126+
conn.close( CloseFrame.NEVER_CONNECTED, "Failed to create or configure SocketChannel." );
127+
}
128+
else{
129+
conn = (WebSocketImpl) wsfactory.createWebSocket( this, draft, channel.socket() );
130+
}
131+
130132
}
131133

132134
/**
@@ -213,11 +215,15 @@ public void run() {
213215
}
214216

215217
private final void interruptableRun() {
218+
if( channel == null ) {
219+
return;// channel will be initialized in the constructor and only be null if no socket channel could be created or if blocking mode could be established
220+
}
221+
216222
try {
217223
String host = uri.getHost();
218224
int port = getPort();
219225
tryToConnect( new InetSocketAddress( host, port ) );
220-
conn.channel = wrappedchannel = wf.wrapChannel( channel, null, host, port );
226+
conn.channel = wrappedchannel = wsfactory.wrapChannel( channel, null, host, port );
221227
timeout = 0; // since connect is over
222228
sendHandshake();
223229
readthread = new Thread( new WebsocketWriteThread() );
@@ -227,11 +233,7 @@ private final void interruptableRun() {
227233
return;
228234
} catch ( /*IOException | SecurityException | UnresolvedAddressException*/Exception e ) {//
229235
onWebsocketError( conn, e );
230-
231-
if(conn != null) {
232-
conn.closeConnection( CloseFrame.NEVER_CONNECTED, e.getMessage() );
233-
}
234-
236+
conn.closeConnection( CloseFrame.NEVER_CONNECTED, e.getMessage() );
235237
return;
236238
}
237239

@@ -389,11 +391,25 @@ public WebSocket getConnection() {
389391
}
390392

391393
public final void setWebSocketFactory( WebSocketClientFactory wsf ) {
392-
this.wf = wsf;
394+
this.wsfactory = wsf;
393395
}
394396

395397
public final WebSocketFactory getWebSocketFactory() {
396-
return wf;
398+
return wsfactory;
399+
}
400+
401+
@Override
402+
public InetSocketAddress getLocalSocketAddress( WebSocket conn ) {
403+
if( channel != null )
404+
return (InetSocketAddress) channel.socket().getLocalSocketAddress();
405+
return null;
406+
}
407+
408+
@Override
409+
public InetSocketAddress getRemoteSocketAddress( WebSocket conn ) {
410+
if( channel != null )
411+
return (InetSocketAddress) channel.socket().getLocalSocketAddress();
412+
return null;
397413
}
398414

399415
// ABTRACT METHODS /////////////////////////////////////////////////////////

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public ByteChannel wrapChannel( SocketChannel channel, SelectionKey key ) throws
4141

4242
@Override
4343
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) {
44-
return new WebSocketImpl( a, d, c );
44+
return new WebSocketImpl( a, d );
4545
}
4646

4747
@Override
4848
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
49-
return new WebSocketImpl( a, d, s );
49+
return new WebSocketImpl( a, d );
5050
}
5151
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
public class DefaultWebSocketServerFactory implements WebSocketServerFactory {
1414
@Override
1515
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket s ) {
16-
return new WebSocketImpl( a, d, s );
16+
return new WebSocketImpl( a, d );
1717
}
1818
@Override
1919
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
20-
return new WebSocketImpl( a, d, s );
20+
return new WebSocketImpl( a, d );
2121
}
2222
@Override
2323
public SocketChannel wrapChannel( SocketChannel channel, SelectionKey key ) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,21 @@ protected boolean onConnect( SelectionKey key ) {
557557
return true;
558558
}
559559

560+
private Socket getSocket( WebSocket conn ) {
561+
WebSocketImpl impl = (WebSocketImpl) conn;
562+
return ( (SocketChannel) impl.key.channel() ).socket();
563+
}
564+
565+
@Override
566+
public InetSocketAddress getLocalSocketAddress( WebSocket conn ) {
567+
return (InetSocketAddress) getSocket( conn ).getLocalSocketAddress();
568+
}
569+
570+
@Override
571+
public InetSocketAddress getRemoteSocketAddress( WebSocket conn ) {
572+
return (InetSocketAddress) getSocket( conn ).getLocalSocketAddress();
573+
}
574+
560575
/** Called after an opening handshake has been performed and the given websocket is ready to be written on. */
561576
public abstract void onOpen( WebSocket conn, ClientHandshake handshake );
562577
/**

0 commit comments

Comments
 (0)