MCPcopy
hub / github.com/davyxu/cellnet / EncodeMessage

Function EncodeMessage

codec/msgcodec.go:8–27  ·  view source on GitHub ↗

编码消息, 在使用了带内存池的codec中,可以传入session或peer的ContextSet,保存内存池上下文,默认ctx传nil

(msg interface{}, ctx cellnet.ContextSet)

Source from the content-addressed store, hash-verified

6
7// 编码消息, 在使用了带内存池的codec中,可以传入session或peer的ContextSet,保存内存池上下文,默认ctx传nil
8func EncodeMessage(msg interface{}, ctx cellnet.ContextSet) (data []byte, meta *cellnet.MessageMeta, err error) {
9
10 // 获取消息元信息
11 meta = cellnet.MessageMetaByMsg(msg)
12 if meta == nil {
13 return nil, nil, cellnet.NewErrorContext("msg not exists", msg)
14 }
15
16 // 将消息编码为字节数组
17 var raw interface{}
18 raw, err = meta.Codec.Encode(msg, ctx)
19
20 if err != nil {
21 return
22 }
23
24 data = raw.([]byte)
25
26 return
27}
28
29// 解码消息
30func DecodeMessage(msgid int, data []byte) (interface{}, *cellnet.MessageMeta, error) {

Callers 7

TestGogopbCodec_CodecFunction · 0.92
OnSendMessageMethod · 0.92
sendPacketFunction · 0.92
SendMethod · 0.92
ReplyMethod · 0.92
SendLTVPacketFunction · 0.92
RelayFunction · 0.92

Calls 3

MessageMetaByMsgFunction · 0.92
NewErrorContextFunction · 0.92
EncodeMethod · 0.65

Tested by 1

TestGogopbCodec_CodecFunction · 0.74