mirror of
https://github.com/tursom/TursomServer.git
synced 2025-01-01 16:00:26 +08:00
fix bug
This commit is contained in:
parent
a14a4b3e0d
commit
b5e4fbf44a
@ -15,7 +15,7 @@ object AsyncDatagramClient {
|
||||
private val nioThread = WorkerLoopNioThread(
|
||||
"nioClient",
|
||||
daemon = false,
|
||||
workLoop = WorkerLoopHandler(AsyncProtocol)::handle
|
||||
workLoop = WorkerLoopHandler(AsyncProtocol)
|
||||
)
|
||||
|
||||
suspend fun connect(host: String, port: Int): NioDatagram = connect(InetSocketAddress(host, port))
|
||||
|
@ -5,8 +5,11 @@ import cn.tursom.niothread.NioProtocol
|
||||
import java.nio.channels.SelectionKey
|
||||
import java.nio.channels.ServerSocketChannel
|
||||
|
||||
class BossLoopHandler(private val protocol: NioProtocol, private val workerThread: NioThread? = null) {
|
||||
fun handle(nioThread: NioThread, key: SelectionKey) {
|
||||
class BossLoopHandler(
|
||||
private val protocol: NioProtocol,
|
||||
private val workerThread: NioThread? = null
|
||||
) : (NioThread, SelectionKey) -> Unit {
|
||||
override fun invoke(nioThread: NioThread, key: SelectionKey) {
|
||||
val workerThread: NioThread = workerThread ?: nioThread
|
||||
try {
|
||||
when {
|
||||
|
@ -4,8 +4,8 @@ import cn.tursom.niothread.NioProtocol
|
||||
import cn.tursom.niothread.NioThread
|
||||
import java.nio.channels.SelectionKey
|
||||
|
||||
class WorkerLoopHandler(private val protocol: NioProtocol) {
|
||||
fun handle(nioThread: NioThread, key: SelectionKey) {
|
||||
class WorkerLoopHandler(private val protocol: NioProtocol) : (NioThread, SelectionKey) -> Unit {
|
||||
override fun invoke(nioThread: NioThread, key: SelectionKey) {
|
||||
try {
|
||||
when {
|
||||
key.isReadable -> {
|
||||
|
@ -3,6 +3,6 @@ package cn.tursom.socket
|
||||
import cn.tursom.core.pool.MemoryPool
|
||||
|
||||
class BufferedNioSocket(
|
||||
val socket: AsyncSocket,
|
||||
override val prevChannel: AsyncSocket,
|
||||
override val pool: MemoryPool
|
||||
) : BufferedAsyncSocket, AsyncSocket by socket
|
||||
) : BufferedAsyncSocket, AsyncSocket by prevChannel
|
@ -1,5 +1,6 @@
|
||||
package cn.tursom.socket
|
||||
|
||||
import cn.tursom.channel.AsyncChannel
|
||||
import cn.tursom.channel.AsyncProtocol
|
||||
import cn.tursom.niothread.WorkerLoopNioThread
|
||||
import cn.tursom.niothread.loophandler.WorkerLoopHandler
|
||||
@ -19,13 +20,13 @@ object NioClient {
|
||||
private val nioThread = WorkerLoopNioThread(
|
||||
"nioClient",
|
||||
daemon = false,
|
||||
workLoop = WorkerLoopHandler(AsyncProtocol)::handle
|
||||
workLoop = WorkerLoopHandler(AsyncProtocol)
|
||||
)
|
||||
|
||||
suspend fun connect(host: String, port: Int, timeout: Long = 0): NioSocket {
|
||||
val key: SelectionKey = suspendCoroutine { cont ->
|
||||
val channel = getConnection(host, port)
|
||||
val timeoutTask = if (timeout > 0) NioSocket.timer.exec(timeout) {
|
||||
val timeoutTask = if (timeout > 0) AsyncChannel.timer.exec(timeout) {
|
||||
channel.close()
|
||||
cont.resumeWithException(TimeoutException())
|
||||
} else {
|
||||
|
@ -25,11 +25,11 @@ class NioLoopServer(
|
||||
}
|
||||
) : SocketServer {
|
||||
private val listenChannel = ServerSocketChannel.open()
|
||||
private val workerNioThread = nioThreadFactory("nio-worker", WorkerLoopHandler(protocol)::handle)
|
||||
private val workerNioThread = nioThreadFactory("nio-worker", WorkerLoopHandler(protocol))
|
||||
private val bossNioThread = nioThreadFactory("nio-boss", BossLoopHandler(
|
||||
protocol,
|
||||
workerNioThread
|
||||
)::handle)
|
||||
)::invoke)
|
||||
private val started = AtomicBoolean(false)
|
||||
|
||||
override fun run() {
|
||||
|
Loading…
Reference in New Issue
Block a user