GeoEditorThread.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.geoeditorcon;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.net.Socket;
  19. import java.net.SocketException;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import javolution.util.FastList;
  23. /**
  24. * @author Luno, Dezmond
  25. */
  26. public class GeoEditorThread extends Thread
  27. {
  28. private static Logger _log = Logger.getLogger(GeoEditorThread.class.getName());
  29. private boolean _working = false;
  30. private int _mode = 0; // 0 - don't send coords, 1 - send each
  31. // validateposition from client, 2 - send in
  32. // intervals of _sendDelay ms.
  33. private int _sendDelay = 1000; // default - once in second
  34. private Socket _geSocket;
  35. private OutputStream _out;
  36. private FastList<L2PcInstance> _gms;
  37. public GeoEditorThread(Socket ge)
  38. {
  39. _geSocket = ge;
  40. _working = true;
  41. _gms = new FastList<L2PcInstance>();
  42. }
  43. @Override
  44. public void interrupt()
  45. {
  46. try
  47. {
  48. _geSocket.close();
  49. }
  50. catch (Exception e)
  51. {
  52. }
  53. super.interrupt();
  54. }
  55. @Override
  56. public void run()
  57. {
  58. try
  59. {
  60. _out = _geSocket.getOutputStream();
  61. int timer = 0;
  62. while (_working)
  63. {
  64. if (!isConnected())
  65. _working = false;
  66. if (_mode == 2 && timer > _sendDelay)
  67. {
  68. for (L2PcInstance gm : _gms)
  69. if (!gm.getClient().getConnection().isClosed())
  70. sendGmPosition(gm);
  71. else
  72. _gms.remove(gm);
  73. timer = 0;
  74. }
  75. try
  76. {
  77. sleep(100);
  78. if (_mode == 2)
  79. timer += 100;
  80. }
  81. catch (Exception e)
  82. {
  83. }
  84. }
  85. }
  86. catch (SocketException e)
  87. {
  88. _log.warning("GeoEditor disconnected. " + e.getMessage());
  89. }
  90. catch (Exception e)
  91. {
  92. e.printStackTrace();
  93. }
  94. finally
  95. {
  96. try
  97. {
  98. _geSocket.close();
  99. }
  100. catch (Exception e)
  101. {
  102. }
  103. _working = false;
  104. }
  105. }
  106. public void sendGmPosition(int gx, int gy, short z)
  107. {
  108. if (!isConnected())
  109. return;
  110. try
  111. {
  112. synchronized (_out)
  113. {
  114. writeC(0x0b); // length 11 bytes!
  115. writeC(0x01); // Cmd = save cell;
  116. writeD(gx); // Global coord X;
  117. writeD(gy); // Global coord Y;
  118. writeH(z); // Coord Z;
  119. _out.flush();
  120. }
  121. }
  122. catch (SocketException e)
  123. {
  124. _log.warning("GeoEditor disconnected. " + e.getMessage());
  125. _working = false;
  126. }
  127. catch (Exception e)
  128. {
  129. e.printStackTrace();
  130. try
  131. {
  132. _geSocket.close();
  133. }
  134. catch (Exception ex)
  135. {
  136. }
  137. _working = false;
  138. }
  139. }
  140. public void sendGmPosition(L2PcInstance _gm)
  141. {
  142. sendGmPosition(_gm.getX(), _gm.getY(), (short) _gm.getZ());
  143. }
  144. public void sendPing()
  145. {
  146. if (!isConnected())
  147. return;
  148. try
  149. {
  150. synchronized (_out)
  151. {
  152. writeC(0x01); // length 1 byte!
  153. writeC(0x02); // Cmd = ping (dummy packet for connection
  154. // test);
  155. _out.flush();
  156. }
  157. }
  158. catch (SocketException e)
  159. {
  160. _log.warning("GeoEditor disconnected. " + e.getMessage());
  161. _working = false;
  162. }
  163. catch (Exception e)
  164. {
  165. e.printStackTrace();
  166. try
  167. {
  168. _geSocket.close();
  169. }
  170. catch (Exception ex)
  171. {
  172. }
  173. _working = false;
  174. }
  175. }
  176. private void writeD(int value) throws IOException
  177. {
  178. _out.write(value & 0xff);
  179. _out.write(value >> 8 & 0xff);
  180. _out.write(value >> 16 & 0xff);
  181. _out.write(value >> 24 & 0xff);
  182. }
  183. private void writeH(int value) throws IOException
  184. {
  185. _out.write(value & 0xff);
  186. _out.write(value >> 8 & 0xff);
  187. }
  188. private void writeC(int value) throws IOException
  189. {
  190. _out.write(value & 0xff);
  191. }
  192. public void setMode(int value)
  193. {
  194. _mode = value;
  195. }
  196. public void setTimer(int value)
  197. {
  198. if (value < 500)
  199. _sendDelay = 500; // maximum - 2 times per second!
  200. else if (value > 60000)
  201. _sendDelay = 60000; // Minimum - 1 time per minute.
  202. else
  203. _sendDelay = value;
  204. }
  205. public void addGM(L2PcInstance gm)
  206. {
  207. if (!_gms.contains(gm))
  208. _gms.add(gm);
  209. }
  210. public void removeGM(L2PcInstance gm)
  211. {
  212. if (_gms.contains(gm))
  213. _gms.remove(gm);
  214. }
  215. public boolean isSend(L2PcInstance gm)
  216. {
  217. if (_mode == 1 && _gms.contains(gm))
  218. return true;
  219. return false;
  220. }
  221. private boolean isConnected()
  222. {
  223. return _geSocket.isConnected() && !_geSocket.isClosed();
  224. }
  225. public boolean isWorking()
  226. {
  227. sendPing();
  228. return _working;
  229. }
  230. public int getMode()
  231. {
  232. return _mode;
  233. }
  234. }