```
feat(socket): 优化服务器事件处理逻辑并修复数据解码问题 - 移除重复的 `gnet.WithTicker(true)` 配置项 - 调整 `OnTick` 的执行间隔从 10 秒延长至 30 秒 - 更新 `NewClientData` 方法以传入连接对象,用于后续消息处理 - 将 `c.Read` 替换为 `c.Discard` 以正确丢弃已读数据 - 改进数据包处理逻辑,增强对不完整包的处理能力 - 修正 `TomeeHeader.Version` 类型由 string 转为 byte,并更新相关读写操作 - 在消息处理中增加错误日志打印
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"blazing/logic/service/player"
|
||||
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
)
|
||||
|
||||
func (s *Server) Boot() error {
|
||||
@@ -22,7 +21,6 @@ func (s *Server) Boot() error {
|
||||
err := gnet.Run(s, s.network+"://"+s.addr,
|
||||
gnet.WithMulticore(true),
|
||||
gnet.WithTicker(true),
|
||||
gnet.WithTicker(true),
|
||||
|
||||
// gnet.WithReusePort(true),
|
||||
// gnet.WithReuseAddr(true),
|
||||
@@ -76,7 +74,7 @@ func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
|
||||
//执行正常退出逻辑
|
||||
os.Exit(0)
|
||||
}
|
||||
return 10 * time.Second, gnet.None
|
||||
return 30 * time.Second, gnet.None
|
||||
}
|
||||
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
||||
s.eng = eng
|
||||
@@ -88,7 +86,7 @@ func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
||||
|
||||
func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
|
||||
if conn.Context() == nil {
|
||||
conn.SetContext(player.NewClientData()) //注入data
|
||||
conn.SetContext(player.NewClientData(conn)) //注入data
|
||||
}
|
||||
|
||||
atomic.AddInt64(&s.connected, 1)
|
||||
@@ -129,7 +127,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
|
||||
}
|
||||
// fmt.Println(ws.Buf.Bytes())
|
||||
c.Read(make([]byte, len1))
|
||||
c.Discard(len1)
|
||||
if ws.Buf.Len() <= 0 {
|
||||
return gnet.None
|
||||
}
|
||||
@@ -144,14 +142,16 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
for _, msg := range messages {
|
||||
t := c.Context().(*player.ClientData)
|
||||
//client := conn.RemoteAddr().String()
|
||||
_ = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
|
||||
t.Lf.Producer().Write(&player.RecvData{
|
||||
|
||||
Conn: c,
|
||||
Data: msg.Payload,
|
||||
})
|
||||
err = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
|
||||
err = t.Lf.Producer().Write(msg.Payload)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("work2", err)
|
||||
}
|
||||
}
|
||||
|
||||
return gnet.None
|
||||
@@ -159,42 +159,41 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
|
||||
func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) {
|
||||
|
||||
if s.discorse && !conn.Context().(*player.ClientData).IsCrossDomain {
|
||||
handle(conn)
|
||||
}
|
||||
conn.Context().(*player.ClientData).IsCrossDomain = true
|
||||
data, err := s.codec.Decode(conn)
|
||||
if err != nil {
|
||||
for {
|
||||
if s.discorse && !conn.Context().(*player.ClientData).IsCrossDomain {
|
||||
handle(conn)
|
||||
}
|
||||
conn.Context().(*player.ClientData).IsCrossDomain = true
|
||||
data, err := s.codec.Decode(conn)
|
||||
|
||||
if err == codec.ErrIncompletePacket {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
return gnet.Close
|
||||
|
||||
if err == codec.ErrIncompletePacket && conn.InboundBuffered() > 0 {
|
||||
t, _ := conn.Peek(conn.InboundBuffered())
|
||||
cool.Loger.Debug(context.Background(), "断包", err.Error(), conn.InboundBuffered(), hex.EncodeToString(t))
|
||||
if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data
|
||||
logging.Errorf("failed to wake up the connection, %v", err)
|
||||
return gnet.Close
|
||||
}
|
||||
} else {
|
||||
t, _ := conn.Peek(conn.InboundBuffered())
|
||||
cool.Loger.Debug(context.Background(), "数据错误", err.Error(), conn.InboundBuffered(), hex.EncodeToString(t))
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
cool.Loger.Debug(context.Background(), "原始数据", hex.EncodeToString(data))
|
||||
t := conn.Context().(*player.ClientData)
|
||||
|
||||
_ = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
|
||||
t.Lf.Producer().Write(&player.RecvData{
|
||||
|
||||
Conn: conn,
|
||||
Data: data,
|
||||
})
|
||||
err = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
|
||||
err = t.Lf.Producer().Write(data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
if conn.InboundBuffered() > 0 {
|
||||
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 action
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -58,6 +60,7 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) {
|
||||
}
|
||||
|
||||
bodyLen := binary.BigEndian.Uint32(lenBuf)
|
||||
cool.Loger.Print(context.TODO(), "lenBuf", bodyLen)
|
||||
if bodyLen > maxBodyLen {
|
||||
return nil, errors.New("packet body exceeds max length")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user