mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2025-04-20 03:24:50 +00:00
Merge branch 'michael/proxy-port-fix' into 'master'
Added retry for creating http server when port is in use See merge request videostreaming/grayjay!48
This commit is contained in:
commit
ecf9f92e50
1 changed files with 19 additions and 4 deletions
|
@ -10,6 +10,7 @@ import java.io.BufferedInputStream
|
|||
import java.io.OutputStream
|
||||
import java.lang.reflect.Field
|
||||
import java.lang.reflect.Method
|
||||
import java.net.BindException
|
||||
import java.net.InetAddress
|
||||
import java.net.NetworkInterface
|
||||
import java.net.ServerSocket
|
||||
|
@ -41,20 +42,34 @@ class ManagedHttpServer(private val _requestedPort: Int = 0) {
|
|||
_workerPool = Executors.newCachedThreadPool();
|
||||
|
||||
Thread {
|
||||
var socket: ServerSocket? = null
|
||||
try {
|
||||
val socket = ServerSocket(_requestedPort);
|
||||
socket = ServerSocket(_requestedPort);
|
||||
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;
|
||||
while (_stopCount == stopCount) {
|
||||
if(_logVerbose)
|
||||
Logger.i(TAG, "Waiting for connection...");
|
||||
val s = socket.accept() ?: continue;
|
||||
val s = socket?.accept() ?: continue;
|
||||
|
||||
try {
|
||||
handleClientRequest(s);
|
||||
}
|
||||
catch(ex : Exception) {
|
||||
} catch(ex : Exception) {
|
||||
Logger.e(TAG, "Client disconnected due to: " + ex.message, ex);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue