public void ReadData() { // lock(Serialization.lockObject) int ReiveBufLength = clientSocket.ReceiveData(BigRecivebuffer); int msgcount = 0; if (ReiveBufLength > 4) { MemoryStream ms = new MemoryStream(BigRecivebuffer, 0, ReiveBufLength); BinaryReader br = new BinaryReader(ms); int currentIndex = 0; while (currentIndex + 4 < ReiveBufLength) { int len = br.ReadInt32(); byte[] result = br.ReadBytes(len); MemoryStream Tms = new MemoryStream(result); DS_protocol.p_AllMsg p_allMsg = Serializer.Deserialize <DS_protocol.p_AllMsg>(Tms); NetCommon.HandleData(this, p_allMsg); msgcount++; currentIndex += 4 + len; } } // Console.WriteLine("recive , ReiveBufLength " + (ReiveBufLength + 4) + " Count " + msgcount); }
public void RecordData(byte[] data, int length, List <DS_protocol.p_AllMsg> TMsgs, List <int> TempMsgConenct) { if (data != null && length > 4) { MemoryStream ms = new MemoryStream(data, 0, length); BinaryReader br = new BinaryReader(ms); int currentIndex = 0; while (currentIndex + 4 < length) { int len = br.ReadInt32(); byte[] result = br.ReadBytes(len); MemoryStream Tms = new MemoryStream(result); DS_protocol.p_AllMsg p_allMsg = null; try { UnityEngine.Profiling.Profiler.BeginSample("RecordData::Deserialize"); p_allMsg = Serializer.Deserialize <DS_protocol.p_AllMsg>(Tms); UnityEngine.Profiling.Profiler.EndSample(); } catch (Exception) { return; } if (p_allMsg.type != (int)CmdType.ASKFRAME) { UnityEngine.Profiling.Profiler.BeginSample("RecordData::HandleData"); NetCommon.HandleData(connectionID, p_allMsg); UnityEngine.Profiling.Profiler.EndSample(); } else { TempMsgConenct.Add(connectionID); TMsgs.Add(p_allMsg); } currentIndex += len + 4; } } }
public static void HandleData(int clientID, DS_protocol.p_AllMsg p_allmsg) { //p_AllMsg ReceiveMsg = new p_AllMsg(); int msg_type = p_allmsg.type; //int seq = p_allmsg.BaseProtocol.seq; //gameServer.hanldeSeq(client_id, seq); //Console.WriteLine("get client: " + client_id + "seq pkg : " + seq); if (msg_type == (int)CmdType.START) { Console.Write("get start info\n"); gameServer.PreGameStart(clientID); } else if (msg_type == (int)CmdType.FRAME) { //Console.WriteLine("get frame from client :" + client_id); //int frame_count = p_allmsg.Frame.syncFrame.frame_count; List <CustomSyncMsg> customSyncMsgs = new List <CustomSyncMsg>(); int area_id = DSerializeData(p_allmsg.Frame.buffers, customSyncMsgs); // extract_msg(p_allmsg.Frame.syncFrame.msg_list, area_id); // if (customSyncMsgs.Count != 3) // UnityEngine.Debug.Log("clientID: " + clientID + " area_id- " + area_id + "customSyncMsgs Count " + customSyncMsgs.Count); gameServer.OnGetFrame(clientID, area_id, customSyncMsgs); } else if (msg_type == (int)CmdType.JOIN) { int room_id = p_allmsg.Join.room_id; //int player_id = allMsg.Join.player_id; Console.WriteLine("get join info, it is from :" + clientID + " Progress id" + room_id); gameServer.OnPlayerJoin(clientID, room_id); } else if (msg_type == (int)CmdType.ASKFRAME) { //Console.WriteLine("get asked chase frame request"); UnityEngine.Profiling.Profiler.BeginSample("HandleData::ASKFRAME"); gameServer.OnAskedFrame(clientID, p_allmsg.AskFrame.areas, p_allmsg.AskFrame.frames); UnityEngine.Profiling.Profiler.EndSample(); } }
public static byte[] Serialize(NetworkMsg networkMsg)//在这可以继续添加要发送的Msg { var p_TheAllMsg = new DS_protocol.p_AllMsg(); p_TheAllMsg.player_id = networkMsg.player_id; p_TheAllMsg.type = networkMsg.type; if (networkMsg.type == (int)CmdType.ASKFRAME) { var askFrame = networkMsg as AskFrame; p_TheAllMsg.AskFrame = new DS_protocol.p_AskFrame(); askFrame.areas.ForEach(i => p_TheAllMsg.AskFrame.areas.Add(i)); askFrame.frames.ForEach(i => p_TheAllMsg.AskFrame.frames.Add(i)); } else if (networkMsg.type == (int)CmdType.START) { var startGame = networkMsg as StartGame; p_TheAllMsg.StartGame = new DS_protocol.p_StartGame(); p_TheAllMsg.StartGame.test = 0; } else if (networkMsg.type == (int)CmdType.FRAME) { var frame = networkMsg as Frame; p_TheAllMsg.Frame = new DS_protocol.p_Frame(); p_TheAllMsg.Frame.buffers = SerializeFrame(frame); // p_TheAllMsg.Frame.syncFrame = new DS_protocol.p_SyncFrame(); // p_TheAllMsg.Frame.area_id = frame.syncFrame.get_area_id(); // p_TheAllMsg.Frame.syncFrame.frame_count = frame.syncFrame.get_frame_count(); // Buffer_SyncFrame_msg_list(frame.syncFrame.get_msg(), p_TheAllMsg.Frame.syncFrame.msg_list); } else if (networkMsg.type == (int)CmdType.JOIN) { var join = networkMsg as Join; p_TheAllMsg.Join = new DS_protocol.p_Join(); p_TheAllMsg.Join.room_id = join.room_id; } return(Serialize <DS_protocol.p_AllMsg>(p_TheAllMsg)); }
public static void HandleData(Robot robot, DS_protocol.p_AllMsg p_allmsg) { int msg_type = p_allmsg.type; if (msg_type == (int)CmdType.REPLYJOIN) { int id = p_allmsg.ReplyJoin.real_player_id; List <string> names = new List <string>();// p_allmsg.ReplyJoin.player_names; robot.OnJoinReply(id, names); } else if (msg_type == (int)CmdType.REPLYSTART) { bool flag = p_allmsg.ReplyStart.start; robot.Start(flag); } else if (msg_type == (int)CmdType.REPLYASKFRAME) { var Ttest = DSerializeData(p_allmsg.ReplyAskFrame.buffers); robot.OnGetFrames(Ttest); /* if (Commpare(Ttest, p_allmsg.ReplyAskFrame.areas_to_frames)) * { * robot.OnGetFrames(Ttest); * } * else * { * robot.OnGetFrames(p_allmsg.ReplyAskFrame.areas_to_frames); * * robot.OnGetFrames(Ttest); * }*/ // //robot.OnGetFrames(p_allmsg.ReplyAskFrame.buffers); } else if (msg_type == (int)CmdType.SPAWNINFO) { int areaID = p_allmsg.player_id; //此处msg用playerID 表示了areaID robot.OnInitMsg(areaID); } }
public static byte[] SerializeData(NetworkMsg networkMsg) { var p_AllMsg = new DS_protocol.p_AllMsg(); p_AllMsg.player_id = networkMsg.player_id; p_AllMsg.type = networkMsg.type; if (networkMsg.type == (int)CmdType.REPLYJOIN) { var replyJoin = networkMsg as ReplyJoin; p_AllMsg.ReplyJoin = new DS_protocol.p_ReplyJoin(); p_AllMsg.ReplyJoin.real_player_id = replyJoin.real_player_id; } else if (networkMsg.type == (int)CmdType.REPLYASKFRAME) { var replyAskFrame = networkMsg as ReplyAskFrame; p_AllMsg.ReplyAskFrame = new DS_protocol.p_ReplyAskFrame(); /*UnityEngine.Profiling.Profiler.BeginSample("SerializeDatareplyAskFramelast"); * for (int nLoop = 0; nLoop < replyAskFrame.areas.Count; nLoop++) * { * DS_protocol.p_IdToFrames idtoframe = new DS_protocol.p_IdToFrames(); * * idtoframe.areaid = replyAskFrame.areas[nLoop]; * * List<SyncFrame> list = replyAskFrame.frames[nLoop]; * for (int i = 0; i < list.Count; i++) * { * DS_protocol.p_SyncFrame p_cur = new DS_protocol.p_SyncFrame(); * var cur = list[i]; * //p_cur.area_id = cur.get_area_id(); * p_cur.frame_count = cur.get_frame_count(); * Buffer_SyncFrame_msg_list2(cur.get_msg(),p_cur.msg_list); * idtoframe.frames.Add(p_cur); * } * * p_AllMsg.ReplyAskFrame.areas_to_frames.Add(idtoframe); * } * UnityEngine.Profiling.Profiler.EndSample();*/ UnityEngine.Profiling.Profiler.BeginSample("SerializeDatareplyAskFrame"); var buffers = SerializeDatareplyAskFrame(replyAskFrame); UnityEngine.Profiling.Profiler.EndSample(); try { // UnityEngine.Profiling.Profiler.BeginSample("SerializeDatareplyCompress"); // p_AllMsg.ReplyAskFrame.buffers = Snappy.Sharp.Snappy.Compress(buffers); // UnityEngine.Profiling.Profiler.EndSample(); // UnityEngine.Debug.Log("Compress " +buffers.Length );//+ " "+ p_AllMsg.ReplyAskFrame.buffers.Length ); p_AllMsg.ReplyAskFrame.buffers = buffers; } catch (System.Exception ex) { UnityEngine.Debug.LogError("Compress SendBuffer Failure! "); } } else if (networkMsg.type == (int)CmdType.REPLYSTART) { var replyStart = networkMsg as ReplyStart; p_AllMsg.ReplyStart = new DS_protocol.p_ReplyStart(); p_AllMsg.ReplyStart.start = replyStart.start; } return(Serialize <DS_protocol.p_AllMsg>(p_AllMsg)); }