Advertisement
Guest User

Untitled

a guest
Jan 4th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. /* L2jFrozen Project - www.l2jfrozen.com
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  16. * 02111-1307, USA.
  17. *
  18. * http://www.gnu.org/copyleft/gpl.html
  19. */
  20. package com.l2jfrozen.loginserver;
  21.  
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.net.InetAddress;
  27. import java.net.UnknownHostException;
  28. import java.security.GeneralSecurityException;
  29. import java.sql.SQLException;
  30. import java.util.logging.LogManager;
  31.  
  32. import org.apache.log4j.Logger;
  33. import org.apache.log4j.PropertyConfigurator;
  34.  
  35. import com.l2jfrozen.Config;
  36. import com.l2jfrozen.FService;
  37. import com.l2jfrozen.L2Frozen;
  38. import com.l2jfrozen.ServerType;
  39. import com.l2jfrozen.gameserver.datatables.GameServerTable;
  40. import com.l2jfrozen.netcore.NetcoreConfig;
  41. import com.l2jfrozen.netcore.SelectorConfig;
  42. import com.l2jfrozen.netcore.SelectorThread;
  43. import com.l2jfrozen.status.Status;
  44. import com.l2jfrozen.util.Util;
  45. import com.l2jfrozen.util.database.L2DatabaseFactory;
  46. import com.l2jfrozen.util.database.SqlUtils;
  47.  
  48. public class L2LoginServer
  49. {
  50. public static final int PROTOCOL_REV = 0x0102;
  51.  
  52. private static L2LoginServer _instance;
  53. private final Logger LOGGER = Logger.getLogger(L2LoginServer.class);
  54. private GameServerListener _gameServerListener;
  55. private SelectorThread<L2LoginClient> _selectorThread;
  56. private Status _statusServer;
  57.  
  58. public static void main(final String[] args)
  59. {
  60. PropertyConfigurator.configure(FService.LOG_CONF_FILE);
  61. _instance = new L2LoginServer();
  62. }
  63.  
  64. public static L2LoginServer getInstance()
  65. {
  66. return _instance;
  67. }
  68.  
  69. public L2LoginServer()
  70. {
  71. ServerType.serverMode = ServerType.MODE_LOGINSERVER;
  72. // Local Constants
  73. final String LOG_FOLDER_BASE = "log"; // Name of folder for LOGGER base file
  74. final File logFolderBase = new File(LOG_FOLDER_BASE);
  75. logFolderBase.mkdir();
  76.  
  77. final String LOG_FOLDER = "log/login"; // Name of folder for LOGGER file
  78.  
  79. /*** Main ***/
  80. // Create LOGGER folder
  81. File logFolder = new File(LOG_FOLDER);
  82. logFolder.mkdir();
  83.  
  84. // Create input stream for LOGGER file -- or store file data into memory
  85. InputStream is = null;
  86. try
  87. {
  88. // check for legacy Implementation
  89. File log_conf_file = new File(FService.LOG_CONF_FILE);
  90. if (!log_conf_file.exists())
  91. {
  92. // old file position
  93. log_conf_file = new File(FService.LEGACY_LOG_CONF_FILE);
  94. }
  95.  
  96. is = new FileInputStream(log_conf_file);
  97. LogManager.getLogManager().readConfiguration(is);
  98.  
  99. }
  100. catch (final IOException e)
  101. {
  102. e.printStackTrace();
  103. }
  104. finally
  105. {
  106. if (is != null)
  107. {
  108. try
  109. {
  110.  
  111. is.close();
  112. }
  113. catch (final IOException e)
  114. {
  115. e.printStackTrace();
  116. }
  117. }
  118.  
  119. }
  120.  
  121. // Team info
  122. Util.printSection("Team");
  123. L2Frozen.info();
  124.  
  125. // Load LoginServer Configs
  126. Config.load();
  127.  
  128. Util.printSection("Database");
  129. // Prepare Database
  130. try
  131. {
  132. L2DatabaseFactory.getInstance();
  133. }
  134. catch (final SQLException e)
  135. {
  136. LOGGER.fatal("Failed initializing database", e);
  137. System.exit(1);
  138. }
  139.  
  140. try
  141. {
  142. LoginController.load();
  143. }
  144. catch (final GeneralSecurityException e)
  145. {
  146. LOGGER.fatal("Failed initializing LoginController", e);
  147. System.exit(1);
  148. }
  149.  
  150. try
  151. {
  152. GameServerTable.load();
  153. }
  154. catch (final GeneralSecurityException e)
  155. {
  156. LOGGER.fatal("Failed to load GameServerTable", e);
  157. System.exit(1);
  158. }
  159. catch (final Exception e)
  160. {
  161. LOGGER.fatal("Failed to load GameServerTable", e);
  162.  
  163. if (Config.ENABLE_ALL_EXCEPTIONS)
  164. e.printStackTrace();
  165.  
  166. System.exit(1);
  167. }
  168.  
  169. InetAddress bindAddress = null;
  170. if (!Config.LOGIN_BIND_ADDRESS.equals("*"))
  171. {
  172. try
  173. {
  174. bindAddress = InetAddress.getByName(Config.LOGIN_BIND_ADDRESS);
  175. }
  176. catch (final UnknownHostException e1)
  177. {
  178. LOGGER.warn("WARNING: The LoginServer bind address is invalid, using all avaliable IPs", e1);
  179. }
  180. }
  181. // Load telnet status
  182. if (Config.IS_TELNET_ENABLED)
  183. {
  184. try
  185. {
  186. _statusServer = new Status(ServerType.serverMode);
  187. _statusServer.start();
  188. }
  189. catch (final IOException e)
  190. {
  191. LOGGER.warn("Failed to start the Telnet Server. Reason: " + e.getMessage(), e);
  192. }
  193. }
  194.  
  195. final SelectorConfig sc = new SelectorConfig();
  196. sc.setMaxReadPerPass(NetcoreConfig.getInstance().MMO_MAX_READ_PER_PASS);
  197. sc.setMaxSendPerPass(NetcoreConfig.getInstance().MMO_MAX_SEND_PER_PASS);
  198. sc.setSleepTime(NetcoreConfig.getInstance().MMO_SELECTOR_SLEEP_TIME);
  199. sc.setHelperBufferCount(NetcoreConfig.getInstance().MMO_HELPER_BUFFER_COUNT);
  200.  
  201. final L2LoginPacketHandler lph = new L2LoginPacketHandler();
  202. final SelectorHelper sh = new SelectorHelper();
  203. try
  204. {
  205. _selectorThread = new SelectorThread<>(sc, sh, lph, sh, sh);
  206. }
  207. catch (final IOException e)
  208. {
  209. LOGGER.fatal("Failed to open Selector", e);
  210. System.exit(1);
  211. }
  212.  
  213. try
  214. {
  215. _gameServerListener = new GameServerListener();
  216. _gameServerListener.start();
  217. LOGGER.info("Listening for GameServers on " + Config.GAME_SERVER_LOGIN_HOST + ":" + Config.GAME_SERVER_LOGIN_PORT);
  218. }
  219. catch (final IOException e)
  220. {
  221. LOGGER.fatal("Failed to start the Game Server Listener" + e);
  222. System.exit(1);
  223. }
  224.  
  225. try
  226. {
  227. _selectorThread.openServerSocket(bindAddress, Config.PORT_LOGIN);
  228. _selectorThread.start();
  229. LOGGER.info("Login Server ready on " + (bindAddress == null ? "*" : bindAddress.getHostAddress()) + ":" + Config.PORT_LOGIN);
  230.  
  231. }
  232. catch (final IOException e)
  233. {
  234. LOGGER.error("Failed to open server socket", e);
  235. System.exit(1);
  236. }
  237.  
  238. // load bannedIps
  239. Config.loadBanFile();
  240.  
  241. logFolder = null;
  242. bindAddress = null;
  243. }
  244.  
  245. public GameServerListener getGameServerListener()
  246. {
  247. return _gameServerListener;
  248. }
  249.  
  250. public void shutdown(final boolean restart)
  251. {
  252. LoginController.getInstance().shutdown();
  253. SqlUtils.OpzLogin();
  254. System.gc();
  255. Runtime.getRuntime().exit(restart ? 2 : 0);
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement