-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpc.go
145 lines (134 loc) · 3.17 KB
/
rpc.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
package ntgo
import "io"
const (
// RPCDefVersion should always be set to 1 according to the
// protocol specification.
RPCDefVersion byte = 0x01
)
type ValueRPC struct {
DefVersion byte
ProcedureName *ValueString
ParamSize uint8
Params []RPCParam
OutputSize uint8
Outputs []RPCOutput
}
type RPCParam struct {
Type EntryType
Name *ValueString
DefaultVal EntryValue
}
type RPCOutput struct {
Type EntryType
Name *ValueString
}
func DecodeRPC(r io.Reader) (*ValueRPC, error) {
versionRaw := make([]byte, 1)
_, versionErr := r.Read(versionRaw)
if versionErr != nil {
return nil, versionErr
}
procName, nameErr := DecodeString(r)
if nameErr != nil {
return nil, nameErr
}
paramSizeRaw := make([]byte, 1)
_, paramSizeErr := r.Read(paramSizeRaw)
if paramSizeErr != nil {
return nil, paramSizeErr
}
paramSize := uint8(paramSizeRaw[0])
params := make([]RPCParam, paramSize)
for i := uint8(0); i < paramSize; i++ {
param, paramErr := DecodeRPCParam(r)
if paramErr != nil {
return nil, paramErr
}
params[i] = param
}
outputSizeRaw := make([]byte, 1)
_, outputSizeErr := r.Read(outputSizeRaw)
if outputSizeErr != nil {
return nil, outputSizeErr
}
outputSize := uint8(outputSizeRaw[0])
outputs := make([]RPCOutput, outputSize)
for i := uint8(0); i < outputSize; i++ {
output, outputErr := DecodeRPCOutput(r)
if outputErr != nil {
return nil, outputErr
}
outputs[i] = output
}
return &ValueRPC{
DefVersion: versionRaw[0],
ProcedureName: procName,
ParamSize: paramSize,
Params: params,
OutputSize: outputSize,
Outputs: outputs,
}, nil
}
func (rpc *ValueRPC) GetRaw() []byte {
raw := []byte{rpc.DefVersion}
raw = append(raw, rpc.ProcedureName.GetRaw()...)
raw = append(raw, byte(rpc.ParamSize))
paramsRaw := []byte{}
for i := uint8(0); i < rpc.ParamSize; i++ {
paramsRaw = append(paramsRaw, rpc.Params[i].GetRaw()...)
}
raw = append(raw, paramsRaw...)
raw = append(raw, byte(rpc.OutputSize))
outputsRaw := []byte{}
for i := uint8(0); i < rpc.OutputSize; i++ {
outputsRaw = append(paramsRaw, rpc.Outputs[i].GetRaw()...)
}
raw = append(raw, outputsRaw...)
return raw
}
func DecodeRPCParam(r io.Reader) (RPCParam, error) {
rpcParam := RPCParam{}
entryType, typeErr := DecodeEntryType(r)
if typeErr != nil {
return rpcParam, typeErr
}
rpcParam.Type = entryType
name, nameErr := DecodeString(r)
if nameErr != nil {
return rpcParam, nameErr
}
rpcParam.Name = name
val, valErr := DecodeEntryValue(r, entryType)
if valErr != nil {
return rpcParam, valErr
}
rpcParam.DefaultVal = val
return rpcParam, nil
}
func (param RPCParam) GetRaw() []byte {
raw := []byte{byte(param.Type)}
raw = append(raw, param.Name.GetRaw()...)
raw = append(raw, param.DefaultVal.GetRaw()...)
return raw
}
func DecodeRPCOutput(r io.Reader) (RPCOutput, error) {
output := RPCOutput{}
entryType, typeErr := DecodeEntryType(r)
if typeErr != nil {
return output, typeErr
}
output.Type = entryType
name, nameErr := DecodeString(r)
if nameErr != nil {
return output, nameErr
}
output.Name = name
return output, nil
}
func (output RPCOutput) GetRaw() []byte {
raw := []byte{
byte(output.Type),
}
raw = append(raw, output.Name.GetRaw()...)
return raw
}