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++; }
void ClientConnected(Socket s) { Client c = new Client(nmbrOfClients, s, this); clientList.Add(c); c.SendAcceptPoll(); nmbrOfClients++; }
public void Send(MessageBuffer msg, Client c) { if (c != null && c.udpAdress != null) outMessages.Add(new MessageInfo(msg, c.udpAdress, this)); }
public void PingResult(Client c, int millis) { if (OnPing != null) OnPing(c, millis); }
public void ClientDisconnected(Client c) { clientList.Remove(c); disconnectedList.Add(c); }
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 OnDisconnect(Client c) { Console.WriteLine("{0}[{1}, {2}] disconnected!", c.ID, c.tcpAdress, c.udpAdress); clientList.Remove(c); if(userList.ContainsKey(c)) { userList.Remove(c); } }