mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-23 22:30:47 +08:00
Rewrite PlatformDatagramChannel, easier to use
This commit is contained in:
parent
f5261b257e
commit
9bc3d933f7
@ -2,43 +2,42 @@ package net.mamoe.mirai.utils.io
|
|||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlinx.io.core.ByteReadPacket
|
||||||
import kotlinx.io.core.Closeable
|
import kotlinx.io.core.Closeable
|
||||||
import kotlinx.io.core.IoBuffer
|
import kotlinx.io.nio.readPacketAtMost
|
||||||
import kotlinx.io.nio.read
|
import kotlinx.io.nio.writePacket
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.nio.channels.DatagramChannel
|
import java.nio.channels.DatagramChannel
|
||||||
import java.nio.channels.ReadableByteChannel
|
import java.nio.channels.ReadableByteChannel
|
||||||
|
import java.nio.channels.WritableByteChannel
|
||||||
|
|
||||||
actual class PlatformDatagramChannel actual constructor(serverHost: String, serverPort: Short) : Closeable {
|
actual typealias ClosedChannelException = java.nio.channels.ClosedChannelException
|
||||||
private val serverAddress: InetSocketAddress = InetSocketAddress(serverHost, serverPort.toInt())
|
|
||||||
private val channel: DatagramChannel = DatagramChannel.open().connect(serverAddress)
|
|
||||||
|
|
||||||
@Throws(ReadPacketInternalException::class)
|
/**
|
||||||
actual suspend fun read(buffer: IoBuffer) = withContext(Dispatchers.IO) {
|
* 多平台适配的 DatagramChannel.
|
||||||
|
*/
|
||||||
|
actual class PlatformDatagramChannel actual constructor(
|
||||||
|
serverHost: String,
|
||||||
|
serverPort: Short
|
||||||
|
) : Closeable {
|
||||||
|
@PublishedApi
|
||||||
|
internal val channel: DatagramChannel = DatagramChannel.open().connect(InetSocketAddress(serverHost, serverPort.toInt()))
|
||||||
|
actual val isOpen: Boolean get() = channel.isOpen
|
||||||
|
override fun close() = channel.close()
|
||||||
|
|
||||||
|
actual suspend inline fun send(packet: ByteReadPacket): Boolean = withContext(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
|
(channel as WritableByteChannel).writePacket(packet)
|
||||||
(channel as ReadableByteChannel).read(buffer)
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
throw ReadPacketInternalException(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(SendPacketInternalException::class)
|
|
||||||
actual suspend fun send(buffer: IoBuffer) = withContext(Dispatchers.IO) {
|
|
||||||
buffer.readDirect {
|
|
||||||
try {
|
|
||||||
channel.send(it, serverAddress)
|
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
throw SendPacketInternalException(e)
|
throw SendPacketInternalException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun close() {
|
actual suspend inline fun read(): ByteReadPacket = withContext(Dispatchers.IO) {
|
||||||
channel.close()
|
try {
|
||||||
|
(channel as ReadableByteChannel).readPacketAtMost(Long.MAX_VALUE)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
throw ReadPacketInternalException(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
actual val isOpen: Boolean get() = channel.isOpen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
actual typealias ClosedChannelException = java.nio.channels.ClosedChannelException
|
|
@ -1,16 +1,24 @@
|
|||||||
package net.mamoe.mirai.utils.io
|
package net.mamoe.mirai.utils.io
|
||||||
|
|
||||||
|
import kotlinx.io.core.ByteReadPacket
|
||||||
import kotlinx.io.core.Closeable
|
import kotlinx.io.core.Closeable
|
||||||
import kotlinx.io.core.IoBuffer
|
|
||||||
import kotlinx.io.errors.IOException
|
import kotlinx.io.errors.IOException
|
||||||
|
import net.mamoe.mirai.utils.MiraiInternalAPI
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多平台适配的 DatagramChannel.
|
* 多平台适配的 DatagramChannel.
|
||||||
*/
|
*/
|
||||||
|
@MiraiInternalAPI
|
||||||
expect class PlatformDatagramChannel(serverHost: String, serverPort: Short) : Closeable {
|
expect class PlatformDatagramChannel(serverHost: String, serverPort: Short) : Closeable {
|
||||||
|
/**
|
||||||
|
* @throws SendPacketInternalException
|
||||||
|
*/
|
||||||
|
suspend inline fun send(packet: ByteReadPacket): Boolean
|
||||||
|
|
||||||
suspend fun read(buffer: IoBuffer): Int
|
/**
|
||||||
suspend fun send(buffer: IoBuffer): Int
|
* @throws ReadPacketInternalException
|
||||||
|
*/
|
||||||
|
suspend inline fun read(): ByteReadPacket
|
||||||
|
|
||||||
val isOpen: Boolean
|
val isOpen: Boolean
|
||||||
}
|
}
|
@ -2,49 +2,47 @@ package net.mamoe.mirai.utils.io
|
|||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import kotlinx.io.core.ByteReadPacket
|
||||||
import kotlinx.io.core.Closeable
|
import kotlinx.io.core.Closeable
|
||||||
import kotlinx.io.core.IoBuffer
|
import kotlinx.io.nio.readPacketAtMost
|
||||||
import kotlinx.io.nio.read
|
import kotlinx.io.nio.writePacket
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.nio.channels.DatagramChannel
|
import java.nio.channels.DatagramChannel
|
||||||
import java.nio.channels.ReadableByteChannel
|
import java.nio.channels.ReadableByteChannel
|
||||||
|
import java.nio.channels.WritableByteChannel
|
||||||
|
|
||||||
actual class PlatformDatagramChannel actual constructor(serverHost: String, serverPort: Short) : Closeable {
|
|
||||||
private val serverAddress: InetSocketAddress = InetSocketAddress(serverHost, serverPort.toInt())
|
|
||||||
private val channel: DatagramChannel = DatagramChannel.open().connect(serverAddress)
|
|
||||||
|
|
||||||
@Throws(ReadPacketInternalException::class)
|
actual typealias ClosedChannelException = java.nio.channels.ClosedChannelException
|
||||||
actual suspend fun read(buffer: IoBuffer) = withContext(Dispatchers.IO) {
|
|
||||||
|
/**
|
||||||
|
* 多平台适配的 DatagramChannel.
|
||||||
|
*/
|
||||||
|
actual class PlatformDatagramChannel actual constructor(
|
||||||
|
serverHost: String,
|
||||||
|
serverPort: Short
|
||||||
|
) : Closeable {
|
||||||
|
@PublishedApi
|
||||||
|
internal val channel: DatagramChannel = DatagramChannel.open().connect(InetSocketAddress(serverHost, serverPort.toInt()))
|
||||||
|
actual val isOpen: Boolean get() = channel.isOpen
|
||||||
|
override fun close() = channel.close()
|
||||||
|
|
||||||
|
actual suspend inline fun send(packet: ByteReadPacket): Boolean = withContext(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
(channel as ReadableByteChannel).read(buffer)
|
(channel as WritableByteChannel).writePacket(packet)
|
||||||
} catch (e: ClosedChannelException) {
|
|
||||||
throw e
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
throw ReadPacketInternalException(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(SendPacketInternalException::class)
|
|
||||||
actual suspend fun send(buffer: IoBuffer) = withContext(Dispatchers.IO) {
|
|
||||||
buffer.readDirect {
|
|
||||||
try {
|
|
||||||
channel.send(it, serverAddress)
|
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
throw SendPacketInternalException(e)
|
throw SendPacketInternalException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actual suspend inline fun read(): ByteReadPacket = withContext(Dispatchers.IO) {
|
||||||
|
try {
|
||||||
|
(channel as ReadableByteChannel).readPacketAtMost(Long.MAX_VALUE)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
throw ReadPacketInternalException(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
|
||||||
channel.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
actual val isOpen: Boolean get() = channel.isOpen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
actual typealias ClosedChannelException = java.nio.channels.ClosedChannelException
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
actual class PlatformDatagramChannel actual constructor(serverHost: String, serverPort: Short) : Closeable {
|
actual class PlatformDatagramChannel actual constructor(serverHost: String, serverPort: Short) : Closeable {
|
||||||
|
Loading…
Reference in New Issue
Block a user