/// <summary> /// 解析proto数据 /// </summary> /// <param name="message"></param> /// <returns></returns> public void ProtobufToReaderCommand(byte[] ms) { Message message = ProtobufSerializer.DeSerialize <Message>(ms); switch (message.type) { case Message.MSG.COMMAND: CommndProcess(message); break; case Message.MSG.REQUEST: RequestProcess(message); break; case Message.MSG.RESPOND: RespondProcess(message); break; case Message.MSG.OTHER: OtherProcess(message); break; default: break; } }
private void Start() { pb.PlayerSnapShootMsg msg = new pb.PlayerSnapShootMsg(); msg.username = "******"; msg.playerId = 9000001; byte[] data = ProtobufSerializer.Serialize(msg); Debug.LogError("data len " + data.Length); ByteBuffer buffer = ByteBuffer.Allocate(1024); buffer.WriteShort(1001); buffer.WriteShort(1002); buffer.WriteShort(1003); buffer.WriteLong(10055555); buffer.WriteBytes(data); short s1 = buffer.ReadShort(); short s2 = buffer.ReadShort(); short s3 = buffer.ReadShort(); long l1 = buffer.ReadLong(); byte[] readData = new byte[buffer.ReadableBytes()]; buffer.ReadBytes(readData, 0, buffer.ReadableBytes()); Debug.LogError("readData len " + readData.Length); pb.PlayerSnapShootMsg dmsg = ProtobufSerializer.DeSerialize <pb.PlayerSnapShootMsg>(readData); Debug.LogError("user name " + dmsg.username); //socket receive bytes // }
private T DeSerialize <T>(SerializeType type, byte[] msg) { T data = default(T); switch (type) { case SerializeType.Json: var message = Encoding.UTF8.GetString(msg); data = JsonConvert.DeserializeObject <T>(message); break; default: data = ProtobufSerializer.DeSerialize <T>(msg); break; } return(data); }