This commit is contained in:
@@ -2,13 +2,15 @@ package socket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"blazing/common/socket/codec"
|
||||
"blazing/cool"
|
||||
"blazing/logic/service/player"
|
||||
"blazing/modules/config/service"
|
||||
@@ -170,22 +172,43 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
const maxBodyLen = 10 * 1024 // 业务最大包体长度,按需调整
|
||||
func (s *Server) handleTCP(conn gnet.Conn) (action gnet.Action) {
|
||||
|
||||
conn.Context().(*player.ClientData).IsCrossDomain.Do(func() { //跨域检测
|
||||
handle(conn)
|
||||
})
|
||||
|
||||
data, err := s.codec.Decode(conn)
|
||||
if err != nil {
|
||||
if err == codec.ErrIncompletePacket {
|
||||
// handle(c)
|
||||
// 先读取4字节的包长度
|
||||
lenBuf, err := conn.Peek(4)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, io.ErrShortBuffer) {
|
||||
return
|
||||
}
|
||||
return gnet.Close
|
||||
|
||||
}
|
||||
s.onevent(conn, data)
|
||||
|
||||
bodyLen := binary.BigEndian.Uint32(lenBuf)
|
||||
|
||||
if bodyLen > maxBodyLen {
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
if conn.InboundBuffered() < int(bodyLen) {
|
||||
return
|
||||
}
|
||||
// 提取包体
|
||||
body, err := conn.Next(int(bodyLen))
|
||||
if err != nil {
|
||||
if errors.Is(err, io.ErrShortBuffer) {
|
||||
return
|
||||
}
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
s.onevent(conn, body)
|
||||
|
||||
if conn.InboundBuffered() > 0 {
|
||||
if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data
|
||||
|
||||
Reference in New Issue
Block a user