GeoEditorThread.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  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 (Exception e)
  87. {
  88. _log.log(Level.WARNING, "GeoEditor disconnected. " + e.getMessage(), e);
  89. }
  90. finally
  91. {
  92. try
  93. {
  94. _geSocket.close();
  95. }
  96. catch (Exception e)
  97. {
  98. }
  99. _working = false;
  100. }
  101. }
  102. public void sendGmPosition(int gx, int gy, short z)
  103. {
  104. if (!isConnected())
  105. return;
  106. try
  107. {
  108. synchronized (_out)
  109. {
  110. writeC(0x0b); // length 11 bytes!
  111. writeC(0x01); // Cmd = save cell;
  112. writeD(gx); // Global coord X;
  113. writeD(gy); // Global coord Y;
  114. writeH(z); // Coord Z;
  115. _out.flush();
  116. }
  117. }
  118. catch (Exception e)
  119. {
  120. _log.log(Level.WARNING, "GeoEditor disconnected. " + e.getMessage(), e);
  121. _working = false;
  122. }
  123. finally
  124. {
  125. try
  126. {
  127. _geSocket.close();
  128. }
  129. catch (Exception ex)
  130. {
  131. }
  132. _working = false;
  133. }
  134. }
  135. public void sendGmPosition(L2PcInstance _gm)
  136. {
  137. sendGmPosition(_gm.getX(), _gm.getY(), (short) _gm.getZ());
  138. }
  139. public void sendPing()
  140. {
  141. if (!isConnected())
  142. return;
  143. try
  144. {
  145. synchronized (_out)
  146. {
  147. writeC(0x01); // length 1 byte!
  148. writeC(0x02); // Cmd = ping (dummy packet for connection
  149. // test);
  150. _out.flush();
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. _log.log(Level.WARNING, "GeoEditor disconnected. " + e.getMessage(), e);
  156. _working = false;
  157. }
  158. finally
  159. {
  160. try
  161. {
  162. _geSocket.close();
  163. }
  164. catch (Exception ex)
  165. {
  166. }
  167. _working = false;
  168. }
  169. }
  170. private void writeD(int value) throws IOException
  171. {
  172. _out.write(value & 0xff);
  173. _out.write(value >> 8 & 0xff);
  174. _out.write(value >> 16 & 0xff);
  175. _out.write(value >> 24 & 0xff);
  176. }
  177. private void writeH(int value) throws IOException
  178. {
  179. _out.write(value & 0xff);
  180. _out.write(value >> 8 & 0xff);
  181. }
  182. private void writeC(int value) throws IOException
  183. {
  184. _out.write(value & 0xff);
  185. }
  186. public void setMode(int value)
  187. {
  188. _mode = value;
  189. }
  190. public void setTimer(int value)
  191. {
  192. if (value < 500)
  193. _sendDelay = 500; // maximum - 2 times per second!
  194. else if (value > 60000)
  195. _sendDelay = 60000; // Minimum - 1 time per minute.
  196. else
  197. _sendDelay = value;
  198. }
  199. public void addGM(L2PcInstance gm)
  200. {
  201. if (!_gms.contains(gm))
  202. _gms.add(gm);
  203. }
  204. public void removeGM(L2PcInstance gm)
  205. {
  206. if (_gms.contains(gm))
  207. _gms.remove(gm);
  208. }
  209. public boolean isSend(L2PcInstance gm)
  210. {
  211. if (_mode == 1 && _gms.contains(gm))
  212. return true;
  213. return false;
  214. }
  215. private boolean isConnected()
  216. {
  217. return _geSocket.isConnected() && !_geSocket.isClosed();
  218. }
  219. public boolean isWorking()
  220. {
  221. sendPing();
  222. return _working;
  223. }
  224. public int getMode()
  225. {
  226. return _mode;
  227. }
  228. }