refactor(logic): 重构逻辑模块

- 移除了 player.go 中的 SendPackBytes 方法
- 将 TomeeHeader 中的 CMDID 改为 CMD,类型从 uint32 改为 EnumCommandID
- 删除了 controller 文件夹下的 service.go 和 login.go
- 新增了 login 文件夹下的 PlayerLoginService.go
- 优化了 main.go 中的 Start 函数,使用 controller.Recv 作为回调
This commit is contained in:
2025-06-26 23:20:11 +08:00
parent dce68c7094
commit feabc7153a
15 changed files with 375 additions and 332 deletions

View File

@@ -1,68 +0,0 @@
package login
import (
"blazing/common/data/entity"
"blazing/cool"
"context"
"encoding/hex"
"fmt"
"strings"
)
// CommendSvrInfo 初始连接请求信息结构体
type CommendSvrInfo struct {
MaxOnlineID uint32 // 最大连接数
IsVip uint32 // 建议为0
ServerInfoLen uint32 `struc:"sizeof=ServerList"` // 服务器信息长度 ServerInfo
ServerList []ServerInfo // 服务器具体信息
Reversed uint32 // 保留字段
}
func NewCommendSvrInfo() *CommendSvrInfo {
return &CommendSvrInfo{
MaxOnlineID: 100,
IsVip: 0,
ServerInfoLen: 0,
ServerList: make([]ServerInfo, 0),
Reversed: 0,
}
}
// ServerInfo 服务器信息结构体
type ServerInfo struct {
// 连接ID, 即服务器序号
OnlineID uint32
// 当前服务器玩家在线数量, 供SWF显示
UserCnt uint32
// 服务器IP, 16字节UTF-8, 不足16补齐到16
IP string `struc:"[16]byte"` // 定长模式16字节
// 端口
Port uint16
// 好友在线的个数
Friends uint32
}
// NewServerInfo 创建新的服务器信息实例
func NewServerInfo() *ServerInfo {
return &ServerInfo{}
}
// LoginSidInfo 登录携带的凭证结构体
type LoginSidInfo struct {
Sid []byte `struc:"[16]byte"` // 登录会话ID固定长度16字节
}
func CheakSession(c LoginSidInfo, p *entity.Player) bool {
// tt, _ := cool.CacheManager.Keys(context.Background())
//g.Dump(tt)
t1 := hex.EncodeToString(c.Sid)
t2 := strings.Trim(t1, " ")
t, err := cool.CacheManager.Get(context.Background(), t2)
fmt.Println("后端获取", string(c.Sid), t, err)
if t.Uint32() == p.UserID {
return true
}
return false
}

34
logic/service/login/in.go Normal file
View File

@@ -0,0 +1,34 @@
package login
import (
"blazing/common/socket/handler"
"blazing/cool"
"context"
"encoding/hex"
"fmt"
"strings"
)
// LoginSidInfo 登录携带的凭证结构体
type LoginSidInfo struct { //这里直接使用组合来实现将传入的原始头部数据和结构体参数序列化
handler.TomeeHeader `cmd:"1001" struc:"[0]pad"`
Sid []byte `struc:"[16]byte"` // 登录会话ID固定长度16字节
// NotLogin uint32 `error="10001"|struc:"[0]pad"` //返回错误码 ,不序列化,仅作为错误码
// ErrorPassWord uint32 `struc:"[0]pad"`
}
func CheakSession(c LoginSidInfo) bool {
// tt, _ := cool.CacheManager.Keys(context.Background())
//g.Dump(tt)
t1 := hex.EncodeToString(c.Sid)
t2 := strings.Trim(t1, " ")
t, err := cool.CacheManager.Get(context.Background(), t2)
fmt.Println("后端获取", string(c.Sid), t, err)
if t.Uint32() == c.UserID {
return true
}
return false
}

View File

@@ -0,0 +1,42 @@
package login
import "blazing/common/socket/handler"
// CommendSvrInfo 初始连接请求信息结构体
type CommendSvrInfo struct {
Handler handler.TomeeHeader ` struc:"[0]pad"` //消息头 ,这里为传入的头部数据,遍历此头部实现解析CommendSvrInfo
MaxOnlineID uint32 // 最大连接数
IsVip uint32 // 建议为0
ServerInfoLen uint32 `struc:"sizeof=ServerList"` // 服务器信息长度 ServerInfo
ServerList []ServerInfo // 服务器具体信息
Reversed uint32 // 保留字段
}
func NewCommendSvrInfo() *CommendSvrInfo {
return &CommendSvrInfo{
MaxOnlineID: 100,
IsVip: 0,
ServerInfoLen: 0,
ServerList: make([]ServerInfo, 0),
Reversed: 0,
}
}
// ServerInfo 服务器信息结构体
type ServerInfo struct {
// 连接ID, 即服务器序号
OnlineID uint32
// 当前服务器玩家在线数量, 供SWF显示
UserCnt uint32
// 服务器IP, 16字节UTF-8, 不足16补齐到16
IP string `struc:"[16]byte"` // 定长模式16字节
// 端口
Port uint16
// 好友在线的个数
Friends uint32
}
// NewServerInfo 创建新的服务器信息实例
func NewServerInfo() *ServerInfo {
return &ServerInfo{}
}

31
logic/service/service.go Normal file
View File

@@ -0,0 +1,31 @@
package service
import (
"blazing/common/core"
"blazing/common/data/entity"
"github.com/panjf2000/gnet/v2"
)
func GetPlayer(c gnet.Conn, userid uint32) *entity.Player { //TODO 这里待优化,可能存在内存泄漏问题
//检查player初始化是否为conn初始后取map防止二次连接后存在两个player
clientdata := c.Context().(*entity.ClientData)
if clientdata != nil && clientdata.Player != nil {
return clientdata.Player
}
var player *entity.Player
if player1, ok := core.Mainplayer.Load((userid)); !ok {
player = entity.NewPlayer(
entity.WithUserID(userid), //注入ID
entity.WithConn(c), //注入conn
)
core.Mainplayer.Store(userid, player)
} else {
player = player1.(*entity.Player) //取成功,否则创建
}
clientdata.Player = player
return player
// return nil
}