Exemple #1
0
        /// <summary>
        /// Handle new connection messages
        /// </summary>
        /// <param name="connection">Connection that send the message</param>
        /// <param name="data">Message raw data</param>
        private void NewConnectionHandler(InputConnection connection, MemoryStream data)
        {
            try
            {
                List <Packet> messages     = PacketHandler.Handle(data);
                Packet        firstMessage = messages.FirstOrDefault();
                _logger.Trace($"Client => Server: {firstMessage?.ToString()}");

                switch (firstMessage?.Type) // FIX: RequestID is always an only packet?
                {
                case PacketType.HttpRequest:
                    if (this._httpServerPort > 0)
                    {
                        connection.Send(Encoding.ASCII.GetBytes(
                                            "HTTP/1.1 301 Moved Permanently\nLocation: http://" +
                                            this._localEndPoint.Address.ToString() +
                                            (this._httpServerPort != 80 ? ":" + this._httpServerPort.ToString() : "") +
                                            "\nConnection: close\n\n")); // TODO: Set public IP or domain
                    }
                    else
                    {
                        connection.Send(Encoding.ASCII.GetBytes(
                                            "HTTP/1.1 503 Service Unavailable\nConnection: close\n\nThis is not a web server"));
                    }
                    connection.Disconnect();
                    break;

                case PacketType.RequestID:
                    RequestIdPacket reqId     = firstMessage as RequestIdPacket;
                    Player          newPlayer = new Player(reqId?.Name, connection);

                    // TODO: Check bans. connection.Send(ResponseIdPacket.RejectPlayer().ToBinary());
                    this.AddPlayer(newPlayer);
                    break;

                default:
                    _logger.Warning(
                        $"Packet not expected on new connection from {connection.Ip}: {Enum.GetName(typeof(PacketType), firstMessage?.Type)}");
                    break;
                }

                if (messages.Count > 1)
                {
                    _logger.Warning("Multiple messages on new connection not expected.");
                }
            }
            catch (UnrecognizedPacketException ex)
            {
                _logger.Warning($"Unrecognized packet from client {connection.Ip}: {ex.Message}");
                connection.Disconnect();
            }
            catch (VersionNotFoundException ex)
            {
                _logger.Warning($"Protocol version mismatch from client {connection.Ip}: {ex.Message}");
                connection.Disconnect();
            }

            // Remove new connection handler. This isn't new already.
            connection.OnMessage -= this._newConnectionEventHandler;
        }