```
fix(socket): 调整连接读超时与数据包解析逻辑 - 在 `OnOpen` 中为连接设置 3 秒读超时 - 简化 `handleTcp` 中不完整数据包的处理流程,移除手动唤醒逻辑 - 修改 `SocketCodec_Tomee.go` 中最大包体长度限制从 10MB 降至 10KB - 修复 `Decode` 方法中未处理 `c.Next` 错误的问题 - 更新 .gitignore 忽略重复的 logic/logic1 文件路径 ```
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -38,3 +38,4 @@ logic/logic2
|
||||
login/login
|
||||
login/login
|
||||
logic/logic1
|
||||
logic/logic1
|
||||
|
||||
@@ -83,6 +83,7 @@ func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
|
||||
if s.network != "tcp" {
|
||||
return nil, gnet.Close
|
||||
}
|
||||
conn.SetReadDeadline(<-time.After(3000))
|
||||
|
||||
if conn.Context() == nil {
|
||||
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)
|
||||
if err != nil {
|
||||
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 gnet.Close
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
return gnet.Close
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
var ErrIncompletePacket = errors.New("incomplete packet")
|
||||
|
||||
const maxBodyLen = 10 * 1024 * 1024 // 业务最大包体长度,按需调整
|
||||
const maxBodyLen = 10 * 1024 // 业务最大包体长度,按需调整
|
||||
// TomeeSocketCodec 协议格式:
|
||||
//
|
||||
// * 0 4
|
||||
@@ -67,7 +67,7 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) {
|
||||
return nil, ErrIncompletePacket
|
||||
}
|
||||
// 提取包体
|
||||
body, _ := c.Next(int(bodyLen))
|
||||
body, err := c.Next(int(bodyLen))
|
||||
|
||||
return body, nil
|
||||
return body, err
|
||||
}
|
||||
|
||||
BIN
logic/logic1
BIN
logic/logic1
Binary file not shown.
Reference in New Issue
Block a user