Skip to content

Commit 774bc89

Browse files
committed
made WebSocket.getLocalSocketAddress and WebSocket.getRemoteSocketAddress more reliable by making them never return null unlike their counterparts Socket.getLocalSocketAddress and Socket.getRemoteSocketAddress (TooTallNate#149)
1 parent 51ec156 commit 774bc89

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/main/java/org/java_websocket/WebSocket.java

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

33
import java.net.InetSocketAddress;
4-
import java.net.Socket;
54
import java.nio.ByteBuffer;
65
import java.nio.channels.NotYetConnectedException;
76

@@ -63,14 +62,12 @@ public enum READYSTATE {
6362
public abstract boolean hasBufferedData();
6463

6564
/**
66-
* @returns null when connections is closed
67-
* @see Socket#getRemoteSocketAddress()
65+
* @returns never returns null
6866
*/
6967
public abstract InetSocketAddress getRemoteSocketAddress();
7068

7169
/**
72-
* @returns null when connections is closed
73-
* @see Socket#getLocalSocketAddress()
70+
* @returns never returns null
7471
*/
7572
public abstract InetSocketAddress getLocalSocketAddress();
7673

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

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

5959
public SelectionKey key;
6060

61-
/* only used to obtain the socket addresses*/
62-
public final Socket socket;
61+
private final InetSocketAddress localSocketAddress;
62+
private final InetSocketAddress remoteSocketAddress;
6363
/** the possibly wrapped channel object whose selection is controlled by {@link #key} */
6464
public ByteChannel channel;
6565
/**
@@ -122,13 +122,22 @@ public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket s
122122
* crates a websocket with client role
123123
*/
124124
public WebSocketImpl( WebSocketListener listener , Draft draft , Socket sock ) {
125+
if( listener == null || sock == null || ( draft == null && role == Role.SERVER ) )
126+
throw new IllegalArgumentException( "parameters must not be null" );
127+
if( !sock.isBound() ) {
128+
throw new IllegalArgumentException( "socket has to be bound" );
129+
}
125130
this.outQueue = new LinkedBlockingQueue<ByteBuffer>();
126131
inQueue = new LinkedBlockingQueue<ByteBuffer>();
127132
this.wsl = listener;
128133
this.role = Role.CLIENT;
129134
if( draft != null )
130135
this.draft = draft.copyInstance();
131-
this.socket = sock;
136+
137+
localSocketAddress = (InetSocketAddress) sock.getLocalSocketAddress();
138+
remoteSocketAddress = (InetSocketAddress) sock.getRemoteSocketAddress();
139+
assert ( localSocketAddress != null );
140+
assert ( remoteSocketAddress != null );
132141
}
133142

134143
/**
@@ -682,12 +691,12 @@ public String toString() {
682691

683692
@Override
684693
public InetSocketAddress getRemoteSocketAddress() {
685-
return (InetSocketAddress) socket.getRemoteSocketAddress();
694+
return remoteSocketAddress;
686695
}
687696

688697
@Override
689698
public InetSocketAddress getLocalSocketAddress() {
690-
return (InetSocketAddress) socket.getLocalSocketAddress();
699+
return localSocketAddress;
691700
}
692701

693702
@Override

0 commit comments

Comments
 (0)