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
ce3ba66d
Commit
ce3ba66d
authored
Feb 08, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Faster init: saved 200ms for each load
parent
44cdbf8a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
109 deletions
+120
-109
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
...moe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
+120
-109
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
View file @
ce3ba66d
...
@@ -33,6 +33,8 @@ import net.mamoe.mirai.utils.*
...
@@ -33,6 +33,8 @@ import net.mamoe.mirai.utils.*
import
net.mamoe.mirai.utils.io.*
import
net.mamoe.mirai.utils.io.*
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.jvm.Volatile
import
kotlin.jvm.Volatile
import
kotlin.time.ExperimentalTime
import
kotlin.time.measureTime
@Suppress
(
"MemberVisibilityCanBePrivate"
)
@Suppress
(
"MemberVisibilityCanBePrivate"
)
@UseExperimental
(
MiraiInternalAPI
::
class
)
@UseExperimental
(
MiraiInternalAPI
::
class
)
...
@@ -106,8 +108,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -106,8 +108,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
StatSvc
.
Register
(
bot
.
client
).
sendAndExpect
<
StatSvc
.
Register
.
Response
>(
6000
)
// it's slow
StatSvc
.
Register
(
bot
.
client
).
sendAndExpect
<
StatSvc
.
Register
.
Response
>(
6000
)
// it's slow
}
}
@UseExperimental
(
MiraiExperimentalAPI
::
class
)
@UseExperimental
(
MiraiExperimentalAPI
::
class
,
ExperimentalTime
::
class
)
override
suspend
fun
init
()
{
override
suspend
fun
init
()
:
Unit
=
coroutineScope
{
this
@QQAndroidBotNetworkHandler
.
subscribeAlways
<
ForceOfflineEvent
>
{
this
@QQAndroidBotNetworkHandler
.
subscribeAlways
<
ForceOfflineEvent
>
{
if
(
this
@QQAndroidBotNetworkHandler
.
bot
==
this
.
bot
)
{
if
(
this
@QQAndroidBotNetworkHandler
.
bot
==
this
.
bot
)
{
this
.
bot
.
logger
.
error
(
"被挤下线"
)
this
.
bot
.
logger
.
error
(
"被挤下线"
)
...
@@ -117,12 +119,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -117,12 +119,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
MessageSvc
.
PbGetMsg
(
bot
.
client
,
MsgSvc
.
SyncFlag
.
START
,
currentTimeSeconds
).
sendWithoutExpect
()
MessageSvc
.
PbGetMsg
(
bot
.
client
,
MsgSvc
.
SyncFlag
.
START
,
currentTimeSeconds
).
sendWithoutExpect
()
//val msg = MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect<MessageSvc.PbGetMsg.Response>()
//println(msg.contentToString())
bot
.
qqs
.
delegate
.
clear
()
bot
.
qqs
.
delegate
.
clear
()
bot
.
groups
.
delegate
.
clear
()
bot
.
groups
.
delegate
.
clear
()
val
startTime
=
currentTimeMillis
val
friendListLoadTime
=
async
{
measureTime
{
try
{
try
{
bot
.
logger
.
info
(
"开始加载好友信息"
)
bot
.
logger
.
info
(
"开始加载好友信息"
)
var
currentFriendCount
=
0
var
currentFriendCount
=
0
...
@@ -153,10 +154,13 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -153,10 +154,13 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
bot
.
logger
.
error
(
"加载好友列表失败|一般这是由于加载过于频繁导致/将以热加载方式加载好友列表"
)
bot
.
logger
.
error
(
"加载好友列表失败|一般这是由于加载过于频繁导致/将以热加载方式加载好友列表"
)
}
}
}
}
val
friendLoadFinish
=
currentTimeMillis
val
groupInfo
=
mutableMapOf
<
Long
,
Int
>()
val
groupInfo
=
mutableMapOf
<
Long
,
Int
>()
coroutineScope
{
val
groupTime
=
async
{
measureTime
{
try
{
try
{
bot
.
logger
.
info
(
"开始加载群组列表与群成员列表"
)
bot
.
logger
.
info
(
"开始加载群组列表与群成员列表"
)
val
troopListData
=
FriendList
.
GetTroopListSimplify
(
bot
.
client
)
val
troopListData
=
FriendList
.
GetTroopListSimplify
(
bot
.
client
)
...
@@ -198,11 +202,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -198,11 +202,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
bot
.
groups
.
delegate
.
addLast
(
group
)
bot
.
groups
.
delegate
.
addLast
(
group
)
launch
{
launch
{
try
{
try
{
get
TroopMemberList
(
group
,
contactList
,
troopNum
.
dwGroupOwnerUin
)
fill
TroopMemberList
(
group
,
contactList
,
troopNum
.
dwGroupOwnerUin
)
groupInfo
[
troopNum
.
groupCode
]
=
contactList
.
size
groupInfo
[
troopNum
.
groupCode
]
=
contactList
.
size
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
groupInfo
[
troopNum
.
groupCode
]
=
-
1
groupInfo
[
troopNum
.
groupCode
]
=
-
1
bot
.
logger
.
info
(
"群${troopNum.groupCode}的列表拉取失败, 将采用动态加入"
)
bot
.
logger
.
warning
(
"群${troopNum.groupCode}的列表拉取失败, 将采用动态加入"
)
bot
.
logger
.
error
(
e
)
bot
.
logger
.
error
(
e
)
}
}
}
}
...
@@ -213,6 +217,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -213,6 +217,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
bot
.
logger
.
error
(
e
)
bot
.
logger
.
error
(
e
)
}
}
}
}
}
//===log===//
//===log===//
fun
fillUntil
(
long
:
Number
,
size
:
Int
):
String
{
fun
fillUntil
(
long
:
Number
,
size
:
Int
):
String
{
val
x
=
long
.
toString
()
val
x
=
long
.
toString
()
...
@@ -225,9 +231,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -225,9 +231,11 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
)
)
}
}
joinAll
(
friendListLoadTime
,
groupTime
)
bot
.
logger
.
info
(
"====================Mirai Bot List初始化完毕===================="
)
bot
.
logger
.
info
(
"====================Mirai Bot List初始化完毕===================="
)
bot
.
logger
.
info
(
"好友数量: ${fillUntil(bot.qqs.size, 9)}\t\t\t 加载时间: ${friendL
oadFinish - startTime
}ms"
)
bot
.
logger
.
info
(
"好友数量: ${fillUntil(bot.qqs.size, 9)}\t\t\t 加载时间: ${friendL
istLoadTime.await().inMilliseconds
}ms"
)
bot
.
logger
.
info
(
"加入群组: ${fillUntil(bot.groups.size, 9)}\t\t\t 加载时间: ${
currentTimeMillis - friendLoadFinish
}ms"
)
bot
.
logger
.
info
(
"加入群组: ${fillUntil(bot.groups.size, 9)}\t\t\t 加载时间: ${
groupTime.await().inMilliseconds
}ms"
)
groupInfo
.
forEach
{
groupInfo
.
forEach
{
if
(
it
.
value
==
-
1
)
{
if
(
it
.
value
==
-
1
)
{
bot
.
logger
.
error
(
"群组号码: ${fillUntil(it.key, 9)}\t 成员数量加载失败"
)
bot
.
logger
.
error
(
"群组号码: ${fillUntil(it.key, 9)}\t 成员数量加载失败"
)
...
@@ -242,11 +250,22 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -242,11 +250,22 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
}
}
bot
.
logger
.
info
(
"====================Mirai Bot List初始化完毕===================="
)
bot
.
logger
.
info
(
"====================Mirai Bot List初始化完毕===================="
)
bot
.
firstLoginSucceed
=
true
this
@QQAndroidBotNetworkHandler
.
launch
(
CoroutineName
(
"Heartbeat"
))
{
launch
{
while
(
this
.
isActive
)
{
while
(
this
.
isActive
)
{
delay
(
bot
.
configuration
.
heartbeatPeriodMillis
)
delay
(
bot
.
configuration
.
heartbeatPeriodMillis
)
val
failException
=
doHeartBeat
()
if
(
failException
!=
null
)
{
delay
(
bot
.
configuration
.
firstReconnectDelayMillis
)
close
()
bot
.
tryReinitializeNetworkHandler
(
failException
)
}
}
}
bot
.
firstLoginSucceed
=
true
}
suspend
fun
doHeartBeat
():
Exception
?
{
var
lastException
:
Exception
?
var
lastException
:
Exception
?
try
{
try
{
check
(
check
(
...
@@ -256,20 +275,15 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -256,20 +275,15 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
retry
=
1
retry
=
1
)
is
StatSvc
.
GetOnlineStatus
.
Response
.
Success
)
is
StatSvc
.
GetOnlineStatus
.
Response
.
Success
)
)
continue
return
null
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
lastException
=
e
lastException
=
e
}
}
delay
(
bot
.
configuration
.
firstReconnectDelayMillis
)
return
lastException
close
()
bot
.
tryReinitializeNetworkHandler
(
lastException
)
}
}
}
}
suspend
fun
getTroopMemberList
(
group
:
GroupImpl
,
list
:
ContactList
<
Member
>,
owner
:
Long
):
ContactList
<
Member
>
{
suspend
fun
fillTroopMemberList
(
group
:
GroupImpl
,
list
:
ContactList
<
Member
>,
owner
:
Long
)
{
bot
.
logger
.
info
(
"开始获取群[${group.uin}]成员列表"
)
bot
.
logger
.
verbose
(
"开始获取群[${group.uin}]成员列表"
)
var
size
=
0
var
size
=
0
var
nextUin
=
0L
var
nextUin
=
0L
while
(
true
)
{
while
(
true
)
{
...
@@ -306,10 +320,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -306,10 +320,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
if
(
nextUin
==
0L
)
{
if
(
nextUin
==
0L
)
{
break
break
}
}
//println("已获取群[${group.uin}]成员列表前" + size + "个成员")
}
}
//println("群[${group.uin}]成员全部获取完成, 共${list.size}个成员")
return
list
}
}
/**
/**
...
@@ -473,7 +484,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -473,7 +484,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
cachedPacketTimeoutJob
=
launch
{
cachedPacketTimeoutJob
=
launch
{
delay
(
1000
)
delay
(
1000
)
if
(
cachedPacketTimeoutJob
==
this
.
coroutineContext
[
Job
]
&&
cachedPacket
.
getAndSet
(
null
)
!=
null
)
{
if
(
cachedPacketTimeoutJob
==
this
.
coroutineContext
[
Job
]
&&
cachedPacket
.
getAndSet
(
null
)
!=
null
)
{
PacketLogger
.
verbose
(
"等待另一部分包时超时. 将舍弃已接收的半个包"
)
PacketLogger
.
verbose
{
"等待另一部分包时超时. 将舍弃已接收的半个包"
}
}
}
}
}
}
}
...
...
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