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

Allow setting custom TCP receive buffer size #1407

Merged
merged 1 commit into from
Apr 27, 2024
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
37 changes: 37 additions & 0 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
*/
private final Object syncConnectionLost = new Object();

/**
* TCP receive buffer size that will be used for sockets (zero means use system default)
*
* @since 1.5.7
*/
private int receiveBufferSize = 0;

/**
* Used for internal buffer allocations when the socket buffer size is not specified.
*/
protected static int DEFAULT_READ_BUFFER_SIZE = 65536;

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

/**
* Returns the TCP receive buffer size that will be used for sockets (or zero, if not explicitly set).
* @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.
* If this is not explicitly set (or set to zero), the system default is used.
* @see java.net.Socket#setReceiveBufferSize(int)
*
* @since 1.5.7
*/
public void setReceiveBufferSize(int receiveBufferSize) {
if (receiveBufferSize < 0) {
throw new IllegalArgumentException("buffer size < 0");
}
this.receiveBufferSize = receiveBufferSize;
}

}
5 changes: 0 additions & 5 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
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
7 changes: 6 additions & 1 deletion src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ public void run() {

socket.setTcpNoDelay(isTcpNoDelay());
socket.setReuseAddress(isReuseAddr());
int receiveBufferSize = getReceiveBufferSize();
if (receiveBufferSize > 0) {
socket.setReceiveBufferSize(receiveBufferSize);
}

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 +535,8 @@ public void run() {
writeThread.setDaemon(isDaemon());
writeThread.start();

byte[] rawbuffer = new byte[WebSocketImpl.RCVBUF];
int receiveBufferSize = getReceiveBufferSize();
byte[] rawbuffer = new byte[receiveBufferSize > 0 ? receiveBufferSize : DEFAULT_READ_BUFFER_SIZE];
int readBytes;

try {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ private boolean doSetupSelectorAndServerThread() {
server = ServerSocketChannel.open();
server.configureBlocking(false);
ServerSocket socket = server.socket();
socket.setReceiveBufferSize(WebSocketImpl.RCVBUF);
int receiveBufferSize = getReceiveBufferSize();
if (receiveBufferSize > 0) {
socket.setReceiveBufferSize(receiveBufferSize);
}
socket.setReuseAddress(isReuseAddr());
socket.bind(address, getMaxPendingConnections());
selector = Selector.open();
Expand Down Expand Up @@ -655,7 +658,8 @@ protected void releaseBuffers(WebSocket c) throws InterruptedException {
}

public ByteBuffer createBuffer() {
return ByteBuffer.allocate(WebSocketImpl.RCVBUF);
int receiveBufferSize = getReceiveBufferSize();
return ByteBuffer.allocate(receiveBufferSize > 0 ? receiveBufferSize : DEFAULT_READ_BUFFER_SIZE);
}

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