nanohttpd: refactor: remove socket timeout parameter

This commit is contained in:
Haowei Wen 2020-08-26 20:42:30 +08:00
parent 85f38d47ac
commit cda96f96e1
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4

View file

@ -160,16 +160,17 @@ public abstract class NanoHTTPD {
*/
private class ServerRunnable implements Runnable {
private final int timeout;
/**
* Maximum time to wait on Socket.getInputStream().read() (in milliseconds)
* This is required as the Keep-Alive HTTP connections would otherwise block
* the socket reading thread forever (or as long the browser is open).
*/
private static final int SOCKET_READ_TIMEOUT = 5000;
private IOException bindException;
private boolean hasBinded = false;
public ServerRunnable(int timeout) {
this.timeout = timeout;
}
@Override
public void run() {
try {
@ -183,9 +184,7 @@ public abstract class NanoHTTPD {
try {
@SuppressWarnings("resource")
final Socket finalAccept = NanoHTTPD.this.myServerSocket.accept();
if (this.timeout > 0) {
finalAccept.setSoTimeout(this.timeout);
}
finalAccept.setSoTimeout(SOCKET_READ_TIMEOUT);
@SuppressWarnings("resource")
final InputStream inputStream = finalAccept.getInputStream();
NanoHTTPD.this.asyncRunner.exec(new ClientHandler(inputStream, finalAccept));
@ -196,13 +195,6 @@ public abstract class NanoHTTPD {
}
}
/**
* Maximum time to wait on Socket.getInputStream().read() (in milliseconds)
* This is required as the Keep-Alive HTTP connections would otherwise block
* the socket reading thread forever (or as long the browser is open).
*/
public static final int SOCKET_READ_TIMEOUT = 5000;
static final void safeClose(Object closeable) {
try {
if (closeable != null) {
@ -281,37 +273,25 @@ public abstract class NanoHTTPD {
}
/**
* Start the server.
*
* @throws IOException
* if the socket is in use.
* Starts the server in daemon mode.
*/
public void start() throws IOException {
start(NanoHTTPD.SOCKET_READ_TIMEOUT);
}
/**
* Starts the server (in setDaemon(true) mode).
*/
public void start(final int timeout) throws IOException {
start(timeout, true);
start(true);
}
/**
* Start the server.
*
* @param timeout
* timeout to use for socket connections.
* @param daemon
* start the thread daemon or not.
* @throws IOException
* if the socket is in use.
*/
public void start(final int timeout, boolean daemon) throws IOException {
public void start(boolean daemon) throws IOException {
this.myServerSocket = new ServerSocket();
this.myServerSocket.setReuseAddress(true);
ServerRunnable serverRunnable = new ServerRunnable(timeout);
ServerRunnable serverRunnable = new ServerRunnable();
this.myThread = new Thread(serverRunnable);
this.myThread.setDaemon(daemon);
this.myThread.setName("NanoHttpd Main Listener");