Skip to content

Commit 2ee3196

Browse files
authored
Merge pull request TooTallNate#462 from marci4/master
Make TCP_NODELAY accessible
2 parents 3816d68 + 168637e commit 2ee3196

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
5858

5959
private int connectTimeout = 0;
6060

61+
/**
62+
* Attribute which allows you to deactivate the Nagle's algorithm
63+
*/
64+
private boolean tcpNoDelay;
65+
6166
/**
6267
* Constructs a WebSocketClient instance and sets it to the connect to the
6368
* specified URI. The channel does not attampt to connect automatically. The connection
@@ -99,6 +104,7 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
99104
this.draft = protocolDraft;
100105
this.headers = httpHeaders;
101106
this.connectTimeout = connectTimeout;
107+
this.tcpNoDelay = false;
102108
this.engine = new WebSocketImpl( this, protocolDraft );
103109
}
104110

@@ -127,6 +133,24 @@ public Socket getSocket() {
127133
return socket;
128134
}
129135

136+
/**
137+
* Tests if TCP_NODELAY is enabled.
138+
* @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections.
139+
*/
140+
public boolean isTcpNoDelay() {
141+
return tcpNoDelay;
142+
}
143+
144+
/**
145+
* Setter for tcpNoDelay
146+
*
147+
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections
148+
* @param tcpNoDelay true to enable TCP_NODELAY, false to disable.
149+
*/
150+
public void setTcpNoDelay( boolean tcpNoDelay ) {
151+
this.tcpNoDelay = tcpNoDelay;
152+
}
153+
130154
/**
131155
* Initiates the websocket connection. This method does not block.
132156
*/
@@ -193,6 +217,7 @@ public void run() {
193217
} else if( socket.isClosed() ) {
194218
throw new IOException();
195219
}
220+
socket.setTcpNoDelay( tcpNoDelay );
196221
if( !socket.isBound() )
197222
socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
198223
istream = socket.getInputStream();

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
8888

8989
private WebSocketServerFactory wsf = new DefaultWebSocketServerFactory();
9090

91+
/**
92+
* Attribute which allows you to deactivate the Nagle's algorithm
93+
*/
94+
private boolean tcpNoDelay;
95+
9196
/**
9297
* Creates a WebSocketServer that will attempt to
9398
* listen on port <var>WebSocket.DEFAULT_PORT</var>.
@@ -181,7 +186,7 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf
181186

182187
this.address = address;
183188
this.connections = connectionscontainer;
184-
189+
tcpNoDelay = false;
185190
iqueue = new LinkedList<WebSocketImpl>();
186191

187192
decoders = new ArrayList<WebSocketWorker>( decodercount );
@@ -193,6 +198,25 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf
193198
}
194199
}
195200

201+
/**
202+
* Tests if TCP_NODELAY is enabled.
203+
* @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections.
204+
*/
205+
public boolean isTcpNoDelay() {
206+
return tcpNoDelay;
207+
}
208+
209+
/**
210+
* Setter for tcpNoDelay
211+
*
212+
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections
213+
* @param tcpNoDelay true to enable TCP_NODELAY, false to disable.
214+
*/
215+
public void setTcpNoDelay( boolean tcpNoDelay ) {
216+
this.tcpNoDelay = tcpNoDelay;
217+
}
218+
219+
196220
/**
197221
* Starts the server selectorthread that binds to the currently set port number and
198222
* listeners for WebSocket connection requests. Creates a fixed thread pool with the size {@link WebSocketServer#DECODERS}<br>
@@ -335,7 +359,9 @@ public void run() {
335359
continue;
336360
}
337361
channel.configureBlocking( false );
338-
WebSocketImpl w = wsf.createWebSocket( this, drafts, channel.socket() );
362+
Socket socket = channel.socket();
363+
socket.setTcpNoDelay( tcpNoDelay );
364+
WebSocketImpl w = wsf.createWebSocket( this, drafts, socket );
339365
w.key = channel.register( selector, SelectionKey.OP_READ, w );
340366
try {
341367
w.channel = wsf.wrapChannel( channel, w.key );

0 commit comments

Comments
 (0)