Example #1
0
 private void Disconnect()
 {
     Logger.LogAlert("Session.Disconnect: Disconnecting");
     if (inputThread != null && inputThread.IsAlive)
     {
         inputThread.Abort();
     }
     if (outputThread != null && outputThread.IsAlive)
     {
         outputThread.Abort();
     }
     if (reader != null)
     {
         reader.Close();
     }
     if (writer != null)
     {
         writer.Close();
     }
     if (client != null)
     {
         if (client.Connected)
         {
             client.Client.Close();
         }
         client.Close();
     }
 }
Example #2
0
        // login logic
        private bool LoginSequence()
        {
            PacketReader loginPacket = new PacketReader(reader);

            if (loginPacket.opcode != (uint)InputCodes.Handshake)
            {
                Logger.LogAlert("Session.LoginSequence: Unexpected opcode in the first packet: " + loginPacket.opcode);
                LoginFailure("Unexpected handshake message - possible protocol mismatch!");
                return(false);
            }

            int clientProtocolVersion = (int)loginPacket.ReadByte();

            if (clientProtocolVersion != Config.ProtocolVersion)
            {
                Logger.LogAlert("Session.LoginSequence: Wrong protocol version: " + clientProtocolVersion);
                LoginFailure("Incompatible protocol version!");
                return(false);
            }

            player.name = loginPacket.ReadString();
            if (!Player.IsValidName(player.name))
            {
                Logger.LogAlert("Session.LoginSequence: Unacceptible player name: " + player.name);
                LoginFailure("Invalid characters in player name!");
                return(false);
            }

            Logger.LogAlert("Session.LoginSequence: Success! " + player.name + " authenticated.");
            LoginSuccess();

            // string verificationKey = loginPacket.ReadString();
            // byte unused = loginPacket.ReadByte();
            return(true);
        }
Example #3
0
        public static bool Init(string configFileName)
        {
            // generate random salt
            Random random = new Random();

            Salt = (long)random.Next() * (long)random.Next();
            Logger.Log("Config: Salt = " + Salt);

            // try to load config file (XML)
            XDocument file;

            if (File.Exists(configFileName))
            {
                try {
                    file = XDocument.Load(configFileName);
                    if (file.Root == null || file.Root.Name != ConfigRootName)
                    {
                        Logger.LogAlert("Config.Init: Malformed or incompatible config file " + configFileName + ". Loading defaults.");
                        file = new XDocument();
                        file.Add(new XElement(ConfigRootName));
                    }
                    else
                    {
                        Logger.Log("Config.Init: Config file " + configFileName + " loaded succesfully.");
                    }
                } catch (Exception ex) {
                    Logger.LogError("Config.Init: Fatal error while loading config file " + configFileName + ": " + ex.Message);
                    return(false);
                }
            }
            else
            {
                // create a new one (with defaults) if no file exists
                file = new XDocument();
                file.Add(new XElement(ConfigRootName));
            }

            XElement config = file.Root;

            // load settings
            ServerName  = ReadString(config, "ServerName", DefaultServerName);
            MOTD        = ReadString(config, "MOTD", DefaultMOTD);
            MaxPlayers  = ReadInt(config, "MaxPlayers", DefaultMaxPlayers);
            IsPublic    = ReadBool(config, "IsPublic", DefaultIsPublic);
            VerifyNames = ReadBool(config, "VerifyNames", DefaultVerifyNames);

            // save the settings
            try {
                file.Save(configFileName);
            } catch (Exception ex) {
                Logger.LogError("Config.Init: Fatal error while saving config file " + configFileName + ": " + ex.Message);
                return(false);
            }
            return(true);
        }
Example #4
0
 public void ShutDown()
 {
     Logger.LogAlert("Server.ShutDown: Shutting down");
     if (listenerThread != null && listenerThread.IsAlive)
     {
         listenerThread.Abort();
     }
     if (listener != null)
     {
         listener.Stop();
     }
 }