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
2bfffa7b
Commit
2bfffa7b
authored
May 21, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix network UnknownHostException handling
parent
ae8d6e0c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
10 deletions
+18
-10
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/BotImpl.kt
...rc/commonMain/kotlin/net/mamoe/mirai/qqandroid/BotImpl.kt
+3
-1
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
...moe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
+11
-7
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/PlatformSocket.kt
.../kotlin/net/mamoe/mirai/qqandroid/utils/PlatformSocket.kt
+2
-1
mirai-core-qqandroid/src/jvmMain/kotlin/net/mamoe/mirai/qqandroid/utils/PlatformSocket.kt
.../kotlin/net/mamoe/mirai/qqandroid/utils/PlatformSocket.kt
+2
-1
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/BotImpl.kt
View file @
2bfffa7b
...
...
@@ -215,7 +215,9 @@ internal abstract class BotImpl<N : BotNetworkHandler> constructor(
init
{
coroutineContext
[
Job
]
!!
.
invokeOnCompletion
{
throwable
->
network
.
close
(
throwable
)
kotlin
.
runCatching
{
network
.
close
(
throwable
)
}
offlineListener
.
cancel
(
CancellationException
(
"Bot cancelled"
,
throwable
))
// help GC release instances
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt
View file @
2bfffa7b
...
...
@@ -38,12 +38,9 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.login.ConfigPushSvc
import
net.mamoe.mirai.qqandroid.network.protocol.packet.login.Heartbeat
import
net.mamoe.mirai.qqandroid.network.protocol.packet.login.StatSvc
import
net.mamoe.mirai.qqandroid.network.protocol.packet.login.WtLogin
import
net.mamoe.mirai.qqandroid.utils.NoRouteToHostException
import
net.mamoe.mirai.qqandroid.utils.PlatformSocket
import
net.mamoe.mirai.qqandroid.utils.SocketException
import
net.mamoe.mirai.qqandroid.utils.*
import
net.mamoe.mirai.qqandroid.utils.io.readPacketExact
import
net.mamoe.mirai.qqandroid.utils.io.useBytes
import
net.mamoe.mirai.qqandroid.utils.retryCatching
import
net.mamoe.mirai.utils.*
import
kotlin.coroutines.CoroutineContext
import
kotlin.jvm.JvmField
...
...
@@ -137,6 +134,13 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
}
else
{
throw
e
}
}
catch
(
e
:
UnknownHostException
)
{
if
(
e
is
NoRouteToHostException
||
e
.
message
?.
contains
(
"Network is unreachable"
)
==
true
)
{
logger
.
warning
{
"No route to host (Mostly due to no Internet connection). Retrying in 3s..."
}
delay
(
3000
)
}
else
{
throw
e
}
}
}
logger
.
info
{
"Connected to server $host:$port"
}
...
...
@@ -627,6 +631,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
check
(
bot
.
isActive
)
{
"bot is dead therefore can't send ${this.commandName}"
}
check
(
this
@QQAndroidBotNetworkHandler
.
isActive
)
{
"network is dead therefore can't send any packet"
}
check
(
channel
.
isOpen
)
{
"network channel is closed"
}
suspend
fun
doSendAndReceive
(
handler
:
PacketListener
,
data
:
Any
,
length
:
Int
):
E
{
when
(
data
)
{
...
...
@@ -637,10 +642,9 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
logger
.
verbose
{
"Send: $commandName"
}
@Suppress
(
"UNCHECKED_CAST"
)
return
withTimeout
OrNull
(
timeoutMillis
)
{
return
withTimeout
(
timeoutMillis
)
{
handler
.
await
()
// 不要 `withTimeout`. timeout 的报错会不正常.
}
as
E
?
?:
throw
TimeoutException
(
"timeout receiving response of $commandName"
)
}
as
E
}
if
(
retry
==
0
)
{
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/PlatformSocket.kt
View file @
2bfffa7b
...
...
@@ -43,4 +43,5 @@ internal expect class PlatformSocket() : Closeable {
}
expect
open
class
SocketException
:
IOException
expect
class
NoRouteToHostException
:
SocketException
\ No newline at end of file
expect
class
NoRouteToHostException
:
SocketException
expect
class
UnknownHostException
:
IOException
\ No newline at end of file
mirai-core-qqandroid/src/jvmMain/kotlin/net/mamoe/mirai/qqandroid/utils/PlatformSocket.kt
View file @
2bfffa7b
...
...
@@ -201,4 +201,5 @@ internal actual class PlatformSocket : Closeable {
actual typealias NoRouteToHostException = java.net.NoRouteToHostException
actual typealias SocketException = SocketException
*/
\ No newline at end of file
*/
actual
typealias
UnknownHostException
=
java
.
net
.
UnknownHostException
\ No newline at end of file
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