Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TCP_NODELAY accessible #462

Merged
merged 1 commit into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab

private int connectTimeout = 0;

/**
* Attribute which allows you to deactivate the Nagle's algorithm
*/
private boolean tcpNoDelay;

/**
* Constructs a WebSocketClient instance and sets it to the connect to the
* specified URI. The channel does not attampt to connect automatically. The connection
Expand Down Expand Up @@ -99,6 +104,7 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
this.draft = protocolDraft;
this.headers = httpHeaders;
this.connectTimeout = connectTimeout;
this.tcpNoDelay = false;
this.engine = new WebSocketImpl( this, protocolDraft );
}

Expand Down Expand Up @@ -127,6 +133,24 @@ public Socket getSocket() {
return socket;
}

/**
* Tests if TCP_NODELAY is enabled.
* @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections.
*/
public boolean isTcpNoDelay() {
return tcpNoDelay;
}

/**
* Setter for tcpNoDelay
*
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections
* @param tcpNoDelay true to enable TCP_NODELAY, false to disable.
*/
public void setTcpNoDelay( boolean tcpNoDelay ) {
this.tcpNoDelay = tcpNoDelay;
}

/**
* Initiates the websocket connection. This method does not block.
*/
Expand Down Expand Up @@ -193,6 +217,7 @@ public void run() {
} else if( socket.isClosed() ) {
throw new IOException();
}
socket.setTcpNoDelay( tcpNoDelay );
if( !socket.isBound() )
socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
istream = socket.getInputStream();
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab

private WebSocketServerFactory wsf = new DefaultWebSocketServerFactory();

/**
* Attribute which allows you to deactivate the Nagle's algorithm
*/
private boolean tcpNoDelay;

/**
* Creates a WebSocketServer that will attempt to
* listen on port <var>WebSocket.DEFAULT_PORT</var>.
Expand Down Expand Up @@ -181,7 +186,7 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf

this.address = address;
this.connections = connectionscontainer;

tcpNoDelay = false;
iqueue = new LinkedList<WebSocketImpl>();

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

/**
* Tests if TCP_NODELAY is enabled.
* @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections.
*/
public boolean isTcpNoDelay() {
return tcpNoDelay;
}

/**
* Setter for tcpNoDelay
*
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections
* @param tcpNoDelay true to enable TCP_NODELAY, false to disable.
*/
public void setTcpNoDelay( boolean tcpNoDelay ) {
this.tcpNoDelay = tcpNoDelay;
}


/**
* Starts the server selectorthread that binds to the currently set port number and
* listeners for WebSocket connection requests. Creates a fixed thread pool with the size {@link WebSocketServer#DECODERS}<br>
Expand Down Expand Up @@ -335,7 +359,9 @@ public void run() {
continue;
}
channel.configureBlocking( false );
WebSocketImpl w = wsf.createWebSocket( this, drafts, channel.socket() );
Socket socket = channel.socket();
socket.setTcpNoDelay( tcpNoDelay );
WebSocketImpl w = wsf.createWebSocket( this, drafts, socket );
w.key = channel.register( selector, SelectionKey.OP_READ, w );
try {
w.channel = wsf.wrapChannel( channel, w.key );
Expand Down