-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
146 lines (133 loc) · 3.67 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package rpc
import (
"bytes"
"context"
"encoding/gob"
"errors"
"reflect"
"strings"
"github.com/erdong01/kit/api"
"github.com/erdong01/kit/base"
"google.golang.org/protobuf/proto"
)
// rpc UnmarshalHead
func UnmarshalHead(buff []byte) (*RpcPacket, RpcHead) {
nLen := base.Clamp(len(buff), 0, 256)
return Unmarshal(buff[:nLen])
}
func Unmarshal(buff []byte) (*RpcPacket, RpcHead) {
rpcPacket := &RpcPacket{}
proto.Unmarshal(buff, rpcPacket)
if rpcPacket.RpcHead == nil {
rpcPacket.RpcHead = &RpcHead{}
}
// betree funcname
/*actorArgs := strings.Split(rpcPacket.FuncName, ".")
if len(actorArgs) == 2 {
rpcPacket.RpcHead.ActorName = actorArgs[0]
rpcPacket.FuncName = actorArgs[1]
} else {
rpcPacket.FuncName = actorArgs[0]
}*/
return rpcPacket, *(*RpcHead)(rpcPacket.RpcHead)
}
func UnmarshalBodyJson(jsonPacket api.JsonPacket, pFuncType reflect.Type) []interface{} {
nCurLen := pFuncType.NumIn()
params := make([]interface{}, nCurLen)
if jsonPacket.Head != nil {
params[0] = context.WithValue(context.Background(), "Head", *(*api.JsonHead)(jsonPacket.Head))
} else {
params[0] = context.Background()
}
for i := 1; i < nCurLen; i++ {
if i == 0 {
continue
}
if nCurLen == 2 {
params[i] = jsonPacket.Data
} else if Data, ok := jsonPacket.Data.([]interface{}); ok && len(Data) >= (nCurLen-1) {
params[i] = Data[i-1]
}
}
return params
}
// rpc Unmarshal
// pFuncType for (this *X)func(conttext, params)
func UnmarshalBody(rpcPacket *RpcPacket, pFuncType reflect.Type) []interface{} {
nCurLen := pFuncType.NumIn()
params := make([]interface{}, nCurLen)
buf := bytes.NewBuffer(rpcPacket.RpcBody)
dec := gob.NewDecoder(buf)
for i := 1; i < nCurLen; i++ {
if i == 1 {
params[1] = context.WithValue(context.Background(), "rpcHead", *(*RpcHead)(rpcPacket.RpcHead))
continue
}
val := reflect.New(pFuncType.In(i))
if i < int(rpcPacket.ArgLen+2) {
dec.DecodeValue(val)
}
params[i] = val.Elem().Interface()
}
return params
}
func UnmarshalBodyCall(rpcPacket *RpcPacket, pFuncType reflect.Type) (error, []interface{}) {
strErr := ""
nCurLen := pFuncType.NumIn()
params := make([]interface{}, nCurLen)
buf := bytes.NewBuffer(rpcPacket.RpcBody)
dec := gob.NewDecoder(buf)
dec.Decode(&strErr)
if strErr != "" {
return errors.New(strErr), params
}
for i := 0; i < nCurLen; i++ {
if i == 0 {
params[0] = context.WithValue(context.Background(), "rpcHead", *(*RpcHead)(rpcPacket.RpcHead))
continue
}
val := reflect.New(pFuncType.In(i))
if i < int(rpcPacket.ArgLen+1) {
dec.DecodeValue(val)
}
params[i] = val.Elem().Interface()
}
return nil, params
}
// ------- 旧代码 -------------
func Unmarshal2(buff []byte) (*RpcPacket, RpcHead) {
rpcPacket := &RpcPacket{}
proto.Unmarshal(buff, rpcPacket)
if rpcPacket.RpcHead == nil {
rpcPacket.RpcHead = &RpcHead{}
}
rpcPacket.FuncName = strings.ToLower(rpcPacket.FuncName)
return rpcPacket, *(*RpcHead)(rpcPacket.RpcHead)
}
func UnmarshalBody2(rpcPacket *RpcPacket, pFuncType reflect.Type) []interface{} {
nCurLen := pFuncType.NumIn()
params := make([]interface{}, nCurLen)
var dec *gob.Decoder
if rpcPacket.ArgLen > 0 {
buf := bytes.NewBuffer(rpcPacket.RpcBody)
dec = gob.NewDecoder(buf)
}
for i := 0; i < nCurLen; i++ {
if i == 0 {
params[0] = context.WithValue(context.Background(), "rpcHead", *(*RpcHead)(rpcPacket.RpcHead))
continue
}
if i < int(rpcPacket.ArgLen+1) {
val := reflect.New(pFuncType.In(i))
dec.DecodeValue(val)
params[i] = val.Elem().Interface()
}
if rpcPacket.ArgLen == 0 {
val := reflect.New(pFuncType.In(i).Elem())
m := val.Interface().(proto.Message)
proto.Unmarshal(rpcPacket.RpcBody, m)
params[i] = m
}
}
return params
}