From 6498851e78384fe483f4f14312400be58c9a3dee Mon Sep 17 00:00:00 2001 From: duanhf2012 Date: Thu, 2 Apr 2020 17:54:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8E=9F=E5=A7=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network/processor/pbprocessor.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/network/processor/pbprocessor.go b/network/processor/pbprocessor.go index 86619d8..6f6c033 100644 --- a/network/processor/pbprocessor.go +++ b/network/processor/pbprocessor.go @@ -32,6 +32,7 @@ func (p *PBProcessor) SetByteOrder(littleEndian bool) { type PBPackInfo struct { typ uint16 msg proto.Message + rawMsg []byte } func (slf *PBPackInfo) GetPackType() uint16 { @@ -82,19 +83,22 @@ func (slf *PBProcessor ) Unmarshal(data []byte) (interface{}, error) { func (slf *PBProcessor ) Marshal(msg interface{}) ([]byte, error){ pMsg := msg.(*PBPackInfo) - bytes,err := proto.Marshal(pMsg.msg.(proto.Message)) - if err != nil { - return nil,err + var err error + if pMsg.msg!=nil { + pMsg.rawMsg,err = proto.Marshal(pMsg.msg.(proto.Message)) + if err != nil { + return nil,err + } } - buff := make([]byte, 2, len(bytes)+MsgTypeSize) + buff := make([]byte, 2, len(pMsg.rawMsg)+MsgTypeSize) if slf.LittleEndian == true { binary.LittleEndian.PutUint16(buff[:2],pMsg.typ) }else{ binary.BigEndian.PutUint16(buff[:2],pMsg.typ) } - buff = append(buff,bytes...) + buff = append(buff,pMsg.rawMsg...) return buff,nil } @@ -109,3 +113,7 @@ func (slf *PBProcessor) Register(msgtype uint16,msg proto.Message,handle Message func (slf *PBProcessor) MakeMsg(msgType uint16,protoMsg proto.Message) *PBPackInfo { return &PBPackInfo{typ:msgType,msg:protoMsg} } + +func (slf *PBProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBPackInfo { + return &PBPackInfo{typ:msgType,rawMsg:msg} +}