diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/datagram/AsyncDatagramClient.kt b/AsyncSocket/src/main/kotlin/cn/tursom/datagram/AsyncDatagramClient.kt index 56b71e6..eca45e5 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/datagram/AsyncDatagramClient.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/datagram/AsyncDatagramClient.kt @@ -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)) diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/BossLoopHandler.kt b/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/BossLoopHandler.kt index 317cff4..71b2f15 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/BossLoopHandler.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/BossLoopHandler.kt @@ -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 { diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/WorkerLoopHandler.kt b/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/WorkerLoopHandler.kt index fb91261..ca04ab0 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/WorkerLoopHandler.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/niothread/loophandler/WorkerLoopHandler.kt @@ -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 -> { diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/socket/BufferedNioSocket.kt b/AsyncSocket/src/main/kotlin/cn/tursom/socket/BufferedNioSocket.kt index dbb7190..cc7f89d 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/socket/BufferedNioSocket.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/socket/BufferedNioSocket.kt @@ -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 \ No newline at end of file +) : BufferedAsyncSocket, AsyncSocket by prevChannel \ No newline at end of file diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/socket/NioClient.kt b/AsyncSocket/src/main/kotlin/cn/tursom/socket/NioClient.kt index 36464d0..f6d639f 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/socket/NioClient.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/socket/NioClient.kt @@ -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 { diff --git a/AsyncSocket/src/main/kotlin/cn/tursom/socket/server/NioLoopServer.kt b/AsyncSocket/src/main/kotlin/cn/tursom/socket/server/NioLoopServer.kt index 4e06d5d..c2d5ef9 100644 --- a/AsyncSocket/src/main/kotlin/cn/tursom/socket/server/NioLoopServer.kt +++ b/AsyncSocket/src/main/kotlin/cn/tursom/socket/server/NioLoopServer.kt @@ -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() {