mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2025-08-03 06:40:19 +00:00
Added retry for creating http server when port is in use
This commit is contained in:
parent
14b699485a
commit
f25c76687e
1 changed files with 19 additions and 4 deletions
|
@ -10,6 +10,7 @@ import java.io.BufferedInputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.lang.reflect.Field
|
import java.lang.reflect.Field
|
||||||
import java.lang.reflect.Method
|
import java.lang.reflect.Method
|
||||||
|
import java.net.BindException
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.net.NetworkInterface
|
import java.net.NetworkInterface
|
||||||
import java.net.ServerSocket
|
import java.net.ServerSocket
|
||||||
|
@ -41,20 +42,34 @@ class ManagedHttpServer(private val _requestedPort: Int = 0) {
|
||||||
_workerPool = Executors.newCachedThreadPool();
|
_workerPool = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
Thread {
|
Thread {
|
||||||
|
var socket: ServerSocket? = null
|
||||||
try {
|
try {
|
||||||
val socket = ServerSocket(_requestedPort);
|
socket = ServerSocket(_requestedPort);
|
||||||
port = socket.localPort;
|
port = socket.localPort;
|
||||||
|
} catch (e: BindException) {
|
||||||
|
try {
|
||||||
|
Logger.w(TAG, "Failed create socket due to port being in use, attempting to automatically choose port...", e);
|
||||||
|
socket = ServerSocket(0);
|
||||||
|
port = socket.localPort;
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
Logger.e(TAG, "Failed to accept socket.", e);
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
Logger.e(TAG, "Failed to accept socket.", e);
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
val stopCount = _stopCount;
|
val stopCount = _stopCount;
|
||||||
while (_stopCount == stopCount) {
|
while (_stopCount == stopCount) {
|
||||||
if(_logVerbose)
|
if(_logVerbose)
|
||||||
Logger.i(TAG, "Waiting for connection...");
|
Logger.i(TAG, "Waiting for connection...");
|
||||||
val s = socket.accept() ?: continue;
|
val s = socket?.accept() ?: continue;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
handleClientRequest(s);
|
handleClientRequest(s);
|
||||||
}
|
} catch(ex : Exception) {
|
||||||
catch(ex : Exception) {
|
|
||||||
Logger.e(TAG, "Client disconnected due to: " + ex.message, ex);
|
Logger.e(TAG, "Client disconnected due to: " + ex.message, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue