feat(common): 重构数据包处理逻辑并添加全局 ID 生成器
- 引入 idgenerator-go 库,实现全局唯一 ID 生成 - 重构 Pack 函数,使用接口参数提高灵活性 - 修改 Player 结构,增加 MainConn 字段用于主连接 - 更新 SocketHandler_Tomee 中的 Data 字段标记 - 优化 Recv 函数中的数据解包和参数处理逻辑
This commit is contained in:
@@ -1,10 +1,31 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/yitter/idgenerator-go/idgen"
|
||||
)
|
||||
|
||||
var (
|
||||
Mainplayer sync.Map //玩家数据
|
||||
Maincmdcache sync.Map //命令缓存
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 创建 IdGeneratorOptions 对象,可在构造函数中输入 WorkerId:
|
||||
tt := gconv.Uint16(1)
|
||||
var options = idgen.NewIdGeneratorOptions(tt)
|
||||
// options.WorkerIdBitLength = 10 // 默认值6,限定 WorkerId 最大值为2^6-1,即默认最多支持64个节点。
|
||||
// options.SeqBitLength = 6; // 默认值6,限制每毫秒生成的ID个数。若生成速度超过5万个/秒,建议加大 SeqBitLength 到 10。
|
||||
// options.BaseTime = Your_Base_Time // 如果要兼容老系统的雪花算法,此处应设置为老系统的BaseTime。
|
||||
// ...... 其它参数参考 IdGeneratorOptions 定义。
|
||||
|
||||
// 保存参数(务必调用,否则参数设置不生效):
|
||||
idgen.SetIdGenerator(options)
|
||||
newId := idgen.NextId()
|
||||
|
||||
fmt.Println(newId, "初始化雪花算法")
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package core
|
||||
|
||||
import (
|
||||
"blazing/common/serialize/bytearray"
|
||||
"blazing/common/socket/cmd"
|
||||
"blazing/common/socket/handler"
|
||||
"bytes"
|
||||
"fmt"
|
||||
@@ -11,21 +10,21 @@ import (
|
||||
"github.com/lunixbochs/struc"
|
||||
)
|
||||
|
||||
func Pack(userid uint32, cmd cmd.EnumCommandID, data any, iserror uint32) []byte { //组包
|
||||
|
||||
t := reflect.TypeOf(data)
|
||||
func Pack(header1 handler.TomeeHeader, data any) []byte { //组包
|
||||
header := header1
|
||||
//t := reflect.TypeOf(data)
|
||||
tv := reflect.ValueOf(data)
|
||||
var datar []byte
|
||||
|
||||
// 处理指针类型
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem() // 获取指针指向的类型
|
||||
if tv.Kind() == reflect.Ptr {
|
||||
//tv = t.Elem() // 获取指针指向的类型
|
||||
tv = tv.Elem() // 获取指针指向的值
|
||||
}
|
||||
//.Println(t.Kind())
|
||||
//t1 := tv.Interface()
|
||||
//fmt.Println(t1)
|
||||
switch t.Kind() {
|
||||
switch tv.Kind() {
|
||||
|
||||
case reflect.Slice:
|
||||
datar = data.([]byte)
|
||||
@@ -39,33 +38,28 @@ func Pack(userid uint32, cmd cmd.EnumCommandID, data any, iserror uint32) []byte
|
||||
}
|
||||
|
||||
datar = data1.Bytes()
|
||||
|
||||
default:
|
||||
datar = []byte{}
|
||||
// fmt.Println(err, datar)
|
||||
// p.Conn.Write(p.pack(cmd, data))
|
||||
|
||||
}
|
||||
header.Len = uint32(len(datar) + 17)
|
||||
|
||||
head1 := handler.TomeeHeader{
|
||||
|
||||
Len: uint32(len(datar) + 17),
|
||||
CMD: cmd,
|
||||
Version: "7",
|
||||
UserID: userid,
|
||||
Result: iserror,
|
||||
Data: datar,
|
||||
}
|
||||
|
||||
if iserror != 0 { //如果存在错误码
|
||||
head1.Data = nil
|
||||
}
|
||||
// if iserror != 0 { //如果存在错误码
|
||||
// head1.Data = nil
|
||||
// }
|
||||
//var data1 bytes.Buffer
|
||||
//.Pack(&data1, &head1)
|
||||
by := bytearray.CreateByteArray()
|
||||
by.WriteUInt32(head1.Len)
|
||||
by.WriteString(head1.Version)
|
||||
by.WriteUInt32(uint32(head1.CMD))
|
||||
by.WriteUInt32(head1.UserID)
|
||||
by.WriteUInt32(head1.Result)
|
||||
by.WriteUInt32(header.Len)
|
||||
by.WriteString(header.Version)
|
||||
by.WriteUInt32(uint32(header.CMD))
|
||||
by.WriteUInt32(header.UserID)
|
||||
by.WriteUInt32(header.Result)
|
||||
by.Write(datar)
|
||||
|
||||
return by.Bytes()
|
||||
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@ import (
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
UserID uint32 //用户ID
|
||||
IsLogin bool //是否登录 //TODO 待实现登录包为第一个包,后续再发其他的包
|
||||
Conn gnet.Conn
|
||||
MainConn gnet.Conn `struc:"[0]pad"` //TODO 不序列化,,序列化下面的作为blob存数据库
|
||||
UserID uint32 //用户ID
|
||||
IsLogin bool //是否登录 //TODO 待实现登录包为第一个包,后续再发其他的包
|
||||
|
||||
}
|
||||
|
||||
// PlayerOption 定义配置 Player 的函数类型
|
||||
@@ -21,7 +22,7 @@ func WithUserID(userID uint32) PlayerOption {
|
||||
}
|
||||
func WithConn(Conn gnet.Conn) PlayerOption {
|
||||
return func(p *Player) {
|
||||
p.Conn = Conn
|
||||
p.MainConn = Conn
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,11 @@ require (
|
||||
require (
|
||||
github.com/antchfx/xmlquery v1.4.4 // indirect
|
||||
github.com/antchfx/xpath v1.3.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/yitter/idgenerator-go v1.3.3 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@@ -8,11 +8,15 @@ github.com/antchfx/xmlquery v1.4.4/go.mod h1:AEPEEPYE9GnA2mj5Ur2L5Q5/2PycJ0N9Fus
|
||||
github.com/antchfx/xpath v1.3.3 h1:tmuPQa1Uye0Ym1Zn65vxPgfltWb/Lxu2jeqIGteJSRs=
|
||||
github.com/antchfx/xpath v1.3.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
@@ -22,6 +26,8 @@ github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
|
||||
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/gogf/gf/contrib/nosql/redis/v2 v2.6.3/go.mod h1:2+evGu1xAlamaYuDdSqa7QCiwPTm1RrGsUFSMc8PyLc=
|
||||
github.com/gogf/gf/v2 v2.6.3 h1:DoqeuwU98wotpFoDSQEx8RZbmJdK8KdGiJtzJeqpyIo=
|
||||
github.com/gogf/gf/v2 v2.6.3/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5k0=
|
||||
@@ -76,6 +82,8 @@ github.com/tnnmigga/enum v1.0.2 h1:Yvchx0Esc01X5HiphW78sKzH/RXKttdFsfPO1ARiOa4=
|
||||
github.com/tnnmigga/enum v1.0.2/go.mod h1:QaBFBwGJi/2GAM34b2pz6UL2NRtl2TRZ8lXp4vGwqhA=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/yitter/idgenerator-go v1.3.3 h1:i6rzmpbCL0vlmr/tuW5+lSQzNuDG9vYBjIYRvnRcHE8=
|
||||
github.com/yitter/idgenerator-go v1.3.3/go.mod h1:VVjbqFjGUsIkaXVkXEdmx1LiXUL3K1NvyxWPJBPbBpE=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
|
||||
|
||||
@@ -16,7 +16,7 @@ type TomeeHeader struct {
|
||||
//Error uint32 `json:"error" struc:"[0]pad"`
|
||||
|
||||
Result uint32 `json:"result"`
|
||||
Data []byte `json:"data" struc:"[0]pad"` //组包忽略此字段// struc:"[0]pad"
|
||||
Data []byte `json:"data"|struc:"skip"` //组包忽略此字段// struc:"[0]pad"
|
||||
}
|
||||
type TomeeHandler struct {
|
||||
Callback func(conn gnet.Conn, data TomeeHeader)
|
||||
|
||||
Reference in New Issue
Block a user