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
3ab0e675
Commit
3ab0e675
authored
Feb 12, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce logger supplier, close #43
parent
defaf83a
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
22 deletions
+34
-22
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
...moe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
+21
-20
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
...nMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
+6
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/BotConfiguration.kt
...mmonMain/kotlin/net.mamoe.mirai/utils/BotConfiguration.kt
+6
-1
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
View file @
3ab0e675
...
@@ -47,9 +47,10 @@ import kotlin.time.ExperimentalTime
...
@@ -47,9 +47,10 @@ import kotlin.time.ExperimentalTime
internal
class
QQAndroidBotNetworkHandler
(
bot
:
QQAndroidBot
)
:
BotNetworkHandler
()
{
internal
class
QQAndroidBotNetworkHandler
(
bot
:
QQAndroidBot
)
:
BotNetworkHandler
()
{
override
val
bot
:
QQAndroidBot
by
bot
.
unsafeWeakRef
()
override
val
bot
:
QQAndroidBot
by
bot
.
unsafeWeakRef
()
override
val
supervisor
:
CompletableJob
=
SupervisorJob
(
bot
.
coroutineContext
[
Job
])
override
val
supervisor
:
CompletableJob
=
SupervisorJob
(
bot
.
coroutineContext
[
Job
])
override
val
logger
:
MiraiLogger
get
()
=
bot
.
configuration
.
networkLoggerSupplier
(
this
)
override
val
coroutineContext
:
CoroutineContext
=
bot
.
coroutineContext
+
CoroutineExceptionHandler
{
_
,
throwable
->
override
val
coroutineContext
:
CoroutineContext
=
bot
.
coroutineContext
+
CoroutineExceptionHandler
{
_
,
throwable
->
bot
.
logger
.
error
(
"Exception in NetworkHandler"
,
throwable
)
logger
.
error
(
"Exception in NetworkHandler"
,
throwable
)
}
}
private
lateinit
var
channel
:
PlatformSocket
private
lateinit
var
channel
:
PlatformSocket
...
@@ -62,7 +63,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -62,7 +63,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
channel
.
connect
(
"113.96.13.208"
,
8080
)
channel
.
connect
(
"113.96.13.208"
,
8080
)
this
.
launch
(
CoroutineName
(
"Incoming Packet Receiver"
))
{
processReceive
()
}
this
.
launch
(
CoroutineName
(
"Incoming Packet Receiver"
))
{
processReceive
()
}
//
bot.
logger.info("Trying login")
// logger.info("Trying login")
var
response
:
WtLogin
.
Login
.
LoginPacketResponse
=
WtLogin
.
Login
.
SubCommand9
(
bot
.
client
).
sendAndExpect
()
var
response
:
WtLogin
.
Login
.
LoginPacketResponse
=
WtLogin
.
Login
.
SubCommand9
(
bot
.
client
).
sendAndExpect
()
mainloop
@
while
(
true
)
{
mainloop
@
while
(
true
)
{
when
(
response
)
{
when
(
response
)
{
...
@@ -104,7 +105,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -104,7 +105,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
}
}
is
WtLogin
.
Login
.
LoginPacketResponse
.
Success
->
{
is
WtLogin
.
Login
.
LoginPacketResponse
.
Success
->
{
bot
.
logger
.
info
(
"Login successful"
)
logger
.
info
(
"Login successful"
)
break
@
mainloop
break
@
mainloop
}
}
}
}
...
@@ -118,7 +119,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -118,7 +119,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
override
suspend
fun
init
():
Unit
=
coroutineScope
{
override
suspend
fun
init
():
Unit
=
coroutineScope
{
this
@QQAndroidBotNetworkHandler
.
subscribeAlways
<
BotOfflineEvent
>
{
this
@QQAndroidBotNetworkHandler
.
subscribeAlways
<
BotOfflineEvent
>
{
if
(
this
@QQAndroidBotNetworkHandler
.
bot
==
this
.
bot
)
{
if
(
this
@QQAndroidBotNetworkHandler
.
bot
==
this
.
bot
)
{
this
.
bot
.
logger
.
error
(
"被挤下线"
)
logger
.
error
(
"被挤下线"
)
close
()
close
()
}
}
}
}
...
@@ -130,7 +131,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -130,7 +131,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
val
friendListJob
=
launch
{
val
friendListJob
=
launch
{
try
{
try
{
bot
.
logger
.
info
(
"开始加载好友信息"
)
logger
.
info
(
"开始加载好友信息"
)
var
currentFriendCount
=
0
var
currentFriendCount
=
0
var
totalFriendCount
:
Short
var
totalFriendCount
:
Short
while
(
true
)
{
while
(
true
)
{
...
@@ -149,21 +150,21 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -149,21 +150,21 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
currentFriendCount
++
currentFriendCount
++
}
}
}
}
bot
.
logger
.
verbose
(
"正在加载好友列表 ${currentFriendCount}/${totalFriendCount}"
)
logger
.
verbose
(
"正在加载好友列表 ${currentFriendCount}/${totalFriendCount}"
)
if
(
currentFriendCount
>=
totalFriendCount
)
{
if
(
currentFriendCount
>=
totalFriendCount
)
{
break
break
}
}
// delay(200)
// delay(200)
}
}
bot
.
logger
.
info
(
"好友列表加载完成, 共 ${currentFriendCount}个"
)
logger
.
info
(
"好友列表加载完成, 共 ${currentFriendCount}个"
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
bot
.
logger
.
error
(
"加载好友列表失败|一般这是由于加载过于频繁导致/将以热加载方式加载好友列表"
)
logger
.
error
(
"加载好友列表失败|一般这是由于加载过于频繁导致/将以热加载方式加载好友列表"
)
}
}
}
}
val
groupJob
=
launch
{
val
groupJob
=
launch
{
try
{
try
{
bot
.
logger
.
info
(
"开始加载群组列表与群成员列表"
)
logger
.
info
(
"开始加载群组列表与群成员列表"
)
val
troopListData
=
FriendList
.
GetTroopListSimplify
(
bot
.
client
)
val
troopListData
=
FriendList
.
GetTroopListSimplify
(
bot
.
client
)
.
sendAndExpect
<
FriendList
.
GetTroopListSimplify
.
Response
>(
retry
=
2
)
.
sendAndExpect
<
FriendList
.
GetTroopListSimplify
.
Response
>(
retry
=
2
)
...
@@ -196,15 +197,15 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -196,15 +197,15 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
)
)
)
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
bot
.
logger
.
error
(
"群${troopNum.groupCode}的列表拉取失败, 一段时间后将会重试"
)
logger
.
error
(
"群${troopNum.groupCode}的列表拉取失败, 一段时间后将会重试"
)
bot
.
logger
.
error
(
e
)
logger
.
error
(
e
)
}
}
}
}
}
}
bot
.
logger
.
info
(
"群组列表与群成员加载完成, 共 ${troopListData.groups.size}个"
)
logger
.
info
(
"群组列表与群成员加载完成, 共 ${troopListData.groups.size}个"
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
bot
.
logger
.
error
(
"加载组信息失败|一般这是由于加载过于频繁导致/将以热加载方式加载群列表"
)
logger
.
error
(
"加载组信息失败|一般这是由于加载过于频繁导致/将以热加载方式加载群列表"
)
bot
.
logger
.
error
(
e
)
logger
.
error
(
e
)
}
}
}
}
...
@@ -317,7 +318,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -317,7 +318,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
if
(
packet
is
CancellableEvent
&&
packet
.
isCancelled
)
return
if
(
packet
is
CancellableEvent
&&
packet
.
isCancelled
)
return
}
}
bot
.
logger
.
info
(
"Received: ${packet.toString().replace("
\
n
", """
\
n
""").replace("
\
r
", "")}"
)
logger
.
info
(
"Received: ${packet.toString().replace("
\
n
", """
\
n
""").replace("
\
r
", "")}"
)
packetFactory
?.
run
{
packetFactory
?.
run
{
when
(
this
)
{
when
(
this
)
{
...
@@ -416,13 +417,13 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -416,13 +417,13 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
bot
.
tryReinitializeNetworkHandler
(
e
)
bot
.
tryReinitializeNetworkHandler
(
e
)
return
return
}
catch
(
e
:
ReadPacketInternalException
)
{
}
catch
(
e
:
ReadPacketInternalException
)
{
bot
.
logger
.
error
(
"Socket channel read failed: ${e.message}"
)
logger
.
error
(
"Socket channel read failed: ${e.message}"
)
bot
.
tryReinitializeNetworkHandler
(
e
)
bot
.
tryReinitializeNetworkHandler
(
e
)
return
return
}
catch
(
e
:
CancellationException
)
{
}
catch
(
e
:
CancellationException
)
{
return
return
}
catch
(
e
:
Throwable
)
{
}
catch
(
e
:
Throwable
)
{
bot
.
logger
.
error
(
"Caught unexpected exceptions"
,
e
)
logger
.
error
(
"Caught unexpected exceptions"
,
e
)
bot
.
tryReinitializeNetworkHandler
(
e
)
bot
.
tryReinitializeNetworkHandler
(
e
)
return
return
}
}
...
@@ -438,7 +439,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -438,7 +439,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
* 发送一个包, 但不期待任何返回.
* 发送一个包, 但不期待任何返回.
*/
*/
suspend
fun
OutgoingPacket
.
sendWithoutExpect
()
{
suspend
fun
OutgoingPacket
.
sendWithoutExpect
()
{
bot
.
logger
.
info
(
"Send: ${this.commandName}"
)
logger
.
info
(
"Send: ${this.commandName}"
)
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
channel
.
send
(
delegate
)
channel
.
send
(
delegate
)
}
}
...
@@ -461,7 +462,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -461,7 +462,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
channel
.
send
(
delegate
)
channel
.
send
(
delegate
)
}
}
bot
.
logger
.
info
(
"Send: ${this.commandName}"
)
logger
.
info
(
"Send: ${this.commandName}"
)
return
withTimeoutOrNull
(
timeoutMillis
)
{
return
withTimeoutOrNull
(
timeoutMillis
)
{
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
)
handler
.
await
()
as
E
handler
.
await
()
as
E
...
@@ -480,7 +481,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
...
@@ -480,7 +481,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
withContext
(
this
@QQAndroidBotNetworkHandler
.
coroutineContext
+
CoroutineName
(
"Packet sender"
))
{
channel
.
send
(
data
,
0
,
length
)
channel
.
send
(
data
,
0
,
length
)
}
}
bot
.
logger
.
info
(
"Send: ${this.commandName}"
)
logger
.
info
(
"Send: ${this.commandName}"
)
return
withTimeoutOrNull
(
timeoutMillis
)
{
return
withTimeoutOrNull
(
timeoutMillis
)
{
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
)
handler
.
await
()
as
E
handler
.
await
()
as
E
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt
View file @
3ab0e675
...
@@ -40,7 +40,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
...
@@ -40,7 +40,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
@UseExperimental
(
RawAccountIdUse
::
class
)
@UseExperimental
(
RawAccountIdUse
::
class
)
override
val
uin
:
Long
override
val
uin
:
Long
get
()
=
account
.
id
get
()
=
account
.
id
final
override
val
logger
:
MiraiLogger
by
lazy
{
configuration
.
logger
?:
DefaultLogger
(
"Bot($uin)"
).
also
{
configuration
.
logger
=
it
}
}
final
override
val
logger
:
MiraiLogger
by
lazy
{
configuration
.
botLoggerSupplier
(
this
)
}
init
{
init
{
instances
.
addLast
(
this
.
weakRef
())
instances
.
addLast
(
this
.
weakRef
())
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
View file @
3ab0e675
...
@@ -17,6 +17,7 @@ import kotlinx.coroutines.CoroutineScope
...
@@ -17,6 +17,7 @@ import kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.Job
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.utils.MiraiInternalAPI
import
net.mamoe.mirai.utils.MiraiInternalAPI
import
net.mamoe.mirai.utils.MiraiLogger
import
net.mamoe.mirai.utils.io.PlatformDatagramChannel
import
net.mamoe.mirai.utils.io.PlatformDatagramChannel
/**
/**
...
@@ -47,6 +48,11 @@ abstract class BotNetworkHandler : CoroutineScope {
...
@@ -47,6 +48,11 @@ abstract class BotNetworkHandler : CoroutineScope {
*/
*/
abstract
val
supervisor
:
CompletableJob
abstract
val
supervisor
:
CompletableJob
/**
* logger
*/
abstract
val
logger
:
MiraiLogger
/**
/**
* 依次尝试登录到可用的服务器. 在任一服务器登录完成后返回.
* 依次尝试登录到可用的服务器. 在任一服务器登录完成后返回.
* 本函数将挂起直到登录成功.
* 本函数将挂起直到登录成功.
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/BotConfiguration.kt
View file @
3ab0e675
...
@@ -11,6 +11,7 @@ package net.mamoe.mirai.utils
...
@@ -11,6 +11,7 @@ package net.mamoe.mirai.utils
import
kotlinx.io.core.IoBuffer
import
kotlinx.io.core.IoBuffer
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.network.BotNetworkHandler
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.jvm.JvmStatic
import
kotlin.jvm.JvmStatic
...
@@ -38,7 +39,11 @@ class BotConfiguration {
...
@@ -38,7 +39,11 @@ class BotConfiguration {
/**
/**
* 日志记录器
* 日志记录器
*/
*/
var
logger
:
MiraiLogger
?
=
null
var
botLoggerSupplier
:
((
Bot
)
->
MiraiLogger
)
=
{
DefaultLogger
(
"Bot(${it.uin})"
)
}
/**
* 网络层日志构造器
*/
var
networkLoggerSupplier
:
((
BotNetworkHandler
)
->
MiraiLogger
)
=
{
DefaultLogger
(
"Network(${it.bot.uin})"
)
}
/**
/**
* 设备信息覆盖
* 设备信息覆盖
*/
*/
...
...
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