Open
Conversation
Collaborator
Author
|
@copilot 用中文 Review,画出 PacketContext.kt 在修改前后的状态机,解释关键区别。 |
PacketContext.kt 修改前后状态机分析修改前(旧逻辑)关键问题:
修改后(新逻辑)引入两个原子标志位:
关键区别1.
|
| 旧逻辑 | 新逻辑 | |
|---|---|---|
| 服务端关闭连接 | break → 永久退出,不重连 |
进入 RECOVERING → 自动重连 |
这是最核心的 bug 修复:原来服务端主动断开时客户端不会重连。
2. 新增 manualCloseRequested 区分断开意图
旧逻辑无法区分"用户主动调用 closeConnection()"与"网络异常断开"。新逻辑通过 manualCloseRequested = true 标记主动断开,异常处理时检查该标志决定是否重连。
3. reconnectRequested 防止重复触发重连
requestReconnect() 使用 compareAndSet(false → true) 保证原子性,即使 sendPacket、心跳包、接收循环同时失败,也只会触发一次重连流程。
4. sendPacket 主动触发重连
旧逻辑中 sendPacket 失败只会抛出异常,不会触发重连。新逻辑在发送包失败(IO 异常)或等待响应超时(仅限 Alive/Heartbeat 命令)时,主动调用 requestReconnect() 关闭 transport,让接收循环自然感知到连接断开并触发完整重连流程。
5. recoverConnection() 统一重连入口
旧逻辑重连步骤散落在 catch 块内;新逻辑将 cleanupPending + doPreOfflineLogic + closeConnection + delay + connect 统一封装进 recoverConnection(),无论是接收循环失败还是发送失败,都走同一个恢复路径。
6. 心跳包感知重连状态
旧逻辑心跳包失败仅打日志,不影响重连。新逻辑在心跳失败后检查 reconnectRequested.value,若已在重连中则退出心跳循环,避免重连期间心跳继续累积错误。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.