2025-06-23 10:15:22 +08:00
|
|
|
|
package socket
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2025-06-24 22:09:05 +08:00
|
|
|
|
"log"
|
2025-10-10 23:59:54 +08:00
|
|
|
|
"os"
|
2025-07-07 19:58:23 +08:00
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
"time"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2025-10-16 23:10:37 +00:00
|
|
|
|
"blazing/common/socket/codec"
|
2025-10-05 02:00:00 +00:00
|
|
|
|
"blazing/cool"
|
2025-09-14 01:35:16 +08:00
|
|
|
|
"blazing/logic/service/player"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2025-12-11 20:27:10 +00:00
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
"github.com/panjf2000/gnet/v2"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) Boot() error {
|
2025-07-11 21:04:28 +08:00
|
|
|
|
// go s.bootws()
|
2025-07-07 19:58:23 +08:00
|
|
|
|
err := gnet.Run(s, s.network+"://"+s.addr,
|
2025-06-23 10:15:22 +08:00
|
|
|
|
gnet.WithMulticore(true),
|
2025-07-07 19:58:23 +08:00
|
|
|
|
gnet.WithTicker(true),
|
2025-10-27 20:14:24 +08:00
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
// gnet.WithReusePort(true),
|
|
|
|
|
|
// gnet.WithReuseAddr(true),
|
2025-06-23 10:15:22 +08:00
|
|
|
|
gnet.WithSocketRecvBuffer(s.bufferSize))
|
|
|
|
|
|
if err != nil {
|
2025-11-01 00:40:19 +08:00
|
|
|
|
panic(err)
|
2025-06-23 10:15:22 +08:00
|
|
|
|
}
|
2025-10-05 02:00:00 +00:00
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
|
|
_ = s.eng.Stop(context.Background())
|
|
|
|
|
|
s.workerPool.Release()
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-09 08:51:29 +00:00
|
|
|
|
func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
2025-10-10 02:23:29 +00:00
|
|
|
|
defer func() {
|
2025-10-28 07:39:11 +00:00
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
// 1. 打印错误信息
|
2025-10-29 15:50:48 +00:00
|
|
|
|
|
|
|
|
|
|
cool.Loger.Error(context.TODO(), "panic 错误:", err)
|
|
|
|
|
|
|
2025-10-10 02:23:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
2025-11-09 08:51:29 +00:00
|
|
|
|
// 识别 RST 导致的连接中断(错误信息含 "connection reset")
|
2025-11-13 05:05:05 +08:00
|
|
|
|
// if err != nil && (strings.Contains(err.Error(), "connection reset") || strings.Contains(err.Error(), "reset by peer")) {
|
|
|
|
|
|
// remoteIP := c.RemoteAddr().(*net.TCPAddr).IP.String()
|
2025-10-28 07:39:11 +00:00
|
|
|
|
|
2025-11-13 05:05:05 +08:00
|
|
|
|
// log.Printf("RST 攻击检测: 来源 %s, 累计攻击次数 %d", remoteIP)
|
2025-11-09 08:51:29 +00:00
|
|
|
|
|
2025-11-13 05:05:05 +08:00
|
|
|
|
// // 防护逻辑:临时封禁异常 IP(可扩展为 IP 黑名单)
|
|
|
|
|
|
// // go s.tempBlockIP(remoteIP, 5*time.Minute)
|
|
|
|
|
|
// }
|
2025-11-13 02:43:00 +08:00
|
|
|
|
//fmt.Println(err, c.RemoteAddr().String(), "断开连接")
|
2025-07-07 19:58:23 +08:00
|
|
|
|
atomic.AddInt64(&s.connected, -1)
|
2025-11-05 22:17:03 +00:00
|
|
|
|
|
2025-07-07 19:58:23 +08:00
|
|
|
|
//logging.Infof("conn[%v] disconnected", c.RemoteAddr().String())
|
2025-10-27 09:36:49 +00:00
|
|
|
|
v, _ := c.Context().(*player.ClientData)
|
2025-11-05 22:17:03 +00:00
|
|
|
|
v.LF.Close()
|
2025-11-01 14:31:19 +08:00
|
|
|
|
if v.Player != nil {
|
|
|
|
|
|
v.SaveL.Do(func() { //使用保存锁,确保在踢人和掉线的时候只保存一次
|
|
|
|
|
|
//cool.Loger.Info(context.TODO(), "准备保存", v.Player.Info.UserID)
|
|
|
|
|
|
v.Player.Save() //保存玩家数据
|
2025-10-28 23:56:15 +08:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
})
|
2025-10-16 18:59:38 +00:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
}
|
2025-07-06 22:58:39 +08:00
|
|
|
|
|
|
|
|
|
|
//}
|
2025-07-02 23:29:30 +08:00
|
|
|
|
//关闭连接
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-07-07 19:58:23 +08:00
|
|
|
|
func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
|
2025-12-13 18:35:17 +08:00
|
|
|
|
glog.Info(context.Background(), gtime.Now().ISO8601(), "链接数", atomic.LoadInt64(&s.connected))
|
2025-10-28 07:39:11 +00:00
|
|
|
|
if s.quit && atomic.LoadInt64(&s.connected) == 0 {
|
2025-10-10 23:59:54 +08:00
|
|
|
|
//执行正常退出逻辑
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
}
|
2025-10-28 02:28:15 +08:00
|
|
|
|
return 30 * time.Second, gnet.None
|
2025-07-07 19:58:23 +08:00
|
|
|
|
}
|
2025-06-23 10:15:22 +08:00
|
|
|
|
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
|
|
|
|
|
s.eng = eng
|
|
|
|
|
|
|
2025-10-08 17:16:54 +08:00
|
|
|
|
// cool.Loger.Infof(context.Background(), " server is listening on %s\n", s.addr)
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
|
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
|
2025-11-01 14:31:19 +08:00
|
|
|
|
if s.network != "tcp" {
|
|
|
|
|
|
return nil, gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
if conn.Context() == nil {
|
2025-10-28 02:28:15 +08:00
|
|
|
|
conn.SetContext(player.NewClientData(conn)) //注入data
|
2025-07-11 21:04:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 19:58:23 +08:00
|
|
|
|
atomic.AddInt64(&s.connected, 1)
|
|
|
|
|
|
|
|
|
|
|
|
return nil, gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
2025-10-10 00:40:32 +08:00
|
|
|
|
defer func() {
|
2025-10-28 07:39:11 +00:00
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
// 1. 打印错误信息
|
2025-10-29 15:50:48 +00:00
|
|
|
|
|
|
|
|
|
|
cool.Loger.Error(context.TODO(), "panic 错误:", err)
|
|
|
|
|
|
|
2025-10-10 00:40:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
2025-09-14 01:35:16 +08:00
|
|
|
|
ws := c.Context().(*player.ClientData).Wsmsg
|
2025-09-13 00:42:39 +08:00
|
|
|
|
if ws.Tcp { //升级失败时候防止缓冲区溢出
|
2025-10-05 01:47:45 +00:00
|
|
|
|
return s.handleTcp(c)
|
|
|
|
|
|
|
2025-09-13 00:42:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
tt, len1 := ws.ReadBufferBytes(c)
|
|
|
|
|
|
if tt == gnet.Close {
|
|
|
|
|
|
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ok, action := ws.Upgrade(c)
|
2025-10-05 01:47:45 +00:00
|
|
|
|
if action != gnet.None { //连接断开
|
2025-09-27 17:35:12 +00:00
|
|
|
|
return action
|
2025-07-11 21:04:28 +08:00
|
|
|
|
}
|
2025-10-05 01:47:45 +00:00
|
|
|
|
if !ok { //升级失败,说明是tcp连接
|
2025-10-17 10:47:17 +08:00
|
|
|
|
ws.Tcp = true
|
|
|
|
|
|
|
2025-10-05 01:47:45 +00:00
|
|
|
|
return s.handleTcp(c)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// fmt.Println(ws.Buf.Bytes())
|
2025-10-28 02:28:15 +08:00
|
|
|
|
c.Discard(len1)
|
2025-11-01 14:31:19 +08:00
|
|
|
|
|
2025-07-11 21:04:28 +08:00
|
|
|
|
messages, err := ws.Decode(c)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
if messages == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-07-11 21:28:38 +08:00
|
|
|
|
|
2025-11-03 03:59:59 +08:00
|
|
|
|
t := c.Context().(*player.ClientData)
|
|
|
|
|
|
//client := conn.RemoteAddr().String()
|
|
|
|
|
|
s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
|
|
|
|
|
|
for _, msg := range messages {
|
2025-11-05 22:17:03 +00:00
|
|
|
|
t.LF.Producer().Write(msg.Payload)
|
|
|
|
|
|
//t.OnEvent(msg.Payload)
|
2025-11-03 03:59:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-10-15 22:53:14 +00:00
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
|
return gnet.None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) {
|
|
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
conn.Context().(*player.ClientData).IsCrossDomain.Do(func() { //跨域检测
|
|
|
|
|
|
handle(conn)
|
|
|
|
|
|
})
|
2025-10-17 13:50:29 +00:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
data, err := s.codec.Decode(conn)
|
|
|
|
|
|
if err != nil {
|
2025-10-28 02:28:15 +08:00
|
|
|
|
if err == codec.ErrIncompletePacket {
|
2025-11-03 05:46:13 +08:00
|
|
|
|
|
|
|
|
|
|
return
|
2025-10-28 02:28:15 +08:00
|
|
|
|
}
|
2025-11-01 14:31:19 +08:00
|
|
|
|
return gnet.Close
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
}
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
|
2025-11-05 22:17:03 +00:00
|
|
|
|
//conn.Context().(*player.ClientData).OnEvent(data)
|
2025-11-08 00:47:45 +08:00
|
|
|
|
if t, ok := conn.Context().(*player.ClientData); ok {
|
|
|
|
|
|
t.LF.Producer().Write(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
})
|
2025-10-15 22:53:14 +00:00
|
|
|
|
|
2025-10-28 02:28:15 +08:00
|
|
|
|
if conn.InboundBuffered() > 0 {
|
|
|
|
|
|
if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data
|
|
|
|
|
|
cool.Loger.Errorf(context.Background(), "failed to wake up the connection, %v", err)
|
|
|
|
|
|
return gnet.Close
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-15 22:53:14 +00:00
|
|
|
|
return action
|
2025-06-23 10:15:22 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-24 22:09:05 +08:00
|
|
|
|
// CROSS_DOMAIN 定义跨域策略文件内容
|
|
|
|
|
|
const CROSS_DOMAIN = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\x00"
|
|
|
|
|
|
|
|
|
|
|
|
// TEXT 定义跨域请求的文本格式
|
|
|
|
|
|
const TEXT = "<policy-file-request/>\x00"
|
|
|
|
|
|
|
|
|
|
|
|
func handle(c gnet.Conn) {
|
|
|
|
|
|
|
|
|
|
|
|
// 读取数据并检查是否为跨域请求
|
|
|
|
|
|
data, err := c.Peek(len(TEXT))
|
|
|
|
|
|
if err != nil {
|
2025-10-15 22:53:14 +00:00
|
|
|
|
log.Printf("Error reading cross-domain request: %v", err)
|
2025-06-24 22:09:05 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if string(data) == TEXT { //判断是否是跨域请求
|
|
|
|
|
|
log.Printf("Received cross-domain request from %s", c.RemoteAddr())
|
|
|
|
|
|
// 处理跨域请求
|
|
|
|
|
|
c.Write([]byte(CROSS_DOMAIN))
|
|
|
|
|
|
c.Discard(len(TEXT))
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//return
|
|
|
|
|
|
}
|