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)
|
||||
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"blazing/common/core"
|
||||
"blazing/common/data/entity"
|
||||
"blazing/common/socket/cmd"
|
||||
"blazing/common/socket/handler"
|
||||
"blazing/logic/service"
|
||||
@@ -94,8 +95,8 @@ func getcmd(t reflect.Type) cmd.EnumCommandID {
|
||||
// fmt.Printf("结构体 %s 的字段信息:\n", t.Name())
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
// fmt.Printf("- 字段名: %s\n", field.Name)
|
||||
|
||||
//fmt.Printf("- 字段名: %s\n", field.Name)
|
||||
//fmt.Printf(" 类型: %v\n", field.Type)
|
||||
if field.Type == reflect.TypeOf(handler.TomeeHeader{}) {
|
||||
// fmt.Println(reflect.ValueOf(field))
|
||||
|
||||
@@ -134,39 +135,51 @@ func Recv(c gnet.Conn, data handler.TomeeHeader) {
|
||||
|
||||
params := []reflect.Value{}
|
||||
|
||||
funct := cmdlister.Type().NumIn()
|
||||
//funct := cmdlister.Type().NumIn()
|
||||
|
||||
// 如果需要可设置的变量(用于修改值),创建指针并解引用
|
||||
ptrValue := reflect.New(cmdlister.Type().In(0))
|
||||
struc.Unpack(bytes.NewBuffer(data.Data), ptrValue)
|
||||
// ttt := cmdlister.Type().In(0)
|
||||
|
||||
tt1 := ptrValue.Elem().Addr().Interface()
|
||||
// fmt.Println(tt1)
|
||||
err := struc.Unpack(bytes.NewBuffer(data.Data), tt1)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
//fmt.Println(tt1)
|
||||
ptrValue1 := ptrValue.Elem()
|
||||
switch funct {
|
||||
case 1:
|
||||
// struc.Unpack(bytes.NewBuffer(data), &a)
|
||||
// return a
|
||||
params = append(params, ptrValue1)
|
||||
// 设置 Name 字段
|
||||
nameField := ptrValue.Elem().Field(0) //首个为header
|
||||
if nameField.IsValid() && nameField.CanSet() {
|
||||
nameField.Set(reflect.ValueOf(data))
|
||||
}
|
||||
if cmdlister.Type().In(1) == reflect.TypeOf(entity.Player{}) {
|
||||
|
||||
case 2:
|
||||
params = append(params, ptrValue1, reflect.ValueOf(service.GetPlayer(c, data.UserID)))
|
||||
} else {
|
||||
|
||||
params = append(params, ptrValue1, reflect.ValueOf(c))
|
||||
}
|
||||
|
||||
ret := cmdlister.Call(params)
|
||||
|
||||
//return core.Pack(data.Handler.UserID, data.Handler.CMD, t1, 0)
|
||||
//core.Pack(c, cmd cmd.EnumCommandID, data any, iserror uint32)
|
||||
switch ret[0].Interface().(type) { //TODO 待修改
|
||||
case []byte: //原始包
|
||||
|
||||
c.Write(ret[0].Interface().([]byte)) //这里直接发送原始包,应该是已经拼接过的原始包,通常不同使用
|
||||
|
||||
//case uint32: //错误码 实际上这里包含在结构体里了 ,错误码应该构造在返回之前
|
||||
|
||||
default:
|
||||
|
||||
var data1 bytes.Buffer
|
||||
struc.Pack(&data1, &data)
|
||||
c.Write(data1.Bytes())
|
||||
|
||||
_, err = c.Write(ret[0].Interface().([]byte))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
// switch ret[0].Interface().(type) { //TODO 待修改
|
||||
// case []byte: //原始包
|
||||
|
||||
// c.Write(ret[0].Interface().([]byte)) //这里直接发送原始包,应该是已经拼接过的原始包,通常不同使用
|
||||
|
||||
// //case uint32: //错误码 实际上这里包含在结构体里了 ,错误码应该构造在返回之前
|
||||
|
||||
// default:
|
||||
|
||||
// var data1 bytes.Buffer
|
||||
// struc.Pack(&data1, &data)
|
||||
// c.Write(data1.Bytes())
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -2,17 +2,33 @@ package controller
|
||||
|
||||
import (
|
||||
"blazing/common/core"
|
||||
"blazing/logic/service"
|
||||
"blazing/logic/service/login"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
)
|
||||
|
||||
// 处理命令: 1001
|
||||
func (h Controller) Login(data login.LoginSidInfo) interface{} { //这个时候player应该是空的
|
||||
func (h Controller) Login(data login.LoginSidInfo, c gnet.Conn) []byte { //这个时候player应该是空的
|
||||
|
||||
fmt.Println(login.CheakSession(data)) //检查结构体
|
||||
|
||||
t1, _ := hex.DecodeString("000186A6683F89CF6E69656F0000000000000000000000000008000F00000000000000000000000000000000000000000000000000000001000001DB0000018B000000000000A8C000000000000000000000000000000000000000080001388000000001000000017FFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000000000000000000000000000000000064000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001FFFFFFFF000000004E4F4E4F0000000000000000000000000000000000000001000000010000000100000001000000010000000100000001000000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000100000064000000000000000000000000000000000000001F000000000000006400000000000093F4000093F4000000D5000000F7000000AD00000088000000920000008C0000009C00000000000000000000000000000000000000000000000000000004000027900000001B00004E6200000014000028380000002800004E3E0000002368493DC60000000000000000000000000000000000000000000100000000000000A937000007D1000186A600000000000186A66E69656F00000000000000000000000000000000000000000000000F0000000000000000000001DB0000018B0000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFF0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
return core.Pack(data.Handler.UserID, data.Handler.CMD, t1, 0)
|
||||
return t1
|
||||
if tt := login.CheakSession(data); tt { //说明sid正确
|
||||
|
||||
service.SetPlayer(c, data.Head.UserID)
|
||||
|
||||
}
|
||||
t1, _ := hex.DecodeString("0000045D37000003E9000186A600000000000186A6683F89CF6E69656F0000000000000000000000000008000F00000000000000000000000000000000000000000000000000000001000001DB0000018B000000000000A8C000000000000000000000000000000000000000080001388000000001000000017FFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000000000000000000000000000000000064000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001FFFFFFFF000000004E4F4E4F0000000000000000000000000000000000000001000000010000000100000001000000010000000100000001000000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000100000064000000000000000000000000000000000000001F000000000000006400000000000093F4000093F4000000D5000000F7000000AD00000088000000920000008C0000009C00000000000000000000000000000000000000000000000000000004000027900000001B00004E6200000014000028380000002800004E3E0000002368493DC60000000000000000000000000000000000000000000100000000000000A937000007D1000186A600000000000186A66E69656F00000000000000000000000000000000000000000000000F0000000000000000000001DB0000018B0000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFF0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
//t1 = t1[17:]
|
||||
fmt.Println(t1[:40])
|
||||
data.Head.Version = "7"
|
||||
data.Head.UserID = 99942
|
||||
data.Head.Result = 0
|
||||
fmt.Println(core.Pack(data.Head, t1[17:])[:40])
|
||||
return core.Pack(data.Head, t1[17:])
|
||||
//return //t1, _ := hex.DecodeString("000186A6683F89CF6E69656F0000000000000000000000000008000F00000000000000000000000000000000000000000000000000000001000001DB0000018B000000000000A8C000000000000000000000000000000000000000080001388000000001000000017FFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000000000000000000000000000000000064000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001FFFFFFFF000000004E4F4E4F0000000000000000000000000000000000000001000000010000000100000001000000010000000100000001000000000003030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030000000100000064000000000000000000000000000000000000001F000000000000006400000000000093F4000093F4000000D5000000F7000000AD00000088000000920000008C0000009C00000000000000000000000000000000000000000000000000000004000027900000001B00004E6200000014000028380000002800004E3E0000002368493DC60000000000000000000000000000000000000000000100000000000000A937000007D1000186A600000000000186A66E69656F00000000000000000000000000000000000000000000000F0000000000000000000001DB0000018B0000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFF0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
|
||||
// return t1
|
||||
}
|
||||
|
||||
11
logic/go.mod
11
logic/go.mod
@@ -5,6 +5,17 @@ go 1.19
|
||||
require github.com/lunixbochs/struc v0.0.0-20241101090106-8d528fa2c543
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/panjf2000/ants/v2 v2.11.3 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
|
||||
26
logic/go.sum
26
logic/go.sum
@@ -1,7 +1,31 @@
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
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/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/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/gogf/gf/v2 v2.6.3/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5k0=
|
||||
github.com/gogf/gf/v2 v2.9.0 h1:semN5Q5qGjDQEv4620VzxcJzJlSD07gmyJ9Sy9zfbHk=
|
||||
github.com/gogf/gf/v2 v2.9.0/go.mod h1:sWGQw+pLILtuHmbOxoe0D+0DdaXxbleT57axOLH2vKI=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/lunixbochs/struc v0.0.0-20241101090106-8d528fa2c543 h1:GxMuVb9tJajC1QpbQwYNY1ZAo1EIE8I+UclBjOfjz/M=
|
||||
github.com/lunixbochs/struc v0.0.0-20241101090106-8d528fa2c543/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/panjf2000/ants/v2 v2.11.3 h1:AfI0ngBoXJmYOpDh9m516vjqoUu2sLrIVgppI9TZVpg=
|
||||
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||
@@ -14,6 +38,8 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
|
||||
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
// LoginSidInfo 登录携带的凭证结构体
|
||||
type LoginSidInfo struct { //这里直接使用组合来实现将传入的原始头部数据和结构体参数序列化
|
||||
Handler handler.TomeeHeader `cmd:"1001" `
|
||||
Head handler.TomeeHeader `cmd:"1001" struc:"[0]pad"`
|
||||
|
||||
Sid []byte `struc:"[16]byte"` // 登录会话ID,固定长度16字节
|
||||
// NotLogin uint32 `error="10001"|struc:"[0]pad"` //返回错误码 ,不序列化,仅作为错误码
|
||||
@@ -26,7 +26,7 @@ func CheakSession(c LoginSidInfo) bool {
|
||||
t2 := strings.Trim(t1, " ")
|
||||
t, err := cool.CacheManager.Get(context.Background(), t2)
|
||||
fmt.Println("后端获取", string(c.Sid), t, err)
|
||||
if t.Uint32() == c.Handler.UserID {
|
||||
if t.Uint32() == c.Head.UserID {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ type CommendSvrInfo struct {
|
||||
|
||||
func NewCommendSvrInfo() *CommendSvrInfo {
|
||||
return &CommendSvrInfo{
|
||||
// Handler: handler.TomeeHeader{},
|
||||
MaxOnlineID: 100,
|
||||
IsVip: 0,
|
||||
ServerInfoLen: 0,
|
||||
|
||||
@@ -16,15 +16,23 @@ func GetPlayer(c gnet.Conn, userid uint32) *entity.Player { //TODO 这里待优
|
||||
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 {
|
||||
if player1, ok := core.Mainplayer.Load((userid)); ok {
|
||||
player = player1.(*entity.Player) //取成功,否则创建
|
||||
clientdata.Player = player
|
||||
}
|
||||
|
||||
return player
|
||||
// return nil
|
||||
}
|
||||
|
||||
func SetPlayer(c gnet.Conn, userid uint32) *entity.Player { //TODO 这里待优化,可能存在内存泄漏问题
|
||||
clientdata := c.Context().(*entity.ClientData)
|
||||
player := entity.NewPlayer(
|
||||
entity.WithUserID(userid), //注入ID
|
||||
entity.WithConn(c), //注入conn
|
||||
)
|
||||
core.Mainplayer.Store(userid, player)
|
||||
|
||||
clientdata.Player = player
|
||||
return player
|
||||
// return nil
|
||||
|
||||
@@ -53,12 +53,12 @@ func recv(c gnet.Conn, data handler.TomeeHeader) {
|
||||
lofin.OnlineID = 1
|
||||
lofin.UserCnt = 77
|
||||
lofin.IP = "127.0.0.1"
|
||||
lofin.Port = 27777
|
||||
lofin.Port = 27000
|
||||
lofin.Friends = 1
|
||||
|
||||
//ret.Handler = data
|
||||
ret.ServerList = append(ret.ServerList, *lofin)
|
||||
|
||||
tt := core.Pack(data.UserID, data.CMD, ret, 0)
|
||||
tt := core.Pack(data, ret)
|
||||
fmt.Println(hex.EncodeToString(tt))
|
||||
c.Write(tt)
|
||||
|
||||
|
||||
@@ -49,9 +49,8 @@ redis:
|
||||
db: 0
|
||||
pass: 154252
|
||||
|
||||
|
||||
blazing:
|
||||
port: 27777
|
||||
port: 27000 ## 也是雪花算法生成端口
|
||||
autoMigrate: true
|
||||
eps: true
|
||||
file:
|
||||
|
||||
@@ -4,11 +4,7 @@ import (
|
||||
baseservice "blazing/modules/base/service"
|
||||
"blazing/modules/blazing/service"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
@@ -48,27 +44,16 @@ func (c *BlazingController) GetSession(ctx context.Context, req *SessionReq) (re
|
||||
}
|
||||
|
||||
accountID := res1.ID
|
||||
retsid, sid, err := service.NewLoginServiceService().GetSessionId(accountID)
|
||||
if err != nil {
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
|
||||
// 生成SID
|
||||
sidInfo := fmt.Sprintf("%d%d", accountID, time.Now().UnixNano())
|
||||
hash := sha256.Sum256([]byte(sidInfo))
|
||||
sidByte := hex.EncodeToString(hash[:])
|
||||
|
||||
// 确保长度为48字符
|
||||
if len(sidByte) < 48 {
|
||||
sidByte = sidByte + strings.Repeat("0", 48-len(sidByte))
|
||||
} else {
|
||||
sidByte = sidByte[:48]
|
||||
}
|
||||
|
||||
// UID拼接SID
|
||||
hex8 := fmt.Sprintf("%08X", int(accountID&0xFFFFFFFF))
|
||||
res.Session = hex8 + sidByte[8:]
|
||||
res.Session = retsid
|
||||
|
||||
// 保存后端校验的SID
|
||||
backendSID := res.Session[8 : len(res.Session)-8]
|
||||
fmt.Println("存储sid", backendSID)
|
||||
if err := service.NewLoginServiceService().SaveSessionId(backendSID, gconv.String(accountID)); err != nil {
|
||||
if err := service.NewLoginServiceService().SaveSessionId(sid, gconv.String(accountID)); err != nil {
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ require (
|
||||
github.com/go-logr/logr v1.4.1 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.1 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
|
||||
@@ -20,6 +20,8 @@ github.com/gogf/gf/v2 v2.6.3/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
|
||||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 h1:03UrQLjAny8xci+R+qjCce/MYnpNXCtgzltlQbOBae4=
|
||||
|
||||
@@ -3,11 +3,15 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type LoginService struct {
|
||||
@@ -30,3 +34,29 @@ func (s *LoginService) SaveSessionId(session string, userid string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
func (s *LoginService) GetSessionId(accountID uint) (string, string, error) {
|
||||
|
||||
t1, _ := uuid.NewV7()
|
||||
tt := strings.Replace(t1.String(), "-", "", -1)
|
||||
|
||||
rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
// 生成4个随机字节
|
||||
bytes := make([]byte, 4)
|
||||
for i := range bytes {
|
||||
bytes[i] = byte(rand.Intn(256)) // 生成0-255之间的随机数
|
||||
}
|
||||
|
||||
buf2 := make([]byte, 4) // 创建4字节的缓冲区
|
||||
binary.BigEndian.PutUint32(buf2, uint32(accountID))
|
||||
|
||||
//fmt.Printf("小端序: %v (十六进制: %x)\n", buf2, buf2) // 输出: [252 255 255 255] (0xFCFFFFFF)
|
||||
|
||||
//fmt.Println(bytes, "随机字节")
|
||||
ret := append(bytes, hex.EncodeToString([]byte(tt))...)
|
||||
ret = append(ret, bytes...)
|
||||
|
||||
return string(ret), tt, nil
|
||||
// /t1.
|
||||
// 以上过程只需全局一次,且应在生成ID之前完成。
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user