Advertisement
RegalChivas

LoginServer

Nov 25th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using SagaDB;
  7. using SagaDB.Item;
  8. using SagaLib;
  9. using SagaLogin.Manager;
  10. using SagaLogin.Network.Client;
  11.  
  12. namespace SagaLogin
  13. {
  14.     public class LoginServer
  15.     {
  16.         /// <summary>
  17.         /// The characterdatabase associated to this mapserver.
  18.         /// </summary>
  19.         public static ActorDB charDB;
  20.         public static AccountDB accountDB;
  21.  
  22.         public static bool StartDatabase()
  23.         {
  24.             try
  25.             {
  26.                 charDB = new MySQLActorDB(Configuration.Instance.DBHost, Configuration.Instance.DBPort,
  27.                     Configuration.Instance.DBName, Configuration.Instance.DBUser, Configuration.Instance.DBPass);
  28.                 accountDB = new MySQLAccountDB(Configuration.Instance.DBHost, Configuration.Instance.DBPort,
  29.                     Configuration.Instance.DBName, Configuration.Instance.DBUser, Configuration.Instance.DBPass);
  30.                 charDB.Connect();
  31.                 accountDB.Connect();
  32.                 return true;
  33.             }
  34.             catch (Exception)
  35.             {
  36.                 return false;
  37.             }
  38.         }
  39.  
  40.         public static void EnsureCharDB()
  41.         {
  42.             bool notConnected = false;
  43.  
  44.             if (!charDB.isConnected())
  45.             {
  46.                 Logger.ShowWarning("LOST CONNECTION TO CHAR DB SERVER!", null);
  47.                 notConnected = true;
  48.             }
  49.             while (notConnected)
  50.             {
  51.                 Logger.ShowInfo("Trying to reconnect to char db server ..", null);
  52.                 charDB.Connect();
  53.                 if (!charDB.isConnected())
  54.                 {
  55.                     Logger.ShowError("Failed.. Trying again in 10sec", null);
  56.                     System.Threading.Thread.Sleep(10000);
  57.                     notConnected = true;
  58.                 }
  59.                 else
  60.                 {
  61.                     Logger.ShowInfo("SUCCESSFULLY RE-CONNECTED to char db server...", null);
  62.                     Logger.ShowInfo("Clients can now connect again", null);
  63.                     notConnected = false;
  64.                 }
  65.             }
  66.         }
  67.        
  68.         public static void EnsureAccountDB()
  69.         {
  70.             bool notConnected = false;
  71.  
  72.             if (!accountDB.isConnected())
  73.             {
  74.                 Logger.ShowWarning("LOST CONNECTION TO CHAR DB SERVER!", null);
  75.                 notConnected = true;
  76.             }
  77.             while (notConnected)
  78.             {
  79.                 Logger.ShowInfo("Trying to reconnect to char db server ..", null);
  80.                 accountDB.Connect();
  81.                 if (!accountDB.isConnected())
  82.                 {
  83.                     Logger.ShowError("Failed.. Trying again in 10sec", null);
  84.                     System.Threading.Thread.Sleep(10000);
  85.                     notConnected = true;
  86.                 }
  87.                 else
  88.                 {
  89.                     Logger.ShowInfo("SUCCESSFULLY RE-CONNECTED to char db server...", null);
  90.                     Logger.ShowInfo("Clients can now connect again", null);
  91.                     notConnected = false;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         static void Main(string[] args)
  97.         {
  98.             Console.CancelKeyPress += new ConsoleCancelEventHandler(ShutingDown);
  99.  
  100.             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  101.             Logger Log = new Logger("SagaLogin.log");
  102.             Logger.defaultlogger = Log;
  103.             Logger.CurrentLogger = Log;
  104.             Console.ForegroundColor = ConsoleColor.Yellow;
  105.             Console.WriteLine("======================================================================");
  106.             Console.ForegroundColor = ConsoleColor.Cyan;
  107.             Console.WriteLine("           SagaECO Login Server - Internal Beta Version                ");
  108.             Console.WriteLine("           (C)2008 The SagaECO Project Development Team                ");
  109.             Console.ForegroundColor = ConsoleColor.Yellow;
  110.             Console.WriteLine("======================================================================");
  111.             Console.ResetColor();
  112.             Logger.ShowInfo("Starting Initialization...", null);
  113.  
  114.             Configuration.Instance.Initialization("./Config/SagaLogin.xml");
  115.  
  116.             Logger.CurrentLogger.LogLevel = (Logger.LogContent)Configuration.Instance.LogLevel;
  117.  
  118.             ItemFactory.Instance.Init("./DB/item.csv", System.Text.Encoding.GetEncoding("gb2312"));
  119.  
  120.             if (!StartDatabase())
  121.             {
  122.                 Logger.ShowError("cannot connect to dbserver", null);
  123.                 Logger.ShowError("Shutting down in 20sec.", null);
  124.                 System.Threading.Thread.Sleep(20000);
  125.                 return;
  126.             }
  127.  
  128.             LoginClientManager.Instance.Start();
  129.             if (!LoginClientManager.Instance.StartNetwork(12000))
  130.             {
  131.                 Logger.ShowError("cannot listen on port: " + 12000);
  132.                 Logger.ShowInfo("Shutting down in 20sec.");
  133.                 System.Threading.Thread.Sleep(20000);
  134.                 return;
  135.             }
  136.  
  137.  
  138.             Global.clientMananger = (ClientManager)LoginClientManager.Instance;
  139.  
  140.             Console.WriteLine("Accepting clients.");
  141.  
  142.             while (true)
  143.             {
  144.                 // keep the connections to the database servers alive
  145.                 EnsureCharDB();
  146.                 EnsureAccountDB();
  147.                 // let new clients (max 10) connect
  148.                 LoginClientManager.Instance.NetworkLoop(10);
  149.                 System.Threading.Thread.Sleep(1);
  150.             }
  151.         }
  152.  
  153.         private static void ShutingDown(object sender, ConsoleCancelEventArgs args)
  154.         {
  155.             Logger.ShowInfo("Closing.....", null);            
  156.         }
  157.  
  158.         private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  159.         {
  160.             Exception ex = e.ExceptionObject as Exception;
  161.             Logger.ShowError("Fatal: An unhandled exception is thrown, terminating...");
  162.             Logger.ShowError("Error Message:" + ex.Message);
  163.             Logger.ShowError("Call Stack:" + ex.StackTrace);
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement