private static void OnClientConnected(ClientConnectedEventArgs args) { // Check header of connection request //string identifier = args.Message.ReadString(); //if (identifier != token) { // Console.WriteLine(" - " + "Bad identifier!"); // return; //} if (args.Message.LengthBytes < 3) { Console.WriteLine(" - " + "Corrupted OnClientConnected message!"); return; } byte major = args.Message.ReadByte(); byte minor = args.Message.ReadByte(); byte build = args.Message.ReadByte(); if (major < neededMajor || (major == neededMajor && (minor < neededMinor || (major == neededMajor && build < neededBuild)))) { Console.WriteLine(" - " + "Incompatible version!"); return; } args.Allow = true; players[args.Message.SenderConnection] = new Player { }; }
private void OnClientConnected(ClientConnectedEventArgs args) { if (args.Message.LengthBytes < 4) { args.DenyReason = "incompatible version"; Log.Write(LogType.Error, "Connection of unsupported client (" + args.Message.SenderConnection.RemoteEndPoint + ") was denied!"); return; } byte flags = args.Message.ReadByte(); byte major = args.Message.ReadByte(); byte minor = args.Message.ReadByte(); byte build = args.Message.ReadByte(); if (major < neededMajor || (major == neededMajor && (minor < neededMinor || (major == neededMajor && build < neededBuild)))) { args.DenyReason = "incompatible version"; Log.Write(LogType.Error, "Connection of outdated client (" + args.Message.SenderConnection.RemoteEndPoint + ") was denied!"); return; } byte[] clientIdentifier = args.Message.ReadBytes(16); if (allowOnlyUniqueClients) { lock (sync) { foreach (KeyValuePair <NetConnection, Player> pair in players) { bool isSame = true; for (int i = 0; i < 16; i++) { if (clientIdentifier[i] != pair.Value.ClientIdentifier[i]) { isSame = false; break; } } if (isSame) { args.DenyReason = "already connected"; return; } } } } string userName = args.Message.ReadString(); players[args.Message.SenderConnection] = new Player { Connection = args.Message.SenderConnection, ClientIdentifier = clientIdentifier, UserName = userName, State = PlayerState.NotReady }; }
private void OnClientConnected(ClientConnectedEventArgs args) { if (serverState == ServerState.Unloaded) { args.DenyReason = "server unloaded"; return; } // Check ban status of endpoint if (bannedEndPoints.Contains(args.Message.SenderConnection?.RemoteEndPoint)) { args.DenyReason = "banned"; return; } if (args.Message.LengthBytes < 4) { args.DenyReason = "incompatible version"; #if DEBUG Log.Write(LogType.Warning, "Connection of unsupported client (" + args.Message.SenderConnection.RemoteEndPoint + ") was denied"); #endif return; } byte flags = args.Message.ReadByte(); // ToDo: flags are unused byte major = args.Message.ReadByte(); byte minor = args.Message.ReadByte(); byte build = args.Message.ReadByte(); if (major < neededMajor || (major == neededMajor && (minor < neededMinor || (major == neededMajor && build < neededBuild)))) { args.DenyReason = "incompatible version"; #if DEBUG Log.Write(LogType.Warning, "Connection of outdated client (" + args.Message.SenderConnection.RemoteEndPoint + ") was denied"); #endif return; } byte[] clientIdentifier = args.Message.ReadBytes(16); // Check ban status of client identifier if (bannedClientIds.Contains(ClientIdentifierToString(clientIdentifier))) { args.DenyReason = "banned"; return; } if (allowOnlyUniqueClients) { lock (sync) { foreach (KeyValuePair <NetConnection, PlayerClient> pair in players) { bool isSame = true; for (int i = 0; i < 16; i++) { if (clientIdentifier[i] != pair.Value.ClientIdentifier[i]) { isSame = false; break; } } if (isSame) { args.DenyReason = "already connected"; return; } } } } // Ensure that player has unique username string userName = args.Message.ReadString(); lock (sync) { if (string.IsNullOrWhiteSpace(userName)) { do { userName = GenerateRandomUserName(); } while (FindPlayerByUserName(userName) != null); } else { userName = userName.Replace(' ', '_'); if (FindPlayerByUserName(userName) != null) { int number = 1; while (FindPlayerByUserName(userName + "_" + number) != null) { number++; } userName = userName + "_" + number; } } } PlayerClient player = new PlayerClient { Connection = args.Message.SenderConnection, ClientIdentifier = clientIdentifier, UserName = userName, State = PlayerState.NotReady }; lock (sync) { players[args.Message.SenderConnection] = player; } }
private void OnMessage() { try { while (true) { server.MessageReceivedEvent.WaitOne(); NetIncomingMessage msg; while (server.ReadMessage(out msg)) { switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: { NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); #if DEBUG Console.ForegroundColor = ConsoleColor.DarkCyan; Console.Write(" S "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("[" + msg.SenderEndPoint + "] " + status); #endif ClientStatusChangedEventArgs args = new ClientStatusChangedEventArgs(msg.SenderConnection, status); ClientStatusChanged?.Invoke(args); break; } case NetIncomingMessageType.Data: { #if DEBUG__ Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(" R "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes"); #endif MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, false); MessageReceived?.Invoke(args); break; } case NetIncomingMessageType.UnconnectedData: { #if DEBUG Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(" R "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Unconnected [" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes"); #endif MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, true); MessageReceived?.Invoke(args); break; } case NetIncomingMessageType.ConnectionApproval: { ClientConnectedEventArgs args = new ClientConnectedEventArgs(msg); ClientConnected?.Invoke(args); if (args.Allow) { msg.SenderConnection.Approve(); } else { msg.SenderConnection.Deny("Incompatible version"); } break; } case NetIncomingMessageType.DiscoveryRequest: { #if DEBUG Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(" Q "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes"); #endif DiscoveryRequestEventArgs args = new DiscoveryRequestEventArgs(); DiscoveryRequest?.Invoke(args); server.SendDiscoveryResponse(args.Message, msg.SenderEndPoint); break; } #if DEBUG case NetIncomingMessageType.VerboseDebugMessage: Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write(" D "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.DebugMessage: Console.ForegroundColor = ConsoleColor.Green; Console.Write(" D "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.WarningMessage: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(" W "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.ErrorMessage: Console.ForegroundColor = ConsoleColor.Red; Console.Write(" E "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; #endif } server.Recycle(msg); } } } catch (ThreadAbortException) { // Server is stopped } }
private void OnHandleMessagesThread() { while (server.Status != NetPeerStatus.NotRunning) { server.MessageReceivedEvent.WaitOne(); NetIncomingMessage msg; while (server.ReadMessage(out msg)) { switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: { NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); #if NETWORK_DEBUG Console.ForegroundColor = ConsoleColor.DarkCyan; Console.Write(" S "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("[" + msg.SenderEndPoint + "] " + status); #endif ClientStatusChangedEventArgs args = new ClientStatusChangedEventArgs(msg.SenderConnection, status); ClientStatusChanged?.Invoke(args); break; } case NetIncomingMessageType.Data: { #if NETWORK_DEBUG__ Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(" R "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes"); #endif MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, false); MessageReceived?.Invoke(args); break; } case NetIncomingMessageType.UnconnectedData: { #if NETWORK_DEBUG Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(" R "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Unconnected [" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes"); #endif MessageReceivedEventArgs args = new MessageReceivedEventArgs(msg, true); MessageReceived?.Invoke(args); break; } case NetIncomingMessageType.ConnectionApproval: { ClientConnectedEventArgs args = new ClientConnectedEventArgs(msg); ClientConnected?.Invoke(args); if (args.DenyReason == null) { msg.SenderConnection.Approve(); } else { msg.SenderConnection.Deny(args.DenyReason); } break; } case NetIncomingMessageType.DiscoveryRequest: { #if NETWORK_DEBUG Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(" Q "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("[" + msg.SenderEndPoint + "] " + msg.LengthBytes + " bytes"); #endif DiscoveryRequestEventArgs args = new DiscoveryRequestEventArgs(); DiscoveryRequest?.Invoke(args); server.SendDiscoveryResponse(args.Message, msg.SenderEndPoint); break; } #if NETWORK_DEBUG case NetIncomingMessageType.VerboseDebugMessage: Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write(" D "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.DebugMessage: Console.ForegroundColor = ConsoleColor.Green; Console.Write(" D "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.WarningMessage: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(" W "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.ErrorMessage: Console.ForegroundColor = ConsoleColor.Red; Console.Write(" E "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(msg.ReadString()); break; #endif } server.Recycle(msg); } } server = null; Debug.WriteLine("ServerConnection: OnHandleMessagesThread exited!"); }