public void OnConnect(Client c) { Console.WriteLine("{0}[{1}, {2}] connected!", c.ID, c.tcpAdress, c.udpAdress); clientList.Add(c); MessageBuffer msg = new MessageBuffer(); msg.WriteInt(cProto.CONNECT); msg.WriteInt(roleId); c.Send(msg); roleId++; }
public void OnMessage(Client c, MessageBuffer msg) { int cproto = msg.ReadInt(); switch (cproto) { case cProto.CONNECT: break; case cProto.READY: if (!userList.ContainsKey(c)) { int id = msg.ReadInt(); userList.Add(c, id); } //所有的玩家都准备好了,可以开始同步 if (userList.Count >= clientList.Count) { frameCount = 1; keyDic = new Dictionary <int, Dictionary <int, List <string> > >(); string playStr = ""; List <string> playList = new List <string>(); foreach (var play in userList) { CharData charData = new CharData(play.Value, play.Value + "_" + play.Value); playList.Add(charData.ToString()); } playStr = string.Join(";", playList.ToArray()); MessageBuffer buff = new MessageBuffer(); buff.WriteInt(cProto.START); buff.WriteString(playStr); for (int i = 0; i < clientList.Count; ++i) { clientList[i].Send(buff); } } break; case cProto.SYNC_POS: for (int i = 0; i < clientList.Count; ++i) { if (c == clientList[i]) { continue; } clientList[i].Send(msg); } break; case cProto.SYNC_KEY: int clientCurFrameCount = msg.ReadInt(); string keyStr = msg.ReadString(); if (keyDic.ContainsKey(clientCurFrameCount)) { if (keyDic[clientCurFrameCount].ContainsKey(userList[c])) { keyDic[clientCurFrameCount][userList[c]].Add(keyStr); } else { keyDic[clientCurFrameCount][userList[c]] = new List <string>(); keyDic[clientCurFrameCount][userList[c]].Add(keyStr); } } else { keyDic[clientCurFrameCount] = new Dictionary <int, List <string> >(); keyDic[clientCurFrameCount][userList[c]] = new List <string>(); keyDic[clientCurFrameCount][userList[c]].Add(keyStr); } if (clientCurFrameCount == frameCount) { if (keyDic[clientCurFrameCount].Count == clientList.Count) { List <string> keyDataList = new List <string>(); foreach (var dataList in keyDic[clientCurFrameCount].Values) { keyDataList.AddRange(dataList); } string keyData = string.Join(";", keyDataList.ToArray()); MessageBuffer buff = new MessageBuffer(); buff.WriteInt(cProto.SYNC_KEY); buff.WriteInt(frameCount); buff.WriteString(keyData); for (int i = 0; i < clientList.Count; ++i) { clientList[i].Send(buff); } frameCount += 1; } } break; case cProto.START: break; } }
public void OnMessage(Client c, MessageBuffer msg) { int cproto = msg.ReadInt(); switch(cproto) { case cProto.CONNECT: break; case cProto.READY: if (!userList.ContainsKey(c)) { int id = msg.ReadInt(); userList.Add(c, id); } //所有的玩家都准备好了,可以开始同步 if(userList.Count >= clientList.Count) { frameCount = 1; keyDic = new Dictionary<int, Dictionary<int, List<string>>>(); string playStr = ""; List<string> playList = new List<string>(); foreach(var play in userList) { CharData charData = new CharData(play.Value, play.Value+ "_" + play.Value); playList.Add(charData.ToString()); } playStr = string.Join(";", playList.ToArray()); MessageBuffer buff = new MessageBuffer(); buff.WriteInt(cProto.START); buff.WriteString(playStr); for (int i = 0; i < clientList.Count; ++i) { clientList[i].Send(buff); } } break; case cProto.SYNC_POS: for (int i = 0; i < clientList.Count; ++i) { if(c == clientList[i]) { continue; } clientList[i].Send(msg); } break; case cProto.SYNC_KEY: int clientCurFrameCount = msg.ReadInt(); string keyStr = msg.ReadString(); if(keyDic.ContainsKey(clientCurFrameCount)) { if(keyDic[clientCurFrameCount].ContainsKey(userList[c])) { keyDic[clientCurFrameCount][userList[c]].Add(keyStr); } else { keyDic[clientCurFrameCount][userList[c]] = new List<string>(); keyDic[clientCurFrameCount][userList[c]].Add(keyStr); } } else { keyDic[clientCurFrameCount] = new Dictionary<int,List<string>>(); keyDic[clientCurFrameCount][userList[c]] = new List<string>(); keyDic[clientCurFrameCount][userList[c]].Add(keyStr); } if(clientCurFrameCount == frameCount) { if(keyDic[clientCurFrameCount].Count == clientList.Count) { List<string> keyDataList = new List<string>(); foreach(var dataList in keyDic[clientCurFrameCount].Values) { keyDataList.AddRange(dataList); } string keyData = string.Join(";", keyDataList.ToArray()); MessageBuffer buff = new MessageBuffer(); buff.WriteInt(cProto.SYNC_KEY); buff.WriteInt(frameCount); buff.WriteString(keyData); for (int i = 0; i < clientList.Count; ++i) { clientList[i].Send(buff); } frameCount += 1; } } break; case cProto.START: break; } }