Skip to content

Commit

Permalink
close receiving socket
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Jul 20, 2024
1 parent 9101c28 commit a2f3b00
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/commonMain/kotlin/dev/datlag/k2k/connect/ConnectionServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class ConnectionServer(

receiveJob = scope.launch(Dispatcher.IO) {
while (currentCoroutineContext().isActive) {
closeSocket()
val socketAddress = InetSocketAddress(NetInterface.getLocalAddress(), port)

connectedSocket = socket.bind(socketAddress) {
Expand All @@ -66,13 +67,10 @@ internal class ConnectionServer(
}
}

override fun close() {
private fun closeSocket() {
connectedSocket?.close()
connectedSocket = null

receiveJob?.cancel()
receiveJob = null

socket = aSocket(SelectorManager(Dispatcher.IO)).let {
if (immediate) {
it.tcpNoDelay().tcp()
Expand All @@ -81,4 +79,11 @@ internal class ConnectionServer(
}
}
}

override fun close() {
receiveJob?.cancel()
receiveJob = null

closeSocket()
}
}

0 comments on commit a2f3b00

Please sign in to comment.