Skip to content

Commit f28bd1f

Browse files
committed
Add better handling for when proxy server port is already in use (fix #16, hopefully)
1 parent dba683b commit f28bd1f

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Common/src/main/kotlin/gay/object/hexdebug/adapter/proxy/DebugProxyClient.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer
1717
import java.io.IOException
1818
import java.io.InputStream
1919
import java.io.OutputStream
20+
import java.net.BindException
2021
import java.nio.charset.StandardCharsets
2122
import java.util.concurrent.Executors
2223
import kotlin.concurrent.thread
@@ -66,7 +67,7 @@ data class DebugProxyClient(val input: InputStream, val output: OutputStream) {
6667
private fun start() {
6768
thread = thread?.also {
6869
HexDebug.LOGGER.warn("Tried to start DebugAdapterProxyClient while already running")
69-
} ?: thread(name="DebugAdapterProxyClient_$port") {
70+
} ?: thread(name="DebugAdapterProxyClient") {
7071
runBlocking {
7172
wrapperJob = launch { runServerWrapper() }
7273
}
@@ -100,10 +101,23 @@ data class DebugProxyClient(val input: InputStream, val output: OutputStream) {
100101
awaitCancellation()
101102
}
102103
HexDebug.LOGGER.info("Listening for debug client on port {}...", port)
103-
aSocket(selector).tcp().bind(port = port).use { serverSocket ->
104-
while (true) {
105-
acceptClient(serverSocket)
106-
}
104+
try {
105+
aSocket(selector)
106+
.tcp()
107+
.bind(
108+
port = port,
109+
configure = {
110+
reuseAddress = true
111+
},
112+
)
113+
.use { serverSocket ->
114+
while (true) {
115+
acceptClient(serverSocket)
116+
}
117+
}
118+
} catch (e: BindException) {
119+
HexDebug.LOGGER.error("Failed to open local proxy server!", e)
120+
awaitCancellation()
107121
}
108122
}
109123

0 commit comments

Comments
 (0)