GameServerThread.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.communityserver.network;
  16. import java.io.IOException;
  17. import java.net.SocketException;
  18. import java.security.KeyPair;
  19. import java.security.interfaces.RSAPrivateKey;
  20. import java.security.interfaces.RSAPublicKey;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.communityserver.GameServerRegistrationTable;
  24. import com.l2jserver.communityserver.communityboard.CommunityBoardManager;
  25. import com.l2jserver.communityserver.network.netcon.BaseReadPacket;
  26. import com.l2jserver.communityserver.network.netcon.BaseWritePacket;
  27. import com.l2jserver.communityserver.network.netcon.NetConnection;
  28. import com.l2jserver.communityserver.network.netcon.NetConnectionConfig;
  29. import com.l2jserver.communityserver.network.readpackets.BlowFishKey;
  30. import com.l2jserver.communityserver.network.readpackets.GameServerAuth;
  31. import com.l2jserver.communityserver.network.readpackets.WorldInfo;
  32. import com.l2jserver.communityserver.network.readpackets.RequestShowCommunityBoard;
  33. import com.l2jserver.communityserver.network.readpackets.RequestWriteCommunityBoard;
  34. import com.l2jserver.communityserver.network.writepackets.AuthResponse;
  35. import com.l2jserver.communityserver.network.writepackets.CommunityServerFail;
  36. import com.l2jserver.communityserver.network.writepackets.InitCS;
  37. import com.l2jserver.communityserver.network.writepackets.RequestWorldInfo;
  38. import com.l2jserver.communityserver.threading.ThreadPoolManager;
  39. /**
  40. * @author Forsaiken
  41. */
  42. public class GameServerThread extends NetConnection
  43. {
  44. protected static final Logger _log = Logger.getLogger(GameServerThread.class.getName());
  45. private final RSAPublicKey _publicKey;
  46. private final RSAPrivateKey _privateKey;
  47. private CommunityBoardManager _communityBoardManager;
  48. private boolean _isAuthed;
  49. private loadingData _loadingTask;
  50. public GameServerThread(final NetConnectionConfig config) throws IOException
  51. {
  52. super(config);
  53. setName(getClass().getSimpleName()+"-"+getId());
  54. final KeyPair pair = GameServerRegistrationTable.getInstance().getRandomKeyPair();
  55. _privateKey = (RSAPrivateKey)pair.getPrivate();
  56. _publicKey = (RSAPublicKey)pair.getPublic();
  57. }
  58. @Override
  59. public final void run()
  60. {
  61. int packetType1 = 0xFF;
  62. int packetType2 = 0xFF;
  63. byte[] data = null;
  64. BaseReadPacket packet = null;
  65. try
  66. {
  67. sendPacket(new InitCS(_publicKey.getModulus().toByteArray()));
  68. while (true)
  69. {
  70. data = super.read();
  71. packetType1 = data[0] & 0xFF;
  72. packetType2 = data[1] & 0xFF;
  73. // _log.log(Level.INFO, "Received packet: 0x" + Integer.toHexString(packetType1) + "-0x" + Integer.toHexString(packetType2));
  74. switch (packetType1)
  75. {
  76. case 0x00:
  77. {
  78. switch (packetType2)
  79. {
  80. case 0x00:
  81. packet = new BlowFishKey(data, _privateKey, this);
  82. break;
  83. case 0x01:
  84. packet = new GameServerAuth(data, this);
  85. break;
  86. }
  87. break;
  88. }
  89. case 0x01:
  90. {
  91. if (!_isAuthed)
  92. {
  93. forceClose(new CommunityServerFail(CommunityServerFail.NOT_AUTHED));
  94. return;
  95. }
  96. if (_loadingTask != null)
  97. _loadingTask.addPacket();
  98. packet = new WorldInfo(data, this, packetType2);
  99. /*
  100. switch (packetType2)
  101. {
  102. case 0x00:
  103. packet = new WorldInfo(data, this, packetType2);
  104. break;
  105. case 0x01:
  106. packet = new WorldInfo(data, this, 1);
  107. break;
  108. case 0x02:
  109. packet = new WorldInfo(data, this, 2);
  110. break;
  111. case 0x03:
  112. packet = new PlayerUpdate(data, this);
  113. break;
  114. case 0x04:
  115. // clan update
  116. break;
  117. case 0x05:
  118. // clan created
  119. break;
  120. case 0x06:
  121. // clan deleted
  122. break;
  123. }*/
  124. break;
  125. }
  126. case 0x02:
  127. {
  128. if (!_isAuthed)
  129. {
  130. forceClose(new CommunityServerFail(CommunityServerFail.NOT_AUTHED));
  131. return;
  132. }
  133. switch (packetType2)
  134. {
  135. case 0x00:
  136. packet = new RequestShowCommunityBoard(data, this);
  137. break;
  138. case 0x01:
  139. packet = new RequestWriteCommunityBoard(data, this);
  140. break;
  141. }
  142. break;
  143. }
  144. }
  145. if (packet != null)
  146. ThreadPoolManager.execute(packet);
  147. else
  148. throw new IOException("Invalid packet!");
  149. }
  150. }
  151. catch (SocketException se)
  152. {
  153. forceClose(null);
  154. }
  155. catch (IOException e)
  156. {
  157. _log.log(Level.INFO, "Connection lost!");
  158. e.printStackTrace();
  159. forceClose(new CommunityServerFail(CommunityServerFail.NOT_AUTHED));
  160. }
  161. }
  162. public final CommunityBoardManager getCommunityBoardManager()
  163. {
  164. return _communityBoardManager;
  165. }
  166. private final void setCommunityBoardManager(final CommunityBoardManager communityBoardManager)
  167. {
  168. if (_communityBoardManager != null)
  169. _communityBoardManager.setGST(null);
  170. _communityBoardManager = communityBoardManager;
  171. if (_communityBoardManager != null)
  172. _communityBoardManager.setGST(this);
  173. }
  174. public final void sendPacket(final BaseWritePacket packet)
  175. {
  176. try
  177. {
  178. super.write(packet);
  179. }
  180. catch (IOException e)
  181. {
  182. e.printStackTrace();
  183. }
  184. }
  185. public final void forceClose(final BaseWritePacket packet)
  186. {
  187. try
  188. {
  189. super.close(packet);
  190. }
  191. catch (IOException e)
  192. {
  193. _log.finer("GameServerThread: Failed disconnecting server, server already disconnected.");
  194. }
  195. finally
  196. {
  197. setCommunityBoardManager(null);
  198. }
  199. }
  200. public final void onGameServerAuth(final byte[] hexId, final int sqlDPId)
  201. {
  202. if (!GameServerRegistrationTable.getInstance().isHexIdOk(hexId))
  203. {
  204. _log.log(Level.INFO, "Invalid HexId. Closing connection...");
  205. forceClose(null);
  206. return;
  207. }
  208. else if (GameServerRegistrationTable.getInstance().isHexIdInUse(hexId))
  209. {
  210. _log.log(Level.INFO, "HexId allready in use. Closing connection...");
  211. forceClose(null);
  212. return;
  213. }
  214. final CommunityBoardManager communityBoardManager = CommunityBoardManager.getInstance(sqlDPId);
  215. if (communityBoardManager.getGST() != null)
  216. {
  217. try
  218. {
  219. if (communityBoardManager.getGST().isConnected())
  220. {
  221. _log.log(Level.INFO, "SQLDPId allready in use. Closing connection...");
  222. forceClose(null);
  223. return;
  224. }
  225. }
  226. catch (IOException e)
  227. {
  228. _log.log(Level.INFO, "Exception. Closing connection...", e);
  229. e.printStackTrace();
  230. forceClose(null);
  231. return;
  232. }
  233. }
  234. GameServerRegistrationTable.getInstance().setHexIdInUse(hexId);
  235. setCommunityBoardManager(communityBoardManager);
  236. _isAuthed = true;
  237. _log.log(Level.INFO, "GameServer connected!");
  238. if (communityBoardManager.isLoaded())
  239. {
  240. sendPacket(new AuthResponse(AuthResponse.AUTHED));
  241. }
  242. else
  243. {
  244. communityBoardManager.clean();
  245. _log.log(Level.INFO, "Transfering needed data for CommunityServer!");
  246. _loadingTask = new loadingData(this);
  247. new Thread(_loadingTask).start();
  248. sendPacket(new RequestWorldInfo(RequestWorldInfo.SERVER_LOAD, 0, null, false));
  249. }
  250. }
  251. public void addNeededPacket(int val)
  252. {
  253. if (_loadingTask != null)
  254. {
  255. _loadingTask.sendedPacket(val);
  256. }
  257. }
  258. private final class loadingData implements Runnable
  259. {
  260. private GameServerThread _gst;
  261. private int _incomePacket;
  262. private int _neededPacket;
  263. private loadingData(GameServerThread gst)
  264. {
  265. _gst = gst;
  266. _neededPacket = -1;
  267. _incomePacket = 0;
  268. }
  269. public final void run()
  270. {
  271. try
  272. {
  273. int i = 0;
  274. while ((_neededPacket == -1 || _incomePacket < _neededPacket) && i++ < 15)
  275. {
  276. currentThread();
  277. Thread.sleep(1000);
  278. }
  279. }
  280. catch (InterruptedException e)
  281. {
  282. // TODO Auto-generated catch block
  283. e.printStackTrace();
  284. }
  285. if (_neededPacket == -1)
  286. _gst.forceClose(null);
  287. else if (_incomePacket != _neededPacket)
  288. _gst.forceClose(null);
  289. else
  290. {
  291. _log.info("CB successfully get " + _gst.getCommunityBoardManager().getPlayerList().size() + " player(s) data.");
  292. _log.info("CB successfully get " + _gst.getCommunityBoardManager().getClanList().size() + " clan(s) data.");
  293. _log.info("CB successfully get " + _gst.getCommunityBoardManager().getCastleList().size() + " castle(s) data.");
  294. _gst.getCommunityBoardManager().setLoaded();
  295. _gst.sendPacket(new AuthResponse(AuthResponse.AUTHED));
  296. }
  297. _loadingTask = null;
  298. }
  299. public void addPacket()
  300. {
  301. _incomePacket++;
  302. }
  303. public void sendedPacket(int val)
  304. {
  305. _neededPacket = val;
  306. }
  307. }
  308. }