|
26 | 26 | package org.java_websocket.server;
|
27 | 27 |
|
28 | 28 | import java.io.IOException;
|
29 |
| -import java.net.InetSocketAddress; |
30 |
| -import java.net.ServerSocket; |
31 |
| -import java.net.Socket; |
| 29 | +import java.net.*; |
32 | 30 | import java.nio.ByteBuffer;
|
33 | 31 | import java.nio.channels.CancelledKeyException;
|
34 | 32 | import java.nio.channels.ClosedByInterruptException;
|
@@ -108,6 +106,12 @@ public abstract class WebSocketServer extends AbstractWebSocket implements Runna
|
108 | 106 |
|
109 | 107 | private WebSocketServerFactory wsf = new DefaultWebSocketServerFactory();
|
110 | 108 |
|
| 109 | + /** |
| 110 | + * Attribute which allows you to configure the socket "backlog" parameter |
| 111 | + * which determines how many client connections can be queued. |
| 112 | + */ |
| 113 | + private int maxPendingConnections = -1; |
| 114 | + |
111 | 115 | /**
|
112 | 116 | * Creates a WebSocketServer that will attempt to
|
113 | 117 | * listen on port <var>WebSocketImpl.DEFAULT_PORT</var>.
|
@@ -308,6 +312,24 @@ public List<Draft> getDraft() {
|
308 | 312 | return Collections.unmodifiableList( drafts );
|
309 | 313 | }
|
310 | 314 |
|
| 315 | + /** |
| 316 | + * Set the requested maximum number of pending connections on the socket. The exact semantics are implementation |
| 317 | + * specific. The value provided should be greater than 0. If it is less than or equal to 0, then |
| 318 | + * an implementation specific default will be used. This option will be passed as "backlog" parameter to {@link ServerSocket#bind(SocketAddress, int)} |
| 319 | + */ |
| 320 | + public void setMaxPendingConnections(int numberOfConnections) { |
| 321 | + maxPendingConnections = numberOfConnections; |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * Returns the currently configured maximum number of pending connections. |
| 326 | + * |
| 327 | + * @see #setMaxPendingConnections(int) |
| 328 | + */ |
| 329 | + public int getMaxPendingConnections() { |
| 330 | + return maxPendingConnections; |
| 331 | + } |
| 332 | + |
311 | 333 | // Runnable IMPLEMENTATION /////////////////////////////////////////////////
|
312 | 334 | public void run() {
|
313 | 335 | if (!doEnsureSingleThread()) {
|
@@ -505,7 +527,7 @@ private boolean doSetupSelectorAndServerThread() {
|
505 | 527 | ServerSocket socket = server.socket();
|
506 | 528 | socket.setReceiveBufferSize( WebSocketImpl.RCVBUF );
|
507 | 529 | socket.setReuseAddress( isReuseAddr() );
|
508 |
| - socket.bind( address ); |
| 530 | + socket.bind( address, getMaxPendingConnections() ); |
509 | 531 | selector = Selector.open();
|
510 | 532 | server.register( selector, server.validOps() );
|
511 | 533 | startConnectionLostTimer();
|
|
0 commit comments