void UpdateClients(bool startGame = false) { var remove = Members.Where(x => x.Key.Connected == false); foreach (var v in remove) { Members.Remove(v.Key); } LobbyUpdatePackage lup = new LobbyUpdatePackage(); lup.LobbyId = LobbyId; lup.Start = startGame; foreach (var v in Members) { string address = ((IPEndPoint)v.Key.Client.RemoteEndPoint).Address.ToString(); if (!lup.Members.ContainsKey(address)) { lup.Members.Add(address, v.Value); } } foreach (var v in Members) { Server.Instance.Client.Write(v.Key, lup); Console.WriteLine("Sent lobby update to " + ((IPEndPoint)v.Key.Client.RemoteEndPoint).Address.ToString()); } }
public override DataPackage CreateFromBody(string b) { string[] split = b.Split(IdSeperator); int id = int.Parse(split[0]); bool start = bool.Parse(split[1]); LobbyUpdatePackage prp = new LobbyUpdatePackage(); prp.LobbyId = id; prp.Start = start; if (split.Length > 2) { string[] entries = split[2].Split(EntrySeperator); for (int i = 0; i < entries.Length; i += 2) { string ip = entries[i]; bool ready; bool.TryParse(entries[i + 1], out ready); prp.Members.Add(ip, ready); } } return(prp); }
void UpdateClients(bool startGame = false) { var remove = Members.Where(x => x.Key.Connected == false); foreach (var v in remove) { Members.Remove(v.Key); } LobbyUpdatePackage lup = new LobbyUpdatePackage(); lup.LobbyId = LobbyId; lup.Start = startGame; foreach (var v in Members) { string address = ((IPEndPoint)v.Key.Client.RemoteEndPoint).Address.ToString(); if(!lup.Members.ContainsKey(address)) lup.Members.Add(address, v.Value); } foreach (var v in Members) { Server.Instance.Client.Write(v.Key, lup); Console.WriteLine("Sent lobby update to " + ((IPEndPoint)v.Key.Client.RemoteEndPoint).Address.ToString()); } }
public override DataPackage CreateFromBody(string b) { string[] split = b.Split(IdSeperator); int id = int.Parse(split[0]); bool start = bool.Parse(split[1]); LobbyUpdatePackage prp = new LobbyUpdatePackage(); prp.LobbyId = id; prp.Start = start; if (split.Length > 2) { string[] entries = split[2].Split(EntrySeperator); for (int i = 0; i < entries.Length; i += 2) { string ip = entries[i]; bool ready; bool.TryParse(entries[i + 1], out ready); prp.Members.Add(ip, ready); } } return prp; }
public void OnDataReceived(DataPackage dp) { if (dp is ResponsePackage) { ResponsePackage rp = (ResponsePackage)dp; ResponseReceived(rp.ResponseId, rp); waitForResponse.Remove(rp.ResponseId); } else if (dp is LobbyUpdatePackage) { LobbyUpdatePackage lup = (LobbyUpdatePackage)dp; if (GetLobbyId(currentLobby) != lup.LobbyId) { return; } currentLobby.clients.Clear(); currentLobby.clients = lup.Members; if (lup.Start) { //c2s.Dispose(); List <TcpClient> clients = new List <TcpClient>(); foreach (var v in currentLobby.clients) { clients.Add(NetworkManager.Instance.Client.Connect(v.Key)); } for (int i = 0; i < clients.Count; i++) { if (clients[i].GetRemoteIPEndPoint().Address.Equals(Client.GetLocalIPAddress())) { if (i == 0) { NetworkManager.Instance.Client.SetHasToken(true); } TcpClient next = null; if (i != clients.Count - 1) { next = clients[i + 1]; } else { next = clients[0]; } if (NetworkManager.Instance.Client.OnLog != null) { NetworkManager.Instance.Client.OnLog("Token ring: I am ip " + Client.GetLocalIPAddress() + ", number " + i + " in the token ring and next in the ring is " + next.GetRemoteIPEndPoint().Address.ToString()); } NetworkManager.Instance.Client.SetNextTokenClient(next); break; } } Application.LoadLevel("TerrainMap"); } } }