Skip to content

Commit

Permalink
Merge pull request #1407 from TooTallNate/receive-buffer-size
Browse files Browse the repository at this point in the history
  • Loading branch information
marci4 authored Apr 27, 2024
2 parents 1f842a6 + f625a1a commit 7e47e4d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
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

0 comments on commit 7e47e4d

Please sign in to comment.