Skip to content

Commit

Permalink
Allow setting custom TCP receive buffer size
Browse files Browse the repository at this point in the history
Also increase default to 64K for performance improvement.
  • Loading branch information
PhilipRoman committed Apr 22, 2024
1 parent aa84b39 commit 84545ec
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Expand Up @@ -102,6 +102,13 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
*/
private final Object syncConnectionLost = new Object();

/**
* TCP receive buffer size that will be used for sockets
*
* @since 1.5.7
*/
private int receiveBufferSize = 65536;

/**
* Get the interval checking for lost connections Default is 60 seconds
*
Expand Down Expand Up @@ -336,4 +343,25 @@ public boolean isDaemon() {
public void setDaemon(boolean daemon) {
this.daemon = daemon;
}

/**
* Returns the TCP receive buffer size that will be used for sockets.
* @see java.net.Socket#setReceiveBufferSize(int)
*
* @since 1.5.7
*/
public int getReceiveBufferSize() {
return receiveBufferSize;
}

/**
* Sets the TCP receive buffer size that will be used for sockets.
* @see java.net.Socket#setReceiveBufferSize(int)
*
* @since 1.5.7
*/
public void setReceiveBufferSize(int receiveBufferSize) {
this.receiveBufferSize = receiveBufferSize;
}

}
5 changes: 0 additions & 5 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Expand Up @@ -85,11 +85,6 @@ public class WebSocketImpl implements WebSocket {
*/
public static final int DEFAULT_WSS_PORT = 443;

/**
* Initial buffer size
*/
public static final int RCVBUF = 16384;

/**
* Logger instance
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -481,6 +481,7 @@ public void run() {

socket.setTcpNoDelay(isTcpNoDelay());
socket.setReuseAddress(isReuseAddr());
socket.setReceiveBufferSize(getReceiveBufferSize());

if (!socket.isConnected()) {
InetSocketAddress addr = dnsResolver == null ? InetSocketAddress.createUnresolved(uri.getHost(), getPort()) : new InetSocketAddress(dnsResolver.resolve(uri), this.getPort());
Expand Down Expand Up @@ -531,7 +532,7 @@ public void run() {
writeThread.setDaemon(isDaemon());
writeThread.start();

byte[] rawbuffer = new byte[WebSocketImpl.RCVBUF];
byte[] rawbuffer = new byte[getReceiveBufferSize()];
int readBytes;

try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Expand Up @@ -578,7 +578,7 @@ private boolean doSetupSelectorAndServerThread() {
server = ServerSocketChannel.open();
server.configureBlocking(false);
ServerSocket socket = server.socket();
socket.setReceiveBufferSize(WebSocketImpl.RCVBUF);
socket.setReceiveBufferSize(getReceiveBufferSize());
socket.setReuseAddress(isReuseAddr());
socket.bind(address, getMaxPendingConnections());
selector = Selector.open();
Expand Down Expand Up @@ -655,7 +655,7 @@ protected void releaseBuffers(WebSocket c) throws InterruptedException {
}

public ByteBuffer createBuffer() {
return ByteBuffer.allocate(WebSocketImpl.RCVBUF);
return ByteBuffer.allocate(getReceiveBufferSize());
}

protected void queue(WebSocketImpl ws) throws InterruptedException {
Expand Down

0 comments on commit 84545ec

Please sign in to comment.