-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_encode.go
43 lines (37 loc) · 1.04 KB
/
rpc_encode.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
package rpc
import (
"bytes"
"encoding/gob"
"github.com/fengqk/mars-base/base"
"github.com/golang/protobuf/proto"
)
// rpc Marshal
func Marshal(head *RpcHead, funcName *string, params ...interface{}) Packet {
return marshal(head, funcName, params...)
}
// rpc marshal
func marshal(head *RpcHead, funcName *string, params ...interface{}) Packet {
defer func() {
if err := recover(); err != nil {
base.TraceCode(err)
}
}()
*funcName = Route(head, *funcName)
rpcPacket := &RpcPacket{FuncName: *funcName, ArgLen: int32(len(params)), RpcHead: (*RpcHead)(head)}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
for _, param := range params {
enc.Encode(param)
}
rpcPacket.RpcBody = buf.Bytes()
data, _ := proto.Marshal(rpcPacket)
return Packet{Buff: data, RpcPacket: rpcPacket}
}
// rpc MarshalPB
func marshalPB(bitstream *base.BitStream, packet proto.Message) {
bitstream.WriteString(proto.MessageName(packet))
buf, _ := proto.Marshal(packet)
nLen := len(buf)
bitstream.WriteInt(nLen, 32)
bitstream.WriteBits(buf, nLen<<3)
}