github.com/aergoio/aergo@v1.3.1/p2p/p2putil/protobuf.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2putil 7 8 import ( 9 "github.com/aergoio/aergo/p2p/p2pcommon" 10 "github.com/golang/protobuf/proto" 11 ) 12 13 func CalculateFieldDescSize(varSize int) int { 14 switch { 15 case varSize == 0: 16 return 0 17 case varSize < 128: 18 return 2 19 case varSize < 16384: 20 return 3 21 case varSize < 2097152: 22 return 4 23 case varSize < 268435456: 24 return 5 25 case varSize < 34359738368: 26 return 6 27 default: 28 return 7 29 } 30 } 31 32 func MarshalMessageBody(message p2pcommon.MessageBody) ([]byte, error) { 33 return proto.Marshal(message) 34 } 35 36 func UnmarshalMessageBody(data []byte, msgData p2pcommon.MessageBody) error { 37 return proto.Unmarshal(data, msgData) 38 } 39 40 func UnmarshalAndReturn(data []byte, msgData p2pcommon.MessageBody) (p2pcommon.MessageBody, error) { 41 return msgData, proto.Unmarshal(data, msgData) 42 }