Fabbian

auth

Jan 12th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. package com.dream.auth;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.InetAddress;
  6. import java.net.UnknownHostException;
  7.  
  8. import org.apache.log4j.Logger;
  9. import org.apache.log4j.xml.DOMConfigurator;
  10.  
  11. import com.dream.AuthConfig;
  12. import com.dream.L2AuthDatabaseFactory;
  13. import com.dream.auth.manager.AuthManager;
  14. import com.dream.auth.manager.BanManager;
  15. import com.dream.auth.manager.GameServerManager;
  16. import com.dream.auth.mmocore.SelectorConfig;
  17. import com.dream.auth.mmocore.SelectorThread;
  18. import com.dream.auth.thread.GameServerListener;
  19. import com.dream.tools.network.Util;
  20.  
  21. public class L2AuthServer
  22. {
  23. public static final int PROTOCOL_REV = 0x102;
  24. public static Logger _log = Logger.getLogger(L2AuthServer.class);
  25. private static double _intialTime = 0;
  26. private GameServerListener _gameServerListener;
  27. private SelectorThread<L2AuthClient> _selectorThread;
  28. private static L2AuthServer _instance;
  29.  
  30. public static L2AuthServer getInstance()
  31. {
  32. return _instance;
  33. }
  34.  
  35. public static void main(String[] args) throws Throwable
  36. {
  37. _instance = new L2AuthServer();
  38. }
  39.  
  40. public L2AuthServer() throws Throwable
  41. {
  42.  
  43. new File("log").mkdirs();
  44. DOMConfigurator.configure("./config/log4j.xml");
  45.  
  46. _intialTime = System.currentTimeMillis();
  47.  
  48. AuthConfig.load();
  49. L2AuthDatabaseFactory.getInstance();
  50.  
  51. GameServerManager.getInstance();
  52. ClientManager.getInstance();
  53. AuthManager.load();
  54.  
  55. BanManager.getInstance();
  56. Runtime.getRuntime().addShutdownHook(new Thread()
  57. {
  58. @Override
  59. public void run()
  60. {
  61. _log.info("Auth server shutting down");
  62. }
  63. });
  64. initNetworkLayer();
  65. initGSListener();
  66. startServer();
  67.  
  68. Util.printSection("Server Info");
  69. printInfo();
  70. }
  71.  
  72. private static void printInfo()
  73. {
  74. long freeMem = ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()) + Runtime.getRuntime().freeMemory()) / 1048576;
  75. long totalMem = Runtime.getRuntime().maxMemory() / 1048576;
  76. double finalTime = System.currentTimeMillis();
  77. if (AuthConfig.CRYPT_TOKEN)
  78. {
  79. _log.info("Token-based brut-proof enabled");
  80. }
  81. _log.info("Free memory: " + freeMem + " Mb of " + totalMem + " Mb");
  82. _log.info("Ready on IP: " + AuthConfig.LOGIN_SERVER_HOSTNAME + ":" + AuthConfig.LOGIN_SERVER_PORT + ".");
  83. _log.info("Load time: " + (int) ((finalTime - _intialTime) / 1000) + " Seconds.");
  84. Util.printSection("Server Info");
  85. _log.info("Auth Server successfully started.");
  86. }
  87.  
  88. private void startServer()
  89. {
  90. InetAddress bindAddress = null;
  91. if (!AuthConfig.LOGIN_SERVER_HOSTNAME.equals("*"))
  92. {
  93. try
  94. {
  95. bindAddress = InetAddress.getByName(AuthConfig.LOGIN_SERVER_HOSTNAME);
  96. }
  97. catch (UnknownHostException e1)
  98. {
  99. _log.warn("WARNING: The LoginServer bind address is invalid, using all available IPs. Reason: " + e1.getMessage());
  100. }
  101. }
  102. try
  103. {
  104. _selectorThread.openServerSocket(bindAddress, AuthConfig.LOGIN_SERVER_PORT);
  105. }
  106. catch (IOException e)
  107. {
  108. _log.fatal("FATAL: Failed to open server socket. Reason: " + e.getMessage(), e);
  109. System.exit(1);
  110. }
  111. _selectorThread.start();
  112. }
  113.  
  114. private void initGSListener()
  115. {
  116. _gameServerListener = new GameServerListener();
  117. _gameServerListener.start();
  118. _log.info("Listening for GameServers on " + AuthConfig.LOGIN_HOSTNAME + ":" + AuthConfig.LOGIN_PORT);
  119. }
  120.  
  121. private void initNetworkLayer()
  122. {
  123. L2AuthPacketHandler loginPacketHandler = new L2AuthPacketHandler();
  124. SelectorHelper sh = new SelectorHelper();
  125. SelectorConfig<L2AuthClient> ssc = new SelectorConfig<>(null, null, sh, loginPacketHandler);
  126. try
  127. {
  128. _selectorThread = new SelectorThread<>(ssc, sh, sh, sh);
  129. }
  130. catch (IOException e)
  131. {
  132. _log.fatal("FATAL: Failed to open Selector. Reason: " + e.getMessage(), e);
  133. System.exit(1);
  134. }
  135. }
  136.  
  137. public GameServerListener getGameServerListener()
  138. {
  139. return _gameServerListener;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment