diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index 4496ee289..c63ba3972 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -2,6 +2,7 @@ package socket import ( "context" + "encoding/hex" "fmt" "log" "os" @@ -140,7 +141,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) { for _, msg := range messages { //client := conn.RemoteAddr().String() _ = s.workerPool.Submit(func() { - s.parser(c, msg.Payload) + s.handler.Handle(c, msg.Payload) }) } @@ -158,13 +159,14 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) { if err == codec.ErrIncompletePacket && conn.InboundBuffered() > 0 { t, _ := conn.Peek(conn.InboundBuffered()) - cool.Loger.Debug(context.Background(), "断包", err.Error(), conn.InboundBuffered(), t) + 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 { - cool.Loger.Debug(context.Background(), "数据错误", err.Error(), conn.InboundBuffered()) + t, _ := conn.Peek(conn.InboundBuffered()) + cool.Loger.Debug(context.Background(), "数据错误", err.Error(), conn.InboundBuffered(), hex.EncodeToString(t)) action = gnet.Close return } @@ -174,7 +176,8 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) { if data != nil { //client := conn.RemoteAddr().String() _ = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复 - s.parser(conn, data) + //todo 这里待实现注入player实体 + s.handler.Handle(conn, data) }) } @@ -182,12 +185,6 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) { } -func (s *Server) parser(c gnet.Conn, line []byte) { - - //todo 这里待实现注入player实体 - s.handler.Handle(c, line) -} - // CROSS_DOMAIN 定义跨域策略文件内容 const CROSS_DOMAIN = "\x00" diff --git a/common/socket/codec/SocketCodec_Tomee.go b/common/socket/codec/SocketCodec_Tomee.go index 85ba680c6..5dbf9c419 100644 --- a/common/socket/codec/SocketCodec_Tomee.go +++ b/common/socket/codec/SocketCodec_Tomee.go @@ -60,7 +60,6 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) { if bodyLen > maxBodyLen { return nil, errors.New("packet body exceeds max length") } - totalLen := 4 + int(bodyLen) // 检查整个包是否完整 buf, err := c.Peek(int(bodyLen)) @@ -76,7 +75,7 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) { copy(body, buf) // 从缓冲区中丢弃已读取的数据 - _, _ = c.Discard(totalLen) + _, _ = c.Discard(4 + int(bodyLen)) return body, nil }