2026-03-31 08:19:53 +08:00
|
|
|
|
package socket
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-06 06:33:24 +08:00
|
|
|
|
"blazing/common/socket/codec"
|
|
|
|
|
|
"blazing/cool"
|
|
|
|
|
|
"blazing/logic/service/player"
|
|
|
|
|
|
"blazing/modules/config/service"
|
|
|
|
|
|
"bytes"
|
2026-03-31 08:19:53 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
|
|
"github.com/panjf2000/gnet/v2"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
const (
|
|
|
|
|
|
minPacketLen = 17
|
|
|
|
|
|
maxPacketLen = 10 * 1024
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
func (s *Server) Boot(serverid, port uint32) error {
|
|
|
|
|
|
// go s.bootws()
|
|
|
|
|
|
s.serverid = serverid
|
|
|
|
|
|
s.port = port
|
|
|
|
|
|
|
|
|
|
|
|
err := gnet.Run(s, s.network+"://"+s.addr,
|
|
|
|
|
|
gnet.WithMulticore(true),
|
|
|
|
|
|
gnet.WithTicker(true),
|
|
|
|
|
|
|
|
|
|
|
|
// 其他调优配置↓
|
|
|
|
|
|
gnet.WithTCPNoDelay(gnet.TCPNoDelay), // 禁用Nagle算法(降低延迟,适合小数据包场景)
|
|
|
|
|
|
//gnet.WithReusePort(true), // 开启SO_REUSEPORT(多核下提升并发)
|
|
|
|
|
|
//gnet.WithReadBufferCap(1024*64), // 读缓冲区64KB(根据业务调整,默认太小)
|
|
|
|
|
|
// gnet.WithWriteBufferCap(1024*64), // 写缓冲区64KB
|
|
|
|
|
|
|
|
|
|
|
|
//gnet.WithLockOSThread(true), // 绑定goroutine到OS线程(减少上下文切换)
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
|
|
_ = s.eng.Stop(context.Background())
|
|
|
|
|
|
s.workerPool.Release()
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
if t, ok := c.Context().(*player.ClientData); ok {
|
|
|
|
|
|
if t.Player != nil {
|
|
|
|
|
|
if t.Player.Info != nil {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "OnClose 错误:", cool.Config.ServerInfo.OnlineID, t.Player.Info.UserID, err)
|
2026-04-05 11:14:25 +08:00
|
|
|
|
go t.Player.SaveOnDisconnect()
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "OnClose 错误:", cool.Config.ServerInfo.OnlineID, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
atomic.AddInt64(&cool.Connected, -1)
|
|
|
|
|
|
|
|
|
|
|
|
v, _ := c.Context().(*player.ClientData)
|
2026-04-05 21:59:22 +08:00
|
|
|
|
if v != nil {
|
|
|
|
|
|
v.Close()
|
|
|
|
|
|
if v.Player != nil {
|
|
|
|
|
|
v.Player.Save() //保存玩家数据
|
|
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
|
|
|
|
|
|
g.Log().Async().Info(context.Background(), gtime.Now().ISO8601(), "服务器ID", cool.Config.ServerInfo.OnlineID, "链接数", atomic.LoadInt64(&cool.Connected))
|
|
|
|
|
|
if s.quit && atomic.LoadInt64(&cool.Connected) == 0 {
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
}
|
|
|
|
|
|
return 30 * time.Second, gnet.None
|
|
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
|
|
|
|
|
s.eng = eng
|
2026-04-06 06:33:24 +08:00
|
|
|
|
service.NewServerService().SetServerID(s.serverid, s.port)
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
|
|
|
|
|
|
if s.network != "tcp" {
|
|
|
|
|
|
return nil, gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
if conn.Context() == nil {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
conn.SetContext(player.NewClientData(conn))
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
atomic.AddInt64(&cool.Connected, 1)
|
|
|
|
|
|
return nil, gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
|
|
|
|
|
defer func() {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if err := recover(); err != nil {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if t, ok := c.Context().(*player.ClientData); ok {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if t.Player != nil && t.Player.Info != nil {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "OnTraffic 错误:", cool.Config.ServerInfo.OnlineID, t.Player.Info.UserID, err)
|
|
|
|
|
|
t.Player.Service.Info.Save(*t.Player.Info)
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2026-04-15 03:22:59 +08:00
|
|
|
|
client := c.Context().(*player.ClientData)
|
|
|
|
|
|
if s.discorse && !client.IsCrossDomainChecked() {
|
|
|
|
|
|
handled, ready, action := handle(c)
|
|
|
|
|
|
if action != gnet.None {
|
|
|
|
|
|
return action
|
|
|
|
|
|
}
|
|
|
|
|
|
if handled {
|
|
|
|
|
|
client.MarkCrossDomainChecked()
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
if !ready {
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
client.MarkCrossDomainChecked()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ws := client.Wsmsg
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if ws.Tcp {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return s.handleTCP(c)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
readAction, inboundLen := ws.ReadBufferBytes(c)
|
|
|
|
|
|
if readAction == gnet.Close {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
state, action := ws.Upgrade(c)
|
|
|
|
|
|
if action != gnet.None {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return action
|
|
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if state == player.UpgradeNeedMoreData {
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
if state == player.UpgradeUseTCP {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return s.handleTCP(c)
|
2026-04-06 06:33:24 +08:00
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if inboundLen > 0 {
|
|
|
|
|
|
if _, err := c.Discard(inboundLen); err != nil {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
ws.ResetInboundMirror()
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messages, err := ws.Decode(c)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
if messages == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, msg := range messages {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if !s.onevent(c, msg.Payload) {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) handleTCP(conn gnet.Conn) (action gnet.Action) {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
client := conn.Context().(*player.ClientData)
|
|
|
|
|
|
if s.discorse && !client.IsCrossDomainChecked() {
|
|
|
|
|
|
handled, ready, action := handle(conn)
|
|
|
|
|
|
if action != gnet.None {
|
|
|
|
|
|
return action
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if !ready {
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
if handled {
|
|
|
|
|
|
client.MarkCrossDomainChecked()
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
client.MarkCrossDomainChecked()
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
body, err := s.codec.Decode(conn)
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if err != nil {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if errors.Is(err, codec.ErrIncompletePacket) {
|
|
|
|
|
|
return gnet.None
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if !s.onevent(conn, body) {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if conn.InboundBuffered() > 0 {
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if err := conn.Wake(nil); err != nil {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return action
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const CROSS_DOMAIN = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\x00"
|
|
|
|
|
|
const TEXT = "<policy-file-request/>\x00"
|
|
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
func handle(c gnet.Conn) (handled bool, ready bool, action gnet.Action) {
|
|
|
|
|
|
probeLen := c.InboundBuffered()
|
|
|
|
|
|
if probeLen == 0 {
|
|
|
|
|
|
return false, false, gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
if probeLen > len(TEXT) {
|
|
|
|
|
|
probeLen = len(TEXT)
|
|
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
data, err := c.Peek(probeLen)
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("Error reading cross-domain request: %v", err)
|
2026-04-06 06:33:24 +08:00
|
|
|
|
return false, false, gnet.Close
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if !bytes.Equal(data, []byte(TEXT[:probeLen])) {
|
|
|
|
|
|
return false, true, gnet.None
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
if probeLen < len(TEXT) {
|
|
|
|
|
|
return false, false, gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
if _, err := c.Write([]byte(CROSS_DOMAIN)); err != nil {
|
|
|
|
|
|
return false, true, gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
if _, err := c.Discard(len(TEXT)); err != nil {
|
|
|
|
|
|
return false, true, gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
return true, true, gnet.None
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 06:33:24 +08:00
|
|
|
|
func (s *Server) onevent(c gnet.Conn, v []byte) bool {
|
|
|
|
|
|
if !isValidPacket(v) {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if t, ok := c.Context().(*player.ClientData); ok {
|
|
|
|
|
|
t.PushEvent(v, s.workerPool.Submit)
|
|
|
|
|
|
}
|
2026-04-06 06:33:24 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isValidPacket(v []byte) bool {
|
|
|
|
|
|
if len(v) < minPacketLen || len(v) > maxPacketLen {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return binary.BigEndian.Uint32(v[0:4]) == uint32(len(v))
|
2026-03-31 08:19:53 +08:00
|
|
|
|
}
|