refactor: 优化命令注册和请求处理逻辑
This commit is contained in:
@@ -53,6 +53,44 @@ func getUnderlyingValue(val reflect.Value) (reflect.Value, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func setFieldByIndex(root reflect.Value, index []int, value reflect.Value) bool {
|
||||
current := root
|
||||
for pos, idx := range index {
|
||||
if current.Kind() == reflect.Ptr {
|
||||
if current.IsNil() {
|
||||
current.Set(reflect.New(current.Type().Elem()))
|
||||
}
|
||||
current = current.Elem()
|
||||
}
|
||||
|
||||
if current.Kind() != reflect.Struct || idx < 0 || idx >= current.NumField() {
|
||||
return false
|
||||
}
|
||||
|
||||
field := current.Field(idx)
|
||||
if pos == len(index)-1 {
|
||||
if !field.CanSet() {
|
||||
return false
|
||||
}
|
||||
if value.Type().AssignableTo(field.Type()) {
|
||||
field.Set(value)
|
||||
return true
|
||||
}
|
||||
if field.Kind() == reflect.Ptr && value.Type().AssignableTo(field.Type().Elem()) {
|
||||
ptr := reflect.New(field.Type().Elem())
|
||||
ptr.Elem().Set(value)
|
||||
field.Set(ptr)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
current = field
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// XORDecryptU 原地执行异或解密,避免额外分配和拷贝。
|
||||
func XORDecryptU(encryptedData []byte, key uint32) []byte {
|
||||
if len(encryptedData) == 0 {
|
||||
@@ -193,45 +231,38 @@ func (h *ClientData) OnEvent(data common.TomeeHeader) {
|
||||
return //TODO 待实现cmd未注册
|
||||
}
|
||||
|
||||
params := []reflect.Value{}
|
||||
var ptrValue reflect.Value
|
||||
if cmdlister.NewReqValue != nil {
|
||||
ptrValue = cmdlister.NewReqValue()
|
||||
} else {
|
||||
ptrValue = reflect.New(cmdlister.Req)
|
||||
}
|
||||
|
||||
//funct := cmdlister.Type().NumIn()
|
||||
|
||||
// 如果需要可设置的变量(用于修改值),创建指针并解引用
|
||||
ptrValue := reflect.New(cmdlister.Req)
|
||||
|
||||
// fmt.Println(tt1)
|
||||
if data.Res != nil {
|
||||
tt1 := ptrValue.Elem().Addr().Interface()
|
||||
err := struc.Unpack(bytes.NewBuffer(data.Res), tt1)
|
||||
err := struc.Unpack(bytes.NewBuffer(data.Res), ptrValue.Interface())
|
||||
|
||||
if err != nil {
|
||||
|
||||
cool.Logger.Error(context.Background(), data.UserID, data.CMD, "解包失败,", err, hex.EncodeToString(data.Res))
|
||||
//fmt.Println(data.UserID, data.CMD, "解包失败,", hex.EncodeToString(data.Data))
|
||||
data.Result = uint32(errorcode.ErrorCodes.ErrSystemProcessingError)
|
||||
h.SendPack(data.Pack(nil))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ptrValue1 := ptrValue.Elem().Addr()
|
||||
// 设置 Name 字段
|
||||
nameField := ptrValue.Elem().Field(0) //首个为header
|
||||
nameField.Set(reflect.ValueOf(data))
|
||||
|
||||
if data.CMD > 1001 { //if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
|
||||
//t := GetPlayer(c, data.UserID)
|
||||
|
||||
// fmt.Println(data.CMD, "接收 变量的地址 ", &t.Info, t.Info.UserID)
|
||||
|
||||
params = append(params, ptrValue1, reflect.ValueOf(h.Player))
|
||||
} else {
|
||||
|
||||
params = append(params, ptrValue1, reflect.ValueOf(h.Conn))
|
||||
if !setFieldByIndex(ptrValue.Elem(), cmdlister.HeaderFieldIndex, reflect.ValueOf(data)) {
|
||||
cool.Logger.Warning(context.Background(), data.UserID, data.CMD, "设置请求头失败")
|
||||
return
|
||||
}
|
||||
|
||||
ret := cmdlister.Func.Call(params)
|
||||
var params [2]reflect.Value
|
||||
params[0] = ptrValue
|
||||
if cmdlister.UseConn {
|
||||
params[1] = reflect.ValueOf(h.Conn)
|
||||
} else {
|
||||
params[1] = reflect.ValueOf(h.Player)
|
||||
}
|
||||
|
||||
ret := cmdlister.Func.Call(params[:])
|
||||
|
||||
if len(ret) <= 0 { //如果判断没有参数,那就说明这个包没有返回参数
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user