private void Loop() { var writer = new NetDataWriter(); var broadcastPacket = new LobbyBroadcastPacket() { ClientId = ClientInstance.Id }; while (_isRunning) { broadcastPacket.Username = Username; broadcastPacket.IsHosting = _game.SkirmishManager?.IsHosting ?? false; writer.Reset(); _processor.Write(writer, broadcastPacket); _manager.SendBroadcast(writer, Ports.LobbyScan); _manager.PollEvents(); var removedCount = _players.RemoveAll(IsTimedOut); if (removedCount > 0) { Logger.Info($"Timeout: Removed {removedCount} players from lobby."); } Thread.Sleep(100); } bool IsTimedOut(LobbyPlayer player) => (DateTime.Now - player.LastSeen).TotalSeconds >= 3.0; }
private void LobbyBroadcastReceived(LobbyBroadcastPacket packet, IPEndPoint endPoint) { var player = _players.FirstOrDefault(p => p.EndPoint.Equals(endPoint) && p.ProcessId == packet.ProcessId); if (player == null) { player = new LobbyPlayer() { EndPoint = endPoint, ProcessId = packet.ProcessId }; _players.Add(player); } player.Username = packet.Username; player.IsHosting = packet.IsHosting; player.LastSeen = DateTime.Now; }
private GameSession AddLobbyGameSession(LobbyBroadcastPacket packet, IPEndPoint ip = null) { var gameSession = _gameSessions.FirstOrDefault(p => p.Id == packet.Id); if (gameSession == null) { gameSession = new GameSession() { Id = packet.Id, SessionName = packet.SessionName, SessionCreator = packet.SessionCreator, JoinedPlayersNumber = packet.JoinedPlayersNumber, FieldHeight = packet.FieldHeight, FieldWidth = packet.FieldWidth, IP = packet.Ip, BroadcastReceivedTimeStamp = DateTime.Now }; lock (_synch) { _gameSessions.Add(gameSession); } listBoxLobby.Items.Add(packet.ToString()); } else { gameSession.JoinedPlayersNumber = packet.JoinedPlayersNumber; gameSession.BroadcastReceivedTimeStamp = DateTime.Now; for (int i = 0; i < listBoxLobby.Items.Count; i++) { var item = listBoxLobby.Items[i]; var lobbyItem = ((string)item); if (lobbyItem.Contains(gameSession.GetKey())) { var index = listBoxLobby.Items.IndexOf(item); if (index >= 0) { listBoxLobby.Items.RemoveAt(index); listBoxLobby.Items.Insert(index, packet.ToString()); } } } } return(gameSession); }
private void buttonCreateGame_Click(object sender, EventArgs e) { var packet = new LobbyBroadcastPacket(); packet.Id = Guid.NewGuid(); packet.SessionName = textBoxSessionName.Text; packet.SessionCreator = textBoxPlayerName.Text; packet.FieldHeight = Convert.ToInt32(textBoxFieldHeight.Text); packet.FieldWidth = Convert.ToInt32(textBoxFieldWIdth.Text); packet.Ip = GetMulticastIp(); Broadcaster.BroadcastGameSession <LobbyBroadcastPacket>(() => { return(packet); }); var gameSession = AddLobbyGameSession(packet); using (var game = new GameForm(gameSession, new GameSettings { PlayerName = textBoxPlayerName.Text, Team = GetTeam() })) { game.Run(); } }
private void buttonCreateGame_Click(object sender, EventArgs e) { var packet = new LobbyBroadcastPacket(); packet.Id = Guid.NewGuid(); packet.SessionName = textBoxSessionName.Text; packet.SessionCreator = textBoxPlayerName.Text; packet.FieldHeight = Convert.ToInt32(textBoxFieldHeight.Text); packet.FieldWidth = Convert.ToInt32(textBoxFieldWIdth.Text); packet.Ip = GetMulticastIp(); Broadcaster.BroadcastGameSession<LobbyBroadcastPacket>(() => { return packet; }); var gameSession = AddLobbyGameSession(packet); using (var game = new GameForm(gameSession, new GameSettings { PlayerName = textBoxPlayerName.Text, Team = GetTeam() })) { game.Run(); } }
private GameSession AddLobbyGameSession(LobbyBroadcastPacket packet, IPEndPoint ip = null) { var gameSession = _gameSessions.FirstOrDefault(p => p.Id == packet.Id); if (gameSession == null) { gameSession = new GameSession() { Id = packet.Id, SessionName = packet.SessionName, SessionCreator = packet.SessionCreator, JoinedPlayersNumber = packet.JoinedPlayersNumber, FieldHeight = packet.FieldHeight, FieldWidth = packet.FieldWidth, IP = packet.Ip, BroadcastReceivedTimeStamp = DateTime.Now }; lock (_synch) { _gameSessions.Add(gameSession); } listBoxLobby.Items.Add(packet.ToString()); } else { gameSession.JoinedPlayersNumber = packet.JoinedPlayersNumber; gameSession.BroadcastReceivedTimeStamp = DateTime.Now; for (int i = 0; i < listBoxLobby.Items.Count; i++) { var item = listBoxLobby.Items[i]; var lobbyItem = ((string)item); if (lobbyItem.Contains(gameSession.GetKey())) { var index = listBoxLobby.Items.IndexOf(item); if (index >= 0) { listBoxLobby.Items.RemoveAt(index); listBoxLobby.Items.Insert(index, packet.ToString()); } } } } return gameSession; }