fix(socket): 调整连接读超时与数据包解析逻辑

- 在 `OnOpen` 中为连接设置 3 秒读超时
- 简化 `handleTcp` 中不完整数据包的处理流程,移除手动唤醒逻辑
- 修改 `SocketCodec_Tomee.go` 中最大包体长度限制从 10MB 降至 10KB
- 修复 `Decode` 方法中未处理 `c.Next` 错误的问题
- 更新 .gitignore 忽略重复的 logic/logic1 文件路径
```
This commit is contained in:
2025-11-03 05:46:13 +08:00
parent f9279e75fa
commit 9a802ce948
4 changed files with 7 additions and 7 deletions

1
.gitignore vendored
View File

@@ -38,3 +38,4 @@ logic/logic2
login/login login/login
login/login login/login
logic/logic1 logic/logic1
logic/logic1

View File

@@ -83,6 +83,7 @@ func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
if s.network != "tcp" { if s.network != "tcp" {
return nil, gnet.Close return nil, gnet.Close
} }
conn.SetReadDeadline(<-time.After(3000))
if conn.Context() == nil { if conn.Context() == nil {
conn.SetContext(player.NewClientData(conn)) //注入data conn.SetContext(player.NewClientData(conn)) //注入data
@@ -156,10 +157,8 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) {
data, err := s.codec.Decode(conn) data, err := s.codec.Decode(conn)
if err != nil { if err != nil {
if err == codec.ErrIncompletePacket { if err == codec.ErrIncompletePacket {
if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data
cool.Loger.Errorf(context.Background(), "failed to wake up the connection, %v", err) return
return gnet.Close
}
} }
return gnet.Close return gnet.Close

View File

@@ -10,7 +10,7 @@ import (
var ErrIncompletePacket = errors.New("incomplete packet") var ErrIncompletePacket = errors.New("incomplete packet")
const maxBodyLen = 10 * 1024 * 1024 // 业务最大包体长度,按需调整 const maxBodyLen = 10 * 1024 // 业务最大包体长度,按需调整
// TomeeSocketCodec 协议格式: // TomeeSocketCodec 协议格式:
// //
// * 0 4 // * 0 4
@@ -67,7 +67,7 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) {
return nil, ErrIncompletePacket return nil, ErrIncompletePacket
} }
// 提取包体 // 提取包体
body, _ := c.Next(int(bodyLen)) body, err := c.Next(int(bodyLen))
return body, nil return body, err
} }

Binary file not shown.