CommunityServerThread.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.getMessage(), e);
  54. }
  55. }
  56. else
  57. {
  58. _log.log(Level.INFO, "CommunityServerThread: Deactivated by config.");
  59. _instance = new CommunityServerThread(null);
  60. }
  61. }
  62. }
  63. public static final CommunityServerThread getInstance()
  64. {
  65. return _instance;
  66. }
  67. private boolean _authed;
  68. private CommunityServerThread(final NetConnectionConfig config)
  69. {
  70. super(config);
  71. }
  72. public final boolean isAuthed()
  73. {
  74. return _authed;
  75. }
  76. public final void setAuthed(final boolean authed)
  77. {
  78. _authed = authed;
  79. }
  80. public final void forceClose(final BaseWritePacket packet)
  81. {
  82. _authed = false;
  83. try
  84. {
  85. super.close(packet);
  86. }
  87. catch (IOException e)
  88. {
  89. _log.log(Level.INFO, "CommunityServerThread: Failed disconnecting server, server already disconnected: " + e.getMessage(), e);
  90. }
  91. }
  92. public boolean sendPacket(final BaseWritePacket packet)
  93. {
  94. return sendPacket(packet, true);
  95. }
  96. public boolean sendPacket(final BaseWritePacket packet, final boolean needAuth)
  97. {
  98. if (needAuth && !_authed)
  99. return false;
  100. try
  101. {
  102. super.write(packet);
  103. }
  104. catch (IOException e)
  105. {
  106. _log.log(Level.INFO, "CommunityServerThread: Failed sending TCP packet: " + e.getMessage(), e);
  107. return false;
  108. }
  109. return true;
  110. }
  111. @Override
  112. public void run()
  113. {
  114. _log.log(Level.INFO, "CommunityServerThread: Activated by config.");
  115. int packetType1 = 0xFF;
  116. int packetType2 = 0xFF;
  117. BaseReadPacket packet = null;
  118. byte[] data = null;
  119. while (!isInterrupted())
  120. {
  121. try
  122. {
  123. super.sleep(10000L);
  124. }
  125. catch (InterruptedException e)
  126. {
  127. return;
  128. }
  129. _log.log(Level.INFO, "CommunityServerThread: Trying to connect to " + Config.COMMUNITY_SERVER_ADDRESS + " on port " + Config.COMMUNITY_SERVER_PORT + ".");
  130. try
  131. {
  132. _instance.connect(Config.COMMUNITY_SERVER_ADDRESS, Config.COMMUNITY_SERVER_PORT);
  133. }
  134. catch (SocketException se)
  135. {
  136. _log.log(Level.INFO, "CommunityServerThread: Connecting to " + Config.COMMUNITY_SERVER_ADDRESS + " on port " + Config.COMMUNITY_SERVER_PORT + " failed.");
  137. continue;
  138. }
  139. catch (IOException e)
  140. {
  141. _log.log(Level.INFO, "CommunityServerThread: Connection failed: " + e.getMessage(), e);
  142. continue;
  143. }
  144. try
  145. {
  146. long gameServerConnectStart = System.currentTimeMillis();
  147. while (!isInterrupted())
  148. {
  149. data = super.read();
  150. packetType1 = data[0] & 0xFF;
  151. packetType2 = data[1] & 0xFF;
  152. if (Config.PACKET_HANDLER_DEBUG)
  153. _log.log(Level.INFO, "Received packet: 0x" + Integer.toHexString(packetType1) + "-0x" + Integer.toHexString(packetType2));
  154. switch (packetType1)
  155. {
  156. case 0x00:
  157. {
  158. switch (packetType2)
  159. {
  160. case 0x00:
  161. packet = new InitCS(data, this);
  162. break;
  163. case 0x01:
  164. _log.info("Server connected in " + ((System.currentTimeMillis() - gameServerConnectStart) / 1000) + " seconds");
  165. packet = new AuthResponse(data, this);
  166. break;
  167. case 0x02:
  168. packet = new ConnectionError(data);
  169. break;
  170. }
  171. break;
  172. }
  173. case 0x01:
  174. {
  175. switch (packetType2)
  176. {
  177. case 0x00:
  178. packet = new RequestWorldInfo(data, this, RequestWorldInfo.SERVER_LOAD);
  179. break;
  180. case 0x01:
  181. packet = new RequestWorldInfo(data, this, RequestWorldInfo.PLAYER_DATA_UPDATE);
  182. break;
  183. case 0x02:
  184. packet = new RequestWorldInfo(data, this, RequestWorldInfo.CLAN_DATA_UPDATE);
  185. break;
  186. case 0x03:
  187. packet = new ClanNoticeInfo(data, 0);
  188. break;
  189. case 0x04:
  190. packet = new ClanNoticeInfo(data, 1);
  191. break;
  192. case 0x05:
  193. packet = new ClanNoticeInfo(data, this, 2);
  194. break;
  195. }
  196. break;
  197. }
  198. case 0x02:
  199. {
  200. switch (packetType2)
  201. {
  202. case 0x00:
  203. packet = new RequestPlayerShowBoard(data);
  204. break;
  205. case 0x01:
  206. packet = new RequestPlayerShowMessage(data);
  207. break;
  208. }
  209. break;
  210. }
  211. }
  212. if (packet != null)
  213. //new Thread(packet).start();
  214. ThreadPoolManager.getInstance().executeCommunityPacket(packet);
  215. else
  216. throw new IOException("Invalid packet!");
  217. }
  218. if (isInterrupted())
  219. forceClose(null);
  220. }
  221. catch (IOException e)
  222. {
  223. _log.log(Level.WARNING, "CommunityServerThread: TCP Connection lost: " + e.getMessage(), e);
  224. forceClose(null);
  225. }
  226. }
  227. }
  228. }