void PrintData(ntf_battle_frame_data data) { StringBuilder sb = new StringBuilder(); sb.AppendLine("============================"); sb.AppendFormat("data.server_curr_frame = {0}", data.server_curr_frame); sb.AppendLine(); sb.AppendFormat("data.slot_list.Count = {0}", data.slot_list.Count); sb.AppendLine(); if (data.slot_list.Count > 0) { ntf_battle_frame_data.one_slot oneSlot = data.slot_list[0]; sb.AppendFormat("oneSlot[0].slot = {0}", oneSlot.slot); sb.AppendLine(); sb.AppendFormat("oneSlot[0].cmd_list.Count = {0}", oneSlot.cmd_list.Count); sb.AppendLine(); if (oneSlot.cmd_list.Count > 0) { ntf_battle_frame_data.cmd_with_frame cmdWithFrame = oneSlot.cmd_list[0]; sb.AppendFormat("oneSlot[0].cmd_list[0].server_frame = {0}", cmdWithFrame.server_frame); sb.AppendLine(); sb.AppendFormat("oneSlot[0].cmd_list[0].cmd.cmd_id = {0}", cmdWithFrame.cmd.cmd_id); sb.AppendLine(); sb.AppendFormat("oneSlot[0].cmd_list[0].cmd.UID = {0}", cmdWithFrame.cmd.UID); sb.AppendLine(); sb.AppendFormat("oneSlot[0].cmd_list[0].cmd.cmd_data.Length = {0}", cmdWithFrame.cmd.cmd_data.Length); sb.AppendLine(); sb.AppendFormat("oneSlot[0].cmd_list[0].cmd.cmd_data[0] = {0}", cmdWithFrame.cmd.cmd_data[0]); sb.AppendLine(); sb.AppendFormat("oneSlot[0].cmd_list[0].cmd.cmd_data[Length - 1] = {0}", cmdWithFrame.cmd.cmd_data[DATA_BYTE_LENGTH - 1]); sb.AppendLine(); } } Debug.Log(sb.ToString()); }
void Test1() { MemoryStream oldms = new MemoryStream(); ProtoBuf.Serializer.Serialize(oldms, data); byte[] newbyte = oldms.ToArray(); MemoryStream newms = new MemoryStream(newbyte); ntf_battle_frame_data data2 = ProtoBuf.Serializer.Deserialize <ntf_battle_frame_data>(newms); data.server_curr_frame++; Debug.Log(data2.server_curr_frame); }
void Test2() { msSend.SetLength(SENF_BUFFER_LEN); msSend.Seek(0, SeekOrigin.Begin); ProtoBuf.Serializer.Serialize(msSend, data); // 输出:522,说明Serialize会改变流指针 //Debug.Log((int)msSend.Position); msSend.SetLength(msSend.Position); msSend.Seek(0, SeekOrigin.Begin);//指针一定要复位 ntf_battle_frame_data data2 = ProtoBuf.Serializer.Deserialize <ntf_battle_frame_data>(msSend); data.server_curr_frame++; Debug.Log(data2.server_curr_frame); }
void DeepCopyData(ntf_battle_frame_data source, ntf_battle_frame_data dest) { dest.slot_list[0].cmd_list[0].cmd.UID = source.slot_list[0].cmd_list[0].cmd.UID; dest.slot_list[0].cmd_list[0].cmd.cmd_id = source.slot_list[0].cmd_list[0].cmd.cmd_id; dest.slot_list[0].cmd_list[0].cmd.cmd_data = StreamBufferPool.GetBuffer(DATA_BYTE_LENGTH); for (int i = 0; i < DATA_BYTE_LENGTH; i++) { dest.slot_list[0].cmd_list[0].cmd.cmd_data[i] = source.slot_list[0].cmd_list[0].cmd.cmd_data[i]; } dest.slot_list[0].cmd_list[0].server_frame = source.slot_list[0].cmd_list[0].server_frame; dest.slot_list[0].slot = source.slot_list[0].slot; dest.server_curr_frame = source.server_curr_frame; dest.server_from_slot = source.server_from_slot; dest.server_to_slot = source.server_to_slot; dest.time = source.time; }
void InitData(ntf_battle_frame_data data) { ntf_battle_frame_data.one_slot oneSlot; ntf_battle_frame_data.cmd_with_frame cmdWithFrame; one_cmd oneCmd; oneCmd = new one_cmd(); oneCmd.UID = 1; oneCmd.cmd_id = 1; oneCmd.cmd_data = new byte[DATA_BYTE_LENGTH]; cmdWithFrame = new ntf_battle_frame_data.cmd_with_frame(); cmdWithFrame.server_frame = 1; cmdWithFrame.cmd = oneCmd; oneSlot = new ntf_battle_frame_data.one_slot(); oneSlot.slot = 1; oneSlot.cmd_list.Add(cmdWithFrame); data.server_curr_frame = 1; data.slot_list.Add(oneSlot); }
void Test5() { msSend.SetLength(SENF_BUFFER_LEN); msSend.Seek(0, SeekOrigin.Begin); ntf_battle_frame_data dataTmp = ProtoFactory.Get <ntf_battle_frame_data>(); ntf_battle_frame_data.one_slot oneSlot = ProtoFactory.Get <ntf_battle_frame_data.one_slot>(); ntf_battle_frame_data.cmd_with_frame cmdWithFrame = ProtoFactory.Get <ntf_battle_frame_data.cmd_with_frame>(); one_cmd oneCmd = ProtoFactory.Get <one_cmd>(); cmdWithFrame.cmd = oneCmd; oneSlot.cmd_list.Add(cmdWithFrame); dataTmp.slot_list.Add(oneSlot); DeepCopyData(data, dataTmp); ProtoBufSerializer.Serialize(msSend, dataTmp); ProtoFactory.Recycle(dataTmp); //*************回收,很重要 msSend.SetLength(msSend.Position); //长度一定要设置对 msSend.Seek(0, SeekOrigin.Begin); //指针一定要复位 //msRecive.SetLength(msSend.Length);//同理,但是如果Deserialize指定长度,则不需要设置流长度 msRecive.Seek(0, SeekOrigin.Begin); //同理 Buffer.BlockCopy(msSend.GetBuffer(), 0, msRecive.GetBuffer(), 0, (int)msSend.Length); dataTmp = ProtoBufSerializer.Deserialize(msRecive, typeof(ntf_battle_frame_data), (int)msSend.Length) as ntf_battle_frame_data; PrintData(dataTmp); ProtoFactory.Recycle(dataTmp);//*************回收,很重要 data.server_curr_frame++; data.server_to_slot++; data.server_from_slot++; data.time++; data.slot_list[0].slot++; data.slot_list[0].cmd_list[0].server_frame++; data.slot_list[0].cmd_list[0].cmd.cmd_id++; data.slot_list[0].cmd_list[0].cmd.UID++; data.slot_list[0].cmd_list[0].cmd.cmd_data[0]++; data.slot_list[0].cmd_list[0].cmd.cmd_data[DATA_BYTE_LENGTH - 1]++; }
private void TestLuaEncodeAndCSDecode(byte[] luaEncodeBytes) { #if !FOR_GC_TEST // 打印字节流 Debug.Log("CS receive from Lua =================>>>" + luaEncodeBytes.Length + " bytes : "); var sb = new StringBuilder(); for (int i = 0; i < luaEncodeBytes.Length; i++) { sb.AppendFormat("{0}\t", luaEncodeBytes[i]); } Logger.Log(sb.ToString()); #endif // 解析协议 msRecive.ResetStream(); msRecive.CopyFrom(luaEncodeBytes, 0, 0, luaEncodeBytes.Length); ntf_battle_frame_data dataTmp = ProtoBufSerializer.Deserialize(msRecive.memStream, typeof(ntf_battle_frame_data), (int)luaEncodeBytes.Length) as ntf_battle_frame_data; #if !FOR_GC_TEST PrintData(dataTmp); #endif ProtoFactory.Recycle(dataTmp);//*************回收,很重要 }
private void TestCSEncodeAndLuaDeconde() { #if !FOR_GC_TEST Logger.Log("=========================NewRound========================="); #endif msSend.ResetStream(); ntf_battle_frame_data dataTmp = ProtoFactory.Get <ntf_battle_frame_data>(); ntf_battle_frame_data.one_slot oneSlot = ProtoFactory.Get <ntf_battle_frame_data.one_slot>(); ntf_battle_frame_data.cmd_with_frame cmdWithFrame = ProtoFactory.Get <ntf_battle_frame_data.cmd_with_frame>(); one_cmd oneCmd = ProtoFactory.Get <one_cmd>(); cmdWithFrame.cmd = oneCmd; oneSlot.cmd_list.Add(cmdWithFrame); dataTmp.slot_list.Add(oneSlot); DeepCopyData(data, dataTmp); ProtoBufSerializer.Serialize(msSend.memStream, dataTmp); ProtoFactory.Recycle(dataTmp);//*************回收,很重要 byte[] sendBytes = StreamBufferPool.GetBuffer(msSend, 0, (int)msSend.Position()); #if !FOR_GC_TEST // 打印字节流和数据 Debug.Log("CS send to Lua =================>>>" + sendBytes.Length + " bytes : "); var sb = new StringBuilder(); for (int i = 0; i < sendBytes.Length; i++) { sb.AppendFormat("{0}\t", sendBytes[i]); } Logger.Log(sb.ToString()); PrintData(data); #endif ForCSCallLua(sendBytes); IncreaseData(); StreamBufferPool.RecycleBuffer(sendBytes); }