编码消息, 在使用了带内存池的codec中,可以传入session或peer的ContextSet,保存内存池上下文,默认ctx传nil
(msg interface{}, ctx cellnet.ContextSet)
| 6 | |
| 7 | // 编码消息, 在使用了带内存池的codec中,可以传入session或peer的ContextSet,保存内存池上下文,默认ctx传nil |
| 8 | func 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 | // 解码消息 |
| 30 | func DecodeMessage(msgid int, data []byte) (interface{}, *cellnet.MessageMeta, error) { |