Exemple #1
0
        protected static void Save <T>(T conf) where T : Configuration
        {
            // TODO: Swap old file to .bak, to recover on error
            using (FileStream fs = new FileStream(conf.path, FileMode.Create))
                using (XmlDictionaryWriter jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(fs, Encoding.UTF8, true, true, "  "))
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    ser.WriteObject(jsonWriter, conf);
                }

            _logger.Debug($"Configuration file saved: {conf.path}");
        }
Exemple #2
0
 public void Disconnect()
 {
     if (this._connected)
     {
         this._connected = false;
         this.Socket.Shutdown(SocketShutdown.Both);
         this.Socket.Disconnect(false);
         this.OnDisconnect?.Invoke(this, EventArgs.Empty);
         _logger.Debug("Client " + this.Ip.ToString() + " disconnected.");
     }
 }
Exemple #3
0
        protected void AcceptHandler(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            _connAccepted.Set();

            // Get the socket that handles the client request.
            Socket clientSock = this._socket.EndAccept(ar);

            _logger.Debug($"Client connected from {clientSock.RemoteEndPoint}");

            // Create client
            InputConnection client = new InputConnection(clientSock);

            // Emit events and run default class handler
            this.OnConnection(client);
            NewConnection?.Invoke(client);
        }
Exemple #4
0
        /// <summary>
        /// Handle messages sended from a player
        /// </summary>
        /// <param name="player"></param>
        /// <param name="data"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void PlayerMessageHandler(Player player, MemoryStream data)
        {
            try
            {
                List <Packet> messages = PacketHandler.Handle(data);
                messages.ForEach((message) => _logger.Trace($"Player => Server: {message.ToString()}"));

                foreach (Packet message in messages)
                {
                    switch (message.Type)
                    {
                    case PacketType.RequestJoinZone:
                        this.HandleJoinZoneRequest(player, (RequestJoinZonePacket)message);
                        break;

                    case PacketType.RequestSetAlias:
                        player.SetAlias((message as RequestSetAliasPacket)?.Alias);
                        break;

                    default:
                        _logger.Warning($"Unrecognized message: {message.ToString()}");
                        throw new NotImplementedException();
                    }
                }
            }
            catch (UnrecognizedPacketException ex)
            {
                _logger.Warning($"Unrecognized packet from client {player.Connection.Ip}: {ex.Message}");
                player.Disconnect();
            }
            catch (PlayerRejectedException rejected)
            {
                _logger.Debug($"Player {player.Name} rejected: {rejected.Message}");
                // TODO: rejected.Player.SendMessage(rejected.Message);
                this.RemovePlayer(rejected.Player);
            }
        }