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
e6cb229b
Commit
e6cb229b
authored
Mar 03, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unified packet sending
parent
a448dbba
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
29 deletions
+40
-29
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
...moe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
+37
-28
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/PacketFactory.kt
.../mirai/qqandroid/network/protocol/packet/PacketFactory.kt
+2
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt
.../net.mamoe.mirai/event/internal/InternalEventListeners.kt
+1
-1
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
View file @
e6cb229b
...
@@ -538,64 +538,73 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -538,64 +538,73 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
check
(
this
@QQAndroidBotNetworkHandler
.
isActive
)
{
"network is dead therefore can't send any packet"
}
check
(
this
@QQAndroidBotNetworkHandler
.
isActive
)
{
"network is dead therefore can't send any packet"
}
logger
.
verbose
(
"Send: ${this.commandName}"
)
logger
.
verbose
(
"Send: ${this.commandName}"
)
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
PacketLogger
.
debug
{
"Channel sending: $commandName"
}
channel
.
send
(
delegate
)
channel
.
send
(
delegate
)
PacketLogger
.
debug
{
"Channel send done: $commandName"
}
}
}
}
}
class
TimeoutException
(
override
val
message
:
String
?)
:
Exception
()
/**
/**
* 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms)
* 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms)
*
*
* @param retry 当不为 0 时将使用 [ByteArrayPool] 缓存. 因此若非必要, 请不要允许 retry
* @param retry 当不为 0 时将使用 [ByteArrayPool] 缓存. 因此若非必要, 请不要允许 retry
*/
*/
suspend
fun
<
E
:
Packet
>
OutgoingPacket
.
sendAndExpect
(
timeoutMillis
:
Long
=
3000
,
retry
:
Int
=
0
):
E
{
suspend
fun
<
E
:
Packet
>
OutgoingPacket
.
sendAndExpect
(
timeoutMillis
:
Long
=
3000
,
retry
:
Int
=
0
):
E
{
require
(
timeoutMillis
>
0
)
{
"timeoutMillis must >
0"
}
require
(
timeoutMillis
>
100
)
{
"timeoutMillis must > 10
0"
}
require
(
retry
>=
0
)
{
"retry must >= 0"
}
require
(
retry
>=
0
)
{
"retry must >= 0"
}
check
(
bot
.
isActive
)
{
"bot is dead therefore can't send any packet"
}
check
(
bot
.
isActive
)
{
"bot is dead therefore can't send any packet"
}
check
(
this
@QQAndroidBotNetworkHandler
.
isActive
)
{
"network is dead therefore can't send any packet"
}
check
(
this
@QQAndroidBotNetworkHandler
.
isActive
)
{
"network is dead therefore can't send any packet"
}
var
lastException
:
Exception
?
=
null
suspend
fun
doSendAndReceive
(
handler
:
PacketListener
,
data
:
Any
,
length
:
Int
):
E
{
if
(
retry
==
0
)
{
val
result
=
async
{
val
handler
=
PacketListener
(
commandName
=
commandName
,
sequenceId
=
sequenceId
)
withTimeoutOrNull
(
3000
)
{
packetListeners
.
addLast
(
handler
)
try
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
channel
.
send
(
delegate
)
PacketLogger
.
debug
{
"Channel sending: $commandName"
}
when
(
data
)
{
is
ByteArray
->
channel
.
send
(
data
,
0
,
length
)
is
ByteReadPacket
->
channel
.
send
(
data
)
else
->
error
(
"Internal error: unexpected data type: ${data::class.simpleName}"
)
}
}
logger
.
verbose
(
"Send: ${this.commandName}"
)
PacketLogger
.
debug
{
"Channel send done: $commandName"
}
return
withTimeoutOrNull
(
timeoutMillis
)
{
}
}
?:
return
@
async
"timeout sending packet $commandName"
logger
.
verbose
(
"Send done: $commandName"
)
withTimeoutOrNull
(
timeoutMillis
)
{
handler
.
await
()
// 不要 `withTimeout`. timeout 的报错会不正常.
}
?:
return
@
async
"timeout receiving response of $commandName"
}
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
)
handler
.
await
()
as
E
when
(
val
value
=
result
.
await
())
{
// 不要 `withTimeout`. timeout 的异常会不知道去哪了.
is
String
->
throw
TimeoutException
(
value
)
}
?:
net
.
mamoe
.
mirai
.
qqandroid
.
utils
.
inline
{
else
->
return
value
as
E
error
(
"timeout when receiving response of $commandName"
)
}
}
}
if
(
retry
==
0
)
{
val
handler
=
PacketListener
(
commandName
=
commandName
,
sequenceId
=
sequenceId
)
packetListeners
.
addLast
(
handler
)
try
{
return
doSendAndReceive
(
handler
,
delegate
,
0
)
// no need
}
finally
{
}
finally
{
packetListeners
.
remove
(
handler
)
packetListeners
.
remove
(
handler
)
}
}
}
else
this
.
delegate
.
useBytes
{
data
,
length
->
}
else
this
.
delegate
.
useBytes
{
data
,
length
->
re
peat
(
retry
+
1
)
{
re
turn
tryNTimes
(
retry
+
1
)
{
val
handler
=
PacketListener
(
commandName
=
commandName
,
sequenceId
=
sequenceId
)
val
handler
=
PacketListener
(
commandName
=
commandName
,
sequenceId
=
sequenceId
)
packetListeners
.
addLast
(
handler
)
packetListeners
.
addLast
(
handler
)
try
{
try
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
doSendAndReceive
(
handler
,
data
,
length
)
channel
.
send
(
data
,
0
,
length
)
}
logger
.
verbose
(
"Send: ${this.commandName}"
)
return
withTimeoutOrNull
(
timeoutMillis
)
{
@Suppress
(
"UNCHECKED_CAST"
)
handler
.
await
()
as
E
// 不要 `withTimeout`. timeout 的异常会不知道去哪了.
}
?:
net
.
mamoe
.
mirai
.
qqandroid
.
utils
.
inline
{
error
(
"timeout when receiving response of $commandName"
)
}
}
catch
(
e
:
Exception
)
{
lastException
=
e
}
finally
{
}
finally
{
packetListeners
.
remove
(
handler
)
packetListeners
.
remove
(
handler
)
}
}
}
}
throw
lastException
!!
}
}
}
}
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/PacketFactory.kt
View file @
e6cb229b
...
@@ -214,6 +214,7 @@ internal object KnownPacketFactories {
...
@@ -214,6 +214,7 @@ internal object KnownPacketFactories {
bot
.
network
.
pendingIncomingPackets
?.
addLast
(
it
.
also
{
bot
.
network
.
pendingIncomingPackets
?.
addLast
(
it
.
also
{
it
.
consumer
=
consumer
it
.
consumer
=
consumer
it
.
flag2
=
flag2
it
.
flag2
=
flag2
PacketLogger
.
info
{
"Cached ${it.commandName} #${it.sequenceId}"
}
})
?:
handleIncomingPacket
(
it
,
bot
,
flag2
,
consumer
)
})
?:
handleIncomingPacket
(
it
,
bot
,
flag2
,
consumer
)
}
else
{
}
else
{
handleIncomingPacket
(
it
,
bot
,
flag2
,
consumer
)
handleIncomingPacket
(
it
,
bot
,
flag2
,
consumer
)
...
@@ -234,6 +235,7 @@ internal object KnownPacketFactories {
...
@@ -234,6 +235,7 @@ internal object KnownPacketFactories {
return
return
}
}
PacketLogger
.
info
{
"Handle packet: ${it.commandName}"
}
it
.
data
.
withUse
{
it
.
data
.
withUse
{
when
(
flag2
)
{
when
(
flag2
)
{
0
,
1
->
0
,
1
->
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt
View file @
e6cb229b
...
@@ -86,7 +86,7 @@ internal class Handler<in E : Event>
...
@@ -86,7 +86,7 @@ internal class Handler<in E : Event>
internal
fun
<
E
:
Event
>
KClass
<
out
E
>.
listeners
():
EventListeners
<
E
>
=
EventListenerManager
.
get
(
this
)
internal
fun
<
E
:
Event
>
KClass
<
out
E
>.
listeners
():
EventListeners
<
E
>
=
EventListenerManager
.
get
(
this
)
internal
class
EventListeners
<
E
:
Event
>(
clazz
:
KClass
<
E
>)
:
LockFreeLinkedList
<
Listener
<
E
>>()
{
internal
class
EventListeners
<
E
:
Event
>(
clazz
:
KClass
<
E
>)
:
LockFreeLinkedList
<
Listener
<
E
>>()
{
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
,
"UNSUPPORTED"
,
"NO_REFLECTION_IN_CLASS_PATH"
)
val
supertypes
:
Set
<
KClass
<
out
Event
>>
by
lazy
{
val
supertypes
:
Set
<
KClass
<
out
Event
>>
by
lazy
{
val
supertypes
=
mutableSetOf
<
KClass
<
out
Event
>>()
val
supertypes
=
mutableSetOf
<
KClass
<
out
Event
>>()
...
...
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