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