mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-13 11:20:10 +08:00
fix bug
This commit is contained in:
parent
28c4fe9ad5
commit
797c7d24e6
@ -2,6 +2,7 @@ package cn.tursom.datagram.server
|
||||
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import java.lang.ref.SoftReference
|
||||
import java.net.SocketAddress
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
@ -9,7 +10,7 @@ open class AsyncDatagramServer(
|
||||
port: Int,
|
||||
private val handler: suspend ServerNioDatagram.() -> Unit
|
||||
) : LoopDatagramServer(port, DatagramProtocol) {
|
||||
private val channelMap = ConcurrentHashMap<SocketAddress, ServerNioDatagram>()
|
||||
private val channelMap = ConcurrentHashMap<SocketAddress, SoftReference<ServerNioDatagram>>()
|
||||
|
||||
private fun initChannel(address: SocketAddress): ServerNioDatagram {
|
||||
val datagram = ServerNioDatagram(address, this, listenChannel, key, bossNioThread)
|
||||
@ -22,16 +23,16 @@ open class AsyncDatagramServer(
|
||||
}
|
||||
|
||||
fun closeChannel(address: SocketAddress): ServerNioDatagram? {
|
||||
return channelMap.remove(address)
|
||||
return channelMap.remove(address)?.get()
|
||||
}
|
||||
|
||||
fun getChannel(address: SocketAddress): ServerNioDatagram {
|
||||
var channel = channelMap[address]
|
||||
var channel = channelMap[address]?.get()
|
||||
if (channel != null) {
|
||||
return channel
|
||||
}
|
||||
channel = initChannel(address)
|
||||
channelMap[address] = channel
|
||||
channelMap[address] = SoftReference(channel)
|
||||
return channel
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user