重构
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package cool
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/utils"
|
||||
"context"
|
||||
"reflect"
|
||||
@@ -14,8 +13,7 @@ import (
|
||||
var ctx = context.TODO()
|
||||
|
||||
var (
|
||||
Mainplayer = &utils.SyncMap[uint32, *entity.Player]{} //玩家数据
|
||||
CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存
|
||||
CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存
|
||||
|
||||
Loger = glog.New()
|
||||
)
|
||||
@@ -37,12 +35,3 @@ func init() {
|
||||
glog.Debug(context.Background(), "初始化雪花算法", newId)
|
||||
|
||||
}
|
||||
func ConutPlayer() int {
|
||||
|
||||
count := 0
|
||||
Mainplayer.Range(func(uint32, *entity.Player) bool {
|
||||
count++
|
||||
return true // 继续遍历
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"github.com/gobwas/ws"
|
||||
"github.com/gobwas/ws/wsutil"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库
|
||||
}
|
||||
|
||||
func NewConn(c gnet.Conn) *Conn {
|
||||
return &Conn{MainConn: c}
|
||||
}
|
||||
func (c *Conn) SendPack(bytes []byte) error {
|
||||
if t, ok := c.MainConn.Context().(*ClientData); ok {
|
||||
if t.Getwsmsg().Upgraded {
|
||||
// This is the echo server
|
||||
err := wsutil.WriteServerMessage(c.MainConn, ws.OpBinary, bytes)
|
||||
if err != nil {
|
||||
logging.Infof("conn[%v] [err=%v]", c.MainConn.RemoteAddr().String(), err.Error())
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
||||
_, err := c.MainConn.Write(bytes)
|
||||
if err != nil {
|
||||
logging.Error(err)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package entity
|
||||
@@ -1,6 +1,18 @@
|
||||
package entity
|
||||
package socket
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ConutPlayer() int {
|
||||
|
||||
count := 0
|
||||
Mainplayer.Range(func(uint32, *Player) bool {
|
||||
count++
|
||||
return true // 继续遍历
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
type ClientData struct {
|
||||
isCrossDomain bool //是否跨域过
|
||||
@@ -1,24 +1,52 @@
|
||||
package entity
|
||||
package socket
|
||||
|
||||
import (
|
||||
"blazing/common/utils"
|
||||
"blazing/modules/blazing/model"
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gobwas/ws"
|
||||
"github.com/gobwas/ws/wsutil"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/panjf2000/gnet/pkg/logging"
|
||||
"github.com/tnnmigga/enum"
|
||||
)
|
||||
|
||||
var Mainplayer = &utils.SyncMap[uint32, *Player]{} //玩家数据
|
||||
|
||||
func (c *Conn) SendPack(bytes []byte) error {
|
||||
if t, ok := c.MainConn.Context().(*ClientData); ok {
|
||||
if t.Getwsmsg().Upgraded {
|
||||
// This is the echo server
|
||||
err := wsutil.WriteServerMessage(c.MainConn, ws.OpBinary, bytes)
|
||||
if err != nil {
|
||||
logging.Infof("conn[%v] [err=%v]", c.MainConn.RemoteAddr().String(), err.Error())
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
||||
_, err := c.MainConn.Write(bytes)
|
||||
if err != nil {
|
||||
logging.Error(err)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
MainConn Conn
|
||||
UserID uint32 //用户ID
|
||||
IsLogin bool //是否登录
|
||||
mu sync.Mutex
|
||||
MapId uint32 //当前所在的地图ID
|
||||
|
||||
IsLogin bool //是否登录
|
||||
mu sync.Mutex
|
||||
|
||||
loginChan chan struct{} // 登录完成通知通道
|
||||
Nick string //昵称
|
||||
Info *model.PlayerInfo
|
||||
StopChan chan struct{} //停止刷怪协程
|
||||
IsFighting bool
|
||||
context.Context
|
||||
@@ -28,12 +56,6 @@ type Player struct {
|
||||
// PlayerOption 定义配置 Player 的函数类型
|
||||
type PlayerOption func(*Player)
|
||||
|
||||
// WithUserID 设置用户ID的选项函数
|
||||
func WithUserID(userID uint32) PlayerOption {
|
||||
return func(p *Player) {
|
||||
p.UserID = userID
|
||||
}
|
||||
}
|
||||
func WithConn(c Conn) PlayerOption {
|
||||
return func(p *Player) {
|
||||
p.MainConn = c
|
||||
@@ -51,14 +73,6 @@ func NewPlayer(opts ...PlayerOption) *Player {
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Player) GetUserID() uint32 {
|
||||
return p.UserID
|
||||
}
|
||||
|
||||
func (p *Player) SetData(key any) uint32 {
|
||||
|
||||
return p.UserID
|
||||
}
|
||||
func (p *Player) SendPack(b []byte) error {
|
||||
fmt.Println("发送数据包", len(b))
|
||||
err := p.MainConn.SendPack(b)
|
||||
@@ -66,7 +80,7 @@ func (p *Player) SendPack(b []byte) error {
|
||||
}
|
||||
func (p *Player) Cheak(b error) {
|
||||
if b != nil {
|
||||
g.Log().Error(context.Background(), "出现错误", p.UserID, b.Error())
|
||||
g.Log().Error(context.Background(), "出现错误", p.Info.UserID, b.Error())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -150,10 +164,10 @@ type Playerinvite struct { //挂载到[]Playerinvite上? 被邀请者->邀请
|
||||
func (lw *Player) InvitePlayerToBattle(target int64, mode EnumBattleMode) {
|
||||
t, ok := Playerinvitemap[uint32(target)] //被邀请者是否被邀请过
|
||||
if ok { //说明存在被邀请
|
||||
t = append(t, Playerinvite{uint32(lw.UserID), mode})
|
||||
t = append(t, Playerinvite{uint32(lw.Info.UserID), mode})
|
||||
Playerinvitemap[uint32(target)] = t
|
||||
} else {
|
||||
Playerinvitemap[uint32(target)] = []Playerinvite{{uint32(lw.UserID), mode}}
|
||||
Playerinvitemap[uint32(target)] = []Playerinvite{{uint32(lw.Info.UserID), mode}}
|
||||
}
|
||||
|
||||
lw.Playerinvite = uint32(target)
|
||||
@@ -1,4 +1,4 @@
|
||||
package entity
|
||||
package socket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -11,6 +11,14 @@ import (
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库
|
||||
}
|
||||
|
||||
func NewConn(c gnet.Conn) *Conn {
|
||||
return &Conn{MainConn: c}
|
||||
}
|
||||
|
||||
type WsCodec struct {
|
||||
Upgraded bool // 链接是否升级
|
||||
Buf bytes.Buffer // 从实际socket中读取到的数据缓存
|
||||
@@ -1 +0,0 @@
|
||||
package socket
|
||||
@@ -6,9 +6,8 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/data/share"
|
||||
"blazing/cool"
|
||||
"blazing/common/data/socket"
|
||||
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
@@ -42,17 +41,17 @@ func (s *Server) Stop() error {
|
||||
func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
|
||||
atomic.AddInt64(&s.connected, -1)
|
||||
//logging.Infof("conn[%v] disconnected", c.RemoteAddr().String())
|
||||
v, ok := c.Context().(*entity.ClientData)
|
||||
v, ok := c.Context().(*socket.ClientData)
|
||||
if !ok {
|
||||
return
|
||||
|
||||
}
|
||||
t := v.GetPlayer()
|
||||
if t != nil {
|
||||
glog.Debug(context.Background(), t.UserID, "断开连接")
|
||||
if v != nil {
|
||||
glog.Debug(context.Background(), t, "断开连接")
|
||||
t.IsLogin = false
|
||||
cool.Mainplayer.Delete(t.UserID)
|
||||
share.ShareManager.DeleteUserOnline(t.UserID) //设置用户登录服务器
|
||||
socket.Mainplayer.Delete(t.Info.UserID)
|
||||
share.ShareManager.DeleteUserOnline(t.Info.UserID) //设置用户登录服务器
|
||||
}
|
||||
|
||||
//}
|
||||
@@ -73,7 +72,7 @@ func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
||||
|
||||
func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
|
||||
if conn.Context() == nil {
|
||||
conn.SetContext(entity.NewClientData()) //注入data
|
||||
conn.SetContext(socket.NewClientData()) //注入data
|
||||
}
|
||||
|
||||
atomic.AddInt64(&s.connected, 1)
|
||||
@@ -86,7 +85,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
ws := c.Context().(*entity.ClientData).Getwsmsg()
|
||||
ws := c.Context().(*socket.ClientData).Getwsmsg()
|
||||
tt, len1 := ws.ReadBufferBytes(c)
|
||||
if tt == gnet.Close {
|
||||
|
||||
@@ -165,7 +164,7 @@ const CROSS_DOMAIN = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy><cros
|
||||
const TEXT = "<policy-file-request/>\x00"
|
||||
|
||||
func handle(c gnet.Conn) {
|
||||
clientdata := c.Context().(*entity.ClientData)
|
||||
clientdata := c.Context().(*socket.ClientData)
|
||||
if clientdata.GetIsCrossDomain() {
|
||||
return
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/data/socket"
|
||||
"blazing/common/utils/bytearray"
|
||||
|
||||
"bytes"
|
||||
@@ -37,7 +37,7 @@ func NewTomeeHeader(cmd uint32, userid uint32) *TomeeHeader {
|
||||
}
|
||||
|
||||
type TomeeHandler struct {
|
||||
Callback func(conn *entity.Conn, data TomeeHeader)
|
||||
Callback func(conn *socket.Conn, data TomeeHeader)
|
||||
}
|
||||
|
||||
func NewTomeeHandler() *TomeeHandler {
|
||||
@@ -60,7 +60,7 @@ func (h *TomeeHandler) Handle(c gnet.Conn, data []byte) { //处理接收到的
|
||||
header.Result, _ = tempdata.ReadUInt32()
|
||||
header.Data = tempdata.BytesAvailable()
|
||||
//fmt.Println("接收封包", header)
|
||||
h.Callback(entity.NewConn(c), header)
|
||||
h.Callback(socket.NewConn(c), header)
|
||||
//return header
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user