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
b4140b78
Commit
b4140b78
authored
Apr 08, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract `reloadFriendList()` outside `init()`
parent
6480e78b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
49 deletions
+38
-49
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
...moe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
+38
-49
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
View file @
b4140b78
...
@@ -185,6 +185,42 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -185,6 +185,42 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
internal
var
pendingIncomingPackets
:
LockFreeLinkedList
<
KnownPacketFactories
.
IncomingPacket
<
*
>>?
=
internal
var
pendingIncomingPackets
:
LockFreeLinkedList
<
KnownPacketFactories
.
IncomingPacket
<
*
>>?
=
LockFreeLinkedList
()
LockFreeLinkedList
()
/**
* Don't use concurrently
*/
suspend
fun
reloadFriendList
()
{
// 不要用 fun, 不要 join declaration, 不要用 val, 编译失败警告
logger
.
info
(
"开始加载好友信息"
)
var
currentFriendCount
=
0
var
totalFriendCount
:
Short
while
(
true
)
{
val
data
=
FriendList
.
GetFriendGroupList
(
bot
.
client
,
currentFriendCount
,
150
,
0
,
0
).
sendAndExpect
<
FriendList
.
GetFriendGroupList
.
Response
>(
timeoutMillis
=
5000
,
retry
=
2
)
// self info
data
.
selfInfo
?.
run
{
bot
.
selfInfo
=
this
// bot.remark = remark ?: ""
// bot.sex = sex
}
totalFriendCount
=
data
.
totalFriendCount
data
.
friendList
.
forEach
{
// atomic
bot
.
friends
.
delegate
.
addLast
(
QQImpl
(
bot
,
bot
.
coroutineContext
,
it
.
friendUin
,
FriendInfoImpl
(
it
))
).
also
{
currentFriendCount
++
}
}
logger
.
verbose
{
"正在加载好友列表 ${currentFriendCount}/${totalFriendCount}"
}
if
(
currentFriendCount
>=
totalFriendCount
)
{
break
}
// delay(200)
}
logger
.
info
{
"好友列表加载完成, 共 ${currentFriendCount}个"
}
}
@OptIn
(
MiraiExperimentalAPI
::
class
,
ExperimentalTime
::
class
)
@OptIn
(
MiraiExperimentalAPI
::
class
,
ExperimentalTime
::
class
)
override
suspend
fun
init
():
Unit
=
coroutineScope
{
override
suspend
fun
init
():
Unit
=
coroutineScope
{
check
(
bot
.
isActive
)
{
"bot is dead therefore network can't init"
}
check
(
bot
.
isActive
)
{
"bot is dead therefore network can't init"
}
...
@@ -201,54 +237,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -201,54 +237,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
}
}
supervisorScope
{
supervisorScope
{
launch
{
this
.
launch
{
reloadFriendList
()
}
lateinit
var
loadFriends
:
suspend
()
->
Unit
// 不要用 fun, 不要 join declaration, 不要用 val, 编译失败警告
loadFriends
=
suspend
loadFriends
@
{
logger
.
info
(
"开始加载好友信息"
)
var
currentFriendCount
=
0
var
totalFriendCount
:
Short
while
(
true
)
{
val
data
=
runCatching
{
FriendList
.
GetFriendGroupList
(
bot
.
client
,
currentFriendCount
,
150
,
0
,
0
).
sendAndExpect
<
FriendList
.
GetFriendGroupList
.
Response
>(
timeoutMillis
=
5000
,
retry
=
2
)
}.
getOrElse
{
logger
.
error
(
"无法加载好友列表"
,
it
)
this
@QQAndroidBotNetworkHandler
.
launch
{
delay
(
10
.
secondsToMillis
);
loadFriends
()
}
logger
.
error
(
"稍后重试加载好友列表"
)
return
@
loadFriends
}
// self info
data
.
selfInfo
?.
run
{
bot
.
selfInfo
=
this
// bot.remark = remark ?: ""
// bot.sex = sex
}
totalFriendCount
=
data
.
totalFriendCount
data
.
friendList
.
forEach
{
// atomic add
bot
.
friends
.
delegate
.
addLast
(
QQImpl
(
bot
,
bot
.
coroutineContext
,
it
.
friendUin
,
FriendInfoImpl
(
it
))
).
also
{
currentFriendCount
++
}
}
logger
.
verbose
{
"正在加载好友列表 ${currentFriendCount}/${totalFriendCount}"
}
if
(
currentFriendCount
>=
totalFriendCount
)
{
break
}
// delay(200)
}
logger
.
info
{
"好友列表加载完成, 共 ${currentFriendCount}个"
}
}
loadFriends
()
}
launch
{
launch
{
try
{
try
{
...
@@ -591,7 +580,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -591,7 +580,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
/**
/**
* 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms)
* 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms)
*/
*/
suspend
fun
<
E
:
Packet
>
OutgoingPacket
.
sendAndExpect
(
timeoutMillis
:
Long
=
3000
,
retry
:
Int
=
1
):
E
{
suspend
fun
<
E
:
Packet
>
OutgoingPacket
.
sendAndExpect
(
timeoutMillis
:
Long
=
3000
,
retry
:
Int
=
2
):
E
{
require
(
timeoutMillis
>
100
)
{
"timeoutMillis must > 100"
}
require
(
timeoutMillis
>
100
)
{
"timeoutMillis must > 100"
}
require
(
retry
>=
0
)
{
"retry must >= 0"
}
require
(
retry
>=
0
)
{
"retry must >= 0"
}
...
...
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