refactor(pack): 重构组包逻辑
- 移除 common/core/pack.go 文件 - 在 TomeeHeader 结构体中添加 Pack 方法实现组包功能 - 更新 Player 结构体和相关方法,使用 Conn 包装 gnet.Conn - 修改控制器和服务中的组包调用,使用新的 Pack 方法 - 优化部分代码结构,提高可维护性
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"blazing/common/serialize/bytearray"
|
||||
"blazing/common/socket/handler"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/lunixbochs/struc"
|
||||
)
|
||||
|
||||
func Pack(header1 handler.TomeeHeader, data any) []byte { //组包
|
||||
header := header1
|
||||
//t := reflect.TypeOf(data)
|
||||
tv := reflect.ValueOf(data)
|
||||
var datar []byte
|
||||
|
||||
// 处理指针类型
|
||||
if tv.Kind() == reflect.Ptr {
|
||||
//tv = t.Elem() // 获取指针指向的类型
|
||||
tv = tv.Elem() // 获取指针指向的值
|
||||
}
|
||||
//.Println(t.Kind())
|
||||
//t1 := tv.Interface()
|
||||
//fmt.Println(t1)
|
||||
switch tv.Kind() {
|
||||
|
||||
case reflect.Slice:
|
||||
datar = data.([]byte)
|
||||
//p.Conn.Write(p.pack(cmd, data.([]byte))) //写入数据
|
||||
|
||||
case reflect.Struct:
|
||||
var data1 bytes.Buffer
|
||||
err := struc.Pack(&data1, data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
datar = data1.Bytes()
|
||||
|
||||
default:
|
||||
datar = []byte{}
|
||||
// fmt.Println(err, datar)
|
||||
// p.Conn.Write(p.pack(cmd, data))
|
||||
|
||||
}
|
||||
header.Len = uint32(len(datar) + 17)
|
||||
|
||||
// if iserror != 0 { //如果存在错误码
|
||||
// head1.Data = nil
|
||||
// }
|
||||
//var data1 bytes.Buffer
|
||||
//.Pack(&data1, &head1)
|
||||
by := bytearray.CreateByteArray()
|
||||
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()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user