void ClientLoop() { int pingTimer = 0; Logger.Instance.Log("Client connected!"); while (_clientLoopThread.IsAlive) { try { if (_client != null && _client.Connected) { pingTimer++; if (pingTimer > 180) { SendToClient(JsonConvert.SerializeObject(new ServerCommand(ServerCommandType.Ping))); pingTimer = 0; } string[] commands = ReceiveFromClient(); if (commands != null) { foreach (string data in commands) { ClientCommand command = JsonConvert.DeserializeObject <ClientCommand>(data); if (command.version != Assembly.GetEntryAssembly().GetName().Version.ToString()) { state = ClientState.UpdateRequired; SendToClient(JsonConvert.SerializeObject(new ServerCommand(ServerCommandType.UpdateRequired))); return; } if (state != ClientState.Playing && state != ClientState.Connected) { state = ClientState.Connected; } switch (command.commandType) { case ClientCommandType.SetPlayerInfo: { PlayerInfo receivedPlayerInfo = JsonConvert.DeserializeObject <PlayerInfo>(command.playerInfo); if (receivedPlayerInfo != null) { if (ServerMain.serverState == ServerState.Playing) { state = ClientState.Playing; } if (playerId == 0) { playerId = receivedPlayerInfo.playerId; if (!ServerMain.clients.Contains(this)) { ServerMain.clients.Add(this); Logger.Instance.Log("New player: " + receivedPlayerInfo.playerName); } } else if (playerId != receivedPlayerInfo.playerId) { return; } if (playerName == null) { playerName = receivedPlayerInfo.playerName; } else if (playerName != receivedPlayerInfo.playerName) { return; } playerScore = receivedPlayerInfo.playerScore; playerInfo = receivedPlayerInfo; } } ; break; case ClientCommandType.GetServerState: { if (ServerMain.serverState != ServerState.Playing) { SendToClient( JsonConvert.SerializeObject( new ServerCommand(ServerCommandType.SetServerState))); } else { SendToClient(JsonConvert.SerializeObject(new ServerCommand( ServerCommandType.SetServerState, _selectedLevelID: ServerMain.availableSongs[ServerMain.currentSongIndex].levelId, _selectedSongDuration: ServerMain.availableSongs[ServerMain.currentSongIndex].duration.TotalSeconds, _selectedSongPlayTime: ServerMain.playTime.TotalSeconds))); } } ; break; case ClientCommandType.GetAvailableSongs: { SendToClient(JsonConvert.SerializeObject(new ServerCommand( ServerCommandType.DownloadSongs, _songs: ServerMain.availableSongs.Select(x => x.levelId).ToArray()))); } ; break; } } } while (sendQueue.Count != 0) { SendToClient(sendQueue.Dequeue()); } } else { ServerMain.clients.Remove(this); if (_client != null) { _client.Close(); _client = null; } state = ClientState.Disconnected; Logger.Instance.Log("Client disconnected!"); return; } }catch (Exception e) { Logger.Instance.Warning($"CLIENT EXCEPTION: {e}"); } Thread.Sleep(8); } }
void ClientLoop() { int pingTimer = 0; Console.WriteLine("Client connected!"); while (true) { if (_client != null && _client.Connected) { pingTimer++; if (pingTimer > 180) { SendToClient(JsonConvert.SerializeObject(new ServerCommand(ServerCommandType.Ping))); pingTimer = 0; } string[] commands = ReceiveFromClient(true); if (commands != null) { foreach (string data in commands) { ClientCommand command = JsonConvert.DeserializeObject <ClientCommand>(data); switch (command.commandType) { case ClientCommandType.SetPlayerInfo: { PlayerInfo receivedPlayerInfo = JsonConvert.DeserializeObject <PlayerInfo>(command.playerInfo); if (receivedPlayerInfo != null) { if (playerId == null) { playerId = receivedPlayerInfo.playerId; if (!ServerMain.clients.Contains(this)) { ServerMain.clients.Add(this); Console.WriteLine("New player: " + receivedPlayerInfo.playerName); } } else if (playerId != receivedPlayerInfo.playerId) { return; } playerInfo = receivedPlayerInfo; if (playerName == null) { playerName = receivedPlayerInfo.playerName; } else if (playerName != receivedPlayerInfo.playerName) { return; } playerScore = receivedPlayerInfo.playerScore; Console.WriteLine("New " + playerName + "'s score: " + playerScore); } }; break; case ClientCommandType.GetServerState: { if (ServerMain.serverState != ServerState.Playing) { SendToClient(JsonConvert.SerializeObject(new ServerCommand(ServerCommandType.SetServerState))); } else { Console.WriteLine(ServerMain.playTime); SendToClient(JsonConvert.SerializeObject(new ServerCommand(ServerCommandType.SetServerState, _selectedSongDuration: ServerMain.availableSongs[ServerMain.currentSongIndex].duration.TotalSeconds, _selectedSongPlayTime: ServerMain.playTime.TotalSeconds))); } }; break; case ClientCommandType.GetAvailableSongs: { SendToClient(JsonConvert.SerializeObject(new ServerCommand(ServerCommandType.DownloadSongs, _songs: ServerMain.availableSongs.Select(x => x.levelId).ToArray()))); }; break; } } } } else { ServerMain.clients.Remove(this); if (_client != null) { _client.Close(); _client = null; } Console.WriteLine("Client disconnected!"); return; } Thread.Sleep(16); } }
void ClientLoop() { int pingTimer = 0; Logger.Instance.Log("Client connected!"); if (Misc.Settings.Instance.Server.MaxPlayers != 0) { if (Misc.Settings.Instance.Server.MaxPlayers < ServerMain.clients.Count) { KickClient("Too many players"); return; } } while (_clientLoopThread.IsAlive) { try { if (_client != null && _client.Connected) { clientIP = ((IPEndPoint)_client.Client.RemoteEndPoint).Address.ToString(); pingTimer++; if (pingTimer > 180) { SendToClient(new ServerCommand(ServerCommandType.Ping)); pingTimer = 0; } string[] commands = ReceiveFromClient(); if (commands != null) { foreach (string data in commands) { ClientCommand command = JsonConvert.DeserializeObject <ClientCommand>(data); Version clientVersion = new Version(command.version); if (clientVersion.Major != ServerMain.serverVersion.Major || clientVersion.Minor != ServerMain.serverVersion.Minor || clientVersion.Build != ServerMain.serverVersion.Build) { state = ClientState.UpdateRequired; SendToClient(new ServerCommand(ServerCommandType.UpdateRequired)); DestroyClient(); } if (state != ClientState.Playing && state != ClientState.Connected) { state = ClientState.Connected; } switch (command.commandType) { case ClientCommandType.SetPlayerInfo: { PlayerInfo receivedPlayerInfo = JsonConvert.DeserializeObject <PlayerInfo>(command.playerInfo); if (receivedPlayerInfo != null) { if (ServerMain.serverState == ServerState.Playing) { state = ClientState.Playing; } if (playerId == 0) { playerId = receivedPlayerInfo.playerId; if (!ServerMain.clients.Contains(this)) { ServerMain.clients.Add(this); Logger.Instance.Log("New player: " + receivedPlayerInfo.playerName); } } else if (playerId != receivedPlayerInfo.playerId) { return; } if (playerName == null) { playerName = receivedPlayerInfo.playerName; } else if (playerName != receivedPlayerInfo.playerName) { return; } playerScore = receivedPlayerInfo.playerScore; playerInfo = receivedPlayerInfo; if (Misc.Settings.Instance.Access.Blacklist.Contains(receivedPlayerInfo.playerId.ToString()) || Misc.Settings.Instance.Access.Blacklist.Contains(clientIP)) { KickClient(); return; } if (Misc.Settings.Instance.Access.WhitelistEnabled && !Misc.Settings.Instance.Access.Whitelist.Contains(receivedPlayerInfo.playerId.ToString()) && !Misc.Settings.Instance.Access.Whitelist.Contains(clientIP)) { KickClient(); return; } } } ; break; case ClientCommandType.GetServerState: { if (ServerMain.serverState != ServerState.Playing) { SendToClient(new ServerCommand(ServerCommandType.SetServerState)); } else { SendToClient(new ServerCommand( ServerCommandType.SetServerState, _selectedSongDuration: ServerMain.availableSongs[ServerMain.currentSongIndex].duration.TotalSeconds, _selectedSongPlayTime: ServerMain.playTime.TotalSeconds)); } } ; break; case ClientCommandType.GetAvailableSongs: { SendToClient(new ServerCommand( ServerCommandType.DownloadSongs, _songs: ServerMain.availableSongs.Select(x => x.levelId).ToArray())); }; break; case ClientCommandType.VoteForSong: { if (ServerMain.serverState == ServerState.Voting) { votedFor = ServerMain.availableSongs.FirstOrDefault(x => x.levelId.Substring(0, 32) == command.voteForLevelId.Substring(0, 32)); } }; break; } } } while (sendQueue.Count != 0) { SendToClient(sendQueue.Dequeue()); } } else { if ((ServerMain.serverState == ServerState.Playing && ServerMain.playTime.TotalSeconds <= ServerMain.availableSongs[ServerMain.currentSongIndex].duration.TotalSeconds - 10f) || ServerMain.serverState != ServerState.Playing) { DestroyClient(); return; } } }catch (Exception e) { Logger.Instance.Warning($"CLIENT EXCEPTION: {e}"); } Thread.Sleep(8); } }