CommunityServerThread.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.network.communityserver;
  16. import java.io.IOException;
  17. import java.net.SocketException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. import com.l2jserver.gameserver.network.communityserver.readpackets.AuthResponse;
  23. import com.l2jserver.gameserver.network.communityserver.readpackets.ClanNoticeInfo;
  24. import com.l2jserver.gameserver.network.communityserver.readpackets.ConnectionError;
  25. import com.l2jserver.gameserver.network.communityserver.readpackets.InitCS;
  26. import com.l2jserver.gameserver.network.communityserver.readpackets.RequestPlayerShowBoard;
  27. import com.l2jserver.gameserver.network.communityserver.readpackets.RequestPlayerShowMessage;
  28. import com.l2jserver.gameserver.network.communityserver.readpackets.RequestWorldInfo;
  29. import org.netcon.BaseReadPacket;
  30. import org.netcon.BaseWritePacket;
  31. import org.netcon.NetConnection;
  32. import org.netcon.NetConnectionConfig;
  33. /**
  34. * @authors Forsaiken, Gigiikun
  35. */
  36. public final class CommunityServerThread extends NetConnection
  37. {
  38. private static final Logger _log = Logger.getLogger(CommunityServerThread.class.getName());
  39. private static CommunityServerThread _instance;
  40. public static final void initialize()
  41. {
  42. if (_instance == null)
  43. {
  44. if (Config.ENABLE_COMMUNITY_BOARD)
  45. {
  46. try
  47. {
  48. _instance = new CommunityServerThread(new NetConnectionConfig(Config.COMMUNITY_CONFIGURATION_FILE));
  49. _instance.start();
  50. }
  51. catch (Exception e)
  52. {
  53. _log.log(Level.WARNING, "CommunityServerThread: Failed loading config file!", e);
  54. e.printStackTrace();
  55. }
  56. }
  57. else
  58. {
  59. _log.log(Level.INFO, "CommunityServerThread: Deactivated by config.");
  60. _instance = new CommunityServerThread(null);
  61. }
  62. }
  63. }
  64. public static final CommunityServerThread getInstance()
  65. {
  66. return _instance;
  67. }
  68. private boolean _authed;
  69. private CommunityServerThread(final NetConnectionConfig config)
  70. {
  71. super(config);
  72. }
  73. public final boolean isAuthed()
  74. {
  75. return _authed;
  76. }
  77. public final void setAuthed(final boolean authed)
  78. {
  79. _authed = authed;
  80. }
  81. public final void forceClose(final BaseWritePacket packet)
  82. {
  83. _authed = false;
  84. try
  85. {
  86. super.close(packet);
  87. }
  88. catch (IOException e)
  89. {
  90. _log.log(Level.INFO, "CommunityServerThread: Failed disconnecting server, server already disconnected.");
  91. e.printStackTrace();
  92. }
  93. }
  94. public boolean sendPacket(final BaseWritePacket packet)
  95. {
  96. return sendPacket(packet, true);
  97. }
  98. public boolean sendPacket(final BaseWritePacket packet, final boolean needAuth)
  99. {
  100. if (needAuth && !_authed)
  101. return false;
  102. try
  103. {
  104. super.write(packet);
  105. }
  106. catch (IOException e)
  107. {
  108. _log.log(Level.INFO, "CommunityServerThread: Failed sending TCP packet.");
  109. e.printStackTrace();
  110. return false;
  111. }
  112. return true;
  113. }
  114. @Override
  115. public void run()
  116. {
  117. _log.log(Level.INFO, "CommunityServerThread: Activated by config.");
  118. int packetType1 = 0xFF;
  119. int packetType2 = 0xFF;
  120. BaseReadPacket packet = null;
  121. byte[] data = null;
  122. while (true)
  123. {
  124. try
  125. {
  126. super.sleep(10000L);
  127. }
  128. catch (InterruptedException e)
  129. {
  130. e.printStackTrace();
  131. }
  132. _log.log(Level.INFO, "CommunityServerThread: Trying to connect to " + Config.COMMUNITY_SERVER_ADDRESS + " on port " + Config.COMMUNITY_SERVER_PORT + ".");
  133. try
  134. {
  135. _instance.connect(Config.COMMUNITY_SERVER_ADDRESS, Config.COMMUNITY_SERVER_PORT);
  136. }
  137. catch (SocketException se)
  138. {
  139. _log.log(Level.INFO, "CommunityServerThread: Connecting to " + Config.COMMUNITY_SERVER_ADDRESS + " on port " + Config.COMMUNITY_SERVER_PORT + " failed.");
  140. continue;
  141. }
  142. catch (IOException e)
  143. {
  144. _log.log(Level.INFO, "CommunityServerThread: Connection failed!", e);
  145. e.printStackTrace();
  146. continue;
  147. }
  148. try
  149. {
  150. long gameServerConnectStart = System.currentTimeMillis();
  151. while (true)
  152. {
  153. data = super.read();
  154. packetType1 = data[0] & 0xFF;
  155. packetType2 = data[1] & 0xFF;
  156. if (Config.PACKET_HANDLER_DEBUG)
  157. _log.log(Level.INFO, "Received packet: 0x" + Integer.toHexString(packetType1) + "-0x" + Integer.toHexString(packetType2));
  158. switch (packetType1)
  159. {
  160. case 0x00:
  161. {
  162. switch (packetType2)
  163. {
  164. case 0x00:
  165. packet = new InitCS(data, this);
  166. break;
  167. case 0x01:
  168. _log.info("Server connected in " + ((System.currentTimeMillis() - gameServerConnectStart) / 1000) + " seconds");
  169. packet = new AuthResponse(data, this);
  170. break;
  171. case 0x02:
  172. packet = new ConnectionError(data);
  173. break;
  174. }
  175. break;
  176. }
  177. case 0x01:
  178. {
  179. switch (packetType2)
  180. {
  181. case 0x00:
  182. packet = new RequestWorldInfo(data, this, RequestWorldInfo.SERVER_LOAD);
  183. break;
  184. case 0x01:
  185. packet = new RequestWorldInfo(data, this, RequestWorldInfo.PLAYER_DATA_UPDATE);
  186. break;
  187. case 0x02:
  188. packet = new RequestWorldInfo(data, this, RequestWorldInfo.CLAN_DATA_UPDATE);
  189. break;
  190. case 0x03:
  191. packet = new ClanNoticeInfo(data, 0);
  192. break;
  193. case 0x04:
  194. packet = new ClanNoticeInfo(data, 1);
  195. break;
  196. case 0x05:
  197. packet = new ClanNoticeInfo(data, this, 2);
  198. break;
  199. }
  200. break;
  201. }
  202. case 0x02:
  203. {
  204. switch (packetType2)
  205. {
  206. case 0x00:
  207. packet = new RequestPlayerShowBoard(data);
  208. break;
  209. case 0x01:
  210. packet = new RequestPlayerShowMessage(data);
  211. break;
  212. }
  213. break;
  214. }
  215. }
  216. if (packet != null)
  217. //new Thread(packet).start();
  218. ThreadPoolManager.getInstance().executeCommunityPacket(packet);
  219. else
  220. throw new IOException("Invalid packet!");
  221. }
  222. }
  223. catch (IOException e)
  224. {
  225. //e.printStackTrace();
  226. _log.log(Level.WARNING, "CommunityServerThread: TCP Connection lost!");
  227. forceClose(null);
  228. }
  229. }
  230. }
  231. }