Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
9bc3d933
Commit
9bc3d933
authored
Jan 06, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite PlatformDatagramChannel, easier to use
parent
f5261b25
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
59 deletions
+64
-59
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/io/PlatformDatagramChannelAndroid.kt
...et/mamoe/mirai/utils/io/PlatformDatagramChannelAndroid.kt
+27
-28
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/PlatformDatagramChannel.kt
...otlin/net.mamoe.mirai/utils/io/PlatformDatagramChannel.kt
+11
-3
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/io/PlatformSocketJvm.kt
...Main/kotlin/net/mamoe/mirai/utils/io/PlatformSocketJvm.kt
+26
-28
No files found.
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/io/PlatformDatagramChannelAndroid.kt
View file @
9bc3d933
...
...
@@ -2,43 +2,42 @@ package net.mamoe.mirai.utils.io
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.withContext
import
kotlinx.io.core.ByteReadPacket
import
kotlinx.io.core.Closeable
import
kotlinx.io.
core.IoBuffer
import
kotlinx.io.nio.
read
import
kotlinx.io.
nio.readPacketAtMost
import
kotlinx.io.nio.
writePacket
import
java.net.InetSocketAddress
import
java.nio.channels.DatagramChannel
import
java.nio.channels.ReadableByteChannel
import
java.nio.channels.WritableByteChannel
actual
typealias
ClosedChannelException
=
java
.
nio
.
channels
.
ClosedChannelException
/**
* 多平台适配的 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
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
suspend
fun
read
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
actual
suspend
inline
fun
send
(
packet
:
ByteReadPacket
):
Boolean
=
withContext
(
Dispatchers
.
IO
)
{
try
{
(
channel
as
ReadableByteChannel
).
read
(
buffer
)
(
channel
as
WritableByteChannel
).
writePacket
(
packet
)
}
catch
(
e
:
Throwable
)
{
throw
Rea
dPacketInternalException
(
e
)
throw
Sen
dPacketInternalException
(
e
)
}
}
@Throws
(
SendPacketInternalException
::
class
)
actual
suspend
fun
send
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
buffer
.
readDirect
{
try
{
channel
.
send
(
it
,
serverAddress
)
}
catch
(
e
:
Throwable
)
{
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
\ No newline at end of file
}
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/Platform
Socket
.kt
→
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/Platform
DatagramChannel
.kt
View file @
9bc3d933
package
net.mamoe.mirai.utils.io
import
kotlinx.io.core.ByteReadPacket
import
kotlinx.io.core.Closeable
import
kotlinx.io.core.IoBuffer
import
kotlinx.io.errors.IOException
import
net.mamoe.mirai.utils.MiraiInternalAPI
/**
* 多平台适配的 DatagramChannel.
*/
@MiraiInternalAPI
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
}
...
...
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/io/PlatformSocketJvm.kt
View file @
9bc3d933
...
...
@@ -2,49 +2,47 @@ package net.mamoe.mirai.utils.io
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.withContext
import
kotlinx.io.core.ByteReadPacket
import
kotlinx.io.core.Closeable
import
kotlinx.io.
core.IoBuffer
import
kotlinx.io.nio.
read
import
kotlinx.io.
nio.readPacketAtMost
import
kotlinx.io.nio.
writePacket
import
java.net.InetSocketAddress
import
java.nio.channels.DatagramChannel
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
suspend
fun
read
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
actual
typealias
ClosedChannelException
=
java
.
nio
.
channels
.
ClosedChannelException
/**
* 多平台适配的 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
{
(
channel
as
ReadableByteChannel
).
read
(
buffer
)
}
catch
(
e
:
ClosedChannelException
)
{
throw
e
(
channel
as
WritableByteChannel
).
writePacket
(
packet
)
}
catch
(
e
:
Throwable
)
{
throw
Rea
dPacketInternalException
(
e
)
throw
Sen
dPacketInternalException
(
e
)
}
}
@Throws
(
SendPacketInternalException
::
class
)
actual
suspend
fun
send
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
buffer
.
readDirect
{
try
{
channel
.
send
(
it
,
serverAddress
)
}
catch
(
e
:
Throwable
)
{
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 {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment