private void OnStreamWriteCompleted(IAsyncResult result) { OutputMessage message = (OutputMessage)result.AsyncState; try { Stream.EndWrite(result); if (--PendingWrites > 0) { OutputMessage pendingMessage; if (PendingMessages.TryDequeue(out pendingMessage)) { InternalSend(pendingMessage, true); return; } } if (message.DisconnectAfterMessage) { Disconnect(); } } finally { if (message.IsRecycledMessage) { OutputMessagePool.ReleaseMessage(message); } } }
protected virtual void DispatchDisconnect(string reason) { OutputMessage message = OutputMessagePool.GetOutputMessage(this, false); message.AddByte((byte)ServerPacketType.ErrorMessage); message.AddString(reason); message.DisconnectAfterMessage = true; OutputMessagePool.AddToQueue(message); }
public void Disconnect(string reason, uint version) { OutputMessage message = OutputMessagePool.GetOutputMessage(this, false); if (version > 1076) { message.AddByte((byte)ServerPacketType.Disconnect1076); } else { message.AddByte((byte)ServerPacketType.Disconnect); } message.AddString(reason); message.DisconnectAfterMessage = true; OutputMessagePool.AddToQueue(message); }
private OutputMessage GetOutputBuffer(int size) { if (OutputBuffer != null && OutputMessage.MaxProtocolBodyLength >= OutputBuffer.Length + size) { return(OutputBuffer); } if (Socket != null) { if (OutputBuffer != null) { OutputMessagePool.AddToQueue(OutputBuffer); } OutputBuffer = OutputMessagePool.GetOutputMessage(this); return(OutputBuffer); } return(null); }
protected void DispatcherThread() { //OutputMessagePool* outputPool = OutputMessagePool::getInstance(); Task task; while (State == DispatcherState.Running) { if (TaskList.Count == 0) { Signaler.WaitOne(); } if (TaskList.Count == 0 || State != DispatcherState.Running) { Thread.Sleep(1); continue; } lock (TaskLock) { task = TaskList[0]; TaskList.RemoveAt(0); } if (!task.IsExpired()) { //outputPool->startExecutionFrame(); task.Action.Invoke(); OutputMessagePool.Flush(); //outputPool->sendAll(); //g_game.clearSpectatorCache(); } } State = DispatcherState.Terminated; }
private void HandleLoginPacket(string accountName, byte[] password) { //TODO: Check IP Ban if (string.IsNullOrEmpty(accountName)) { Disconnect("Invalid account name.", Version); return; } byte[] hash = LoginServer.PasswordHasher.ComputeHash(password); string hashedPassword = string.Empty; foreach (byte b in hash) { hashedPassword += b.ToString("x2"); } string sessionKey; AccountModel acc = LoginServerData.RetrieveAccountData(accountName, hashedPassword, out sessionKey); if (acc == null) { Disconnect("Account name or password is not correct.", Version); return; } OutputMessage message = OutputMessagePool.GetOutputMessage(this, false); message.AddByte((byte)ServerPacketType.MOTD); message.AddString(LoginServer.MOTD); message.AddByte((byte)ServerPacketType.SessionKey); message.AddString(sessionKey); message.AddByte((byte)ServerPacketType.CharacterList); message.AddByte((byte)LoginServer.GameWorlds.Count); foreach (GameWorldModel world in LoginServer.GameWorlds.Values) { message.AddByte(world.GameWorldId); message.AddString(world.GameWorldName); message.AddString(world.GameWorldIP); message.AddUInt16(world.GameWorldPort); message.AddByte(0); } message.AddByte((byte)acc.Characters.Count); foreach (AccountCharacterModel character in acc.Characters) { message.AddByte((byte)character.ServerId); message.AddString(character.CharacterName); } if (!acc.PremiumUntil.HasValue) { message.AddUInt16(0xFFFF); } else { TimeSpan premiumLeft = acc.PremiumUntil.Value - DateTime.Now; message.AddUInt16((ushort)premiumLeft.TotalDays); } message.DisconnectAfterMessage = true; OutputMessagePool.AddToQueue(message); }
internal static void Main() { NativeMethods.SetConsoleCtrlHandler(ConsoleCtrlOperationHandler, true); Game.Initialize(); Tools.Initialize(); OutputMessagePool.Initialize(); NetworkMessagePool.Initialize(); Console.Title = Constants.ServerName; Console.Clear(); Console.WriteLine("Welcome to {0} - Version {1}", Constants.ServerName, Constants.ServerVersion); Console.WriteLine("Developed by {0}", Constants.ServerDevelopers); Console.WriteLine(); // Start Threads DispatcherManager.Start(); // Loading config.lua if (!ConfigManager.Load("config.lua")) { ExitApplication(); } if (!Enum.TryParse(ConfigManager.Instance[ConfigStr.MinConsoleLogLevel], true, out Logger.MinConsoleLogLevel)) { Console.WriteLine("LOGGER LOG LEVEL COULD NOT BE PARSED! PLEASE FIX!"); ExitApplication(); } // Setting up process priority switch (ConfigManager.Instance[ConfigStr.DefaultPriority]) { case "realtime": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; break; case "high": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; break; case "higher": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; break; } // Setting up RSA cyrpto if (!Rsa.SetKey(RsaP, RsaQ)) { ExitApplication(); } // Initializing Database connection if (!Database.Initialize()) { ExitApplication(); } //DATABASE MANAGER UPDATE DATABASE //DATABASE TASKS START // Loading vocations if (!Vocation.LoadVocations()) { ExitApplication(); } // Loading items if (!ItemManager.Load()) { ExitApplication(); } // Loading Chat Channels if (!Chat.Load()) { ExitApplication(); } // Loading scripts if (!ScriptManager.LoadCsScripts() || !ScriptManager.LoadLuaScripts()) { ExitApplication(); } // Loading Command Line Operations if (!ScriptManager.LoadCommandLineOperations()) { ExitApplication(); } // LOAD CREATURES HERE // Loading outfits if (!OutfitManager.Load()) { ExitApplication(); } // Loading map if (!Map.Load()) { ExitApplication(); } // Setting game world type switch (ConfigManager.Instance[ConfigStr.WorldType]) { case "pvp": Game.WorldType = GameWorldTypes.Pvp; break; case "no-pvp": Game.WorldType = GameWorldTypes.NoPvp; break; case "pvp-enforced": Game.WorldType = GameWorldTypes.PvpEnforced; break; default: Logger.Log(LogLevels.Error, "Invalid game world type: " + ConfigManager.Instance[ConfigStr.WorldType]); ExitApplication(); break; } Logger.Log(LogLevels.Operation, "Setting Game World Type: " + Game.WorldType); // Initialize Game State Game.GameState = GameStates.Init; //TODO: HOUSE RENTS //TODO: MARKET CHECK OFFERS //TODO: MARKET STATISTICS if (ConfigManager.Instance[ConfigBool.UseExternalLoginServer]) { //Create secret communication channel with login server if login server is external if (!SecretServerConnection.Initialize()) { ExitApplication(); } // Create signal waiting system to get authentication response from external login server } else { _loginServer = new LoginServer.LoginServer(); _loginServer.Start(); } GameServer.Start(); Game.GameState = GameStates.Normal; Game.StartJobs(); //TODO: FIRE SERVER RUNNING EVENT while (true) { string input = Console.ReadLine(); if (input == null) { continue; } string[] firstPass = input.Split('"'); List <string> secondPass = firstPass[0].Trim().Split(' ').ToList(); if (firstPass.Length > 1) { for (int i = 1; i < firstPass.Length; i++) { if (i % 2 == 1) { secondPass.Add(firstPass[i]); } else { secondPass.AddRange(firstPass[i].Trim().Split(' ')); } } } string[] command = secondPass.ToArray(); if (ScriptManager.CommandLineOperations.ContainsKey(command[0])) { try { ScriptManager.CommandLineOperations[command[0]].Invoke(command); } catch (Exception) { Logger.Log(LogLevels.Warning, "Command '" + command[0] + "' could not be executed in this environment!"); } } else { Logger.Log(LogLevels.Warning, "Command is unknown!"); } } // ReSharper disable FunctionNeverReturns }