refactor(socket): 优化TCP连接处理逻辑,新增错误连接计数并调整返回值处理

This commit is contained in:
1
2025-09-27 17:35:12 +00:00
parent d7712ef70f
commit 290e678559
2 changed files with 9 additions and 8 deletions

View File

@@ -84,8 +84,8 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
ws := c.Context().(*player.ClientData).Wsmsg
if ws.Tcp { //升级失败时候防止缓冲区溢出
s.handleTcp(c)
return gnet.None
FF := s.handleTcp(c)
return FF
}
tt, len1 := ws.ReadBufferBytes(c)
@@ -97,7 +97,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
ok, action := ws.Upgrade(c)
if !ok { //升级失败,说明是tcp连接
s.handleTcp(c)
return gnet.None
return action
} else {
// fmt.Println(ws.Buf.Bytes())
c.Read(make([]byte, len1))
@@ -132,6 +132,10 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) {
}
data, err := s.codec.Decode(conn)
if err != nil {
if conn.Context().(*player.ClientData).ERROR_CONNUT > 5 {
action = gnet.Close
}
conn.Context().(*player.ClientData).ERROR_CONNUT += 1
break
}
@@ -140,7 +144,7 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) {
s.parser(conn, data)
})
return gnet.None
return
}
if conn.InboundBuffered() > 0 {

View File

@@ -41,7 +41,7 @@ type ClientData struct {
IsCrossDomain bool //是否跨域过
Player *Player //客户实体
//UserID uint32
ERROR_CONNUT int
Wsmsg *WsCodec
}
@@ -252,8 +252,6 @@ func (p *Player) SendPack(b []byte) error {
return err
}
func (p *Player) Cheak(b error) {
if b != nil {
g.Log().Error(context.Background(), "出现错误", p.Info.UserID, b.Error())
@@ -261,7 +259,6 @@ func (p *Player) Cheak(b error) {
}
func LeaveMap(c common.PlayerI) {
t := NewTomeeHeader(2002, c.GetInfo().UserID)