NetConnection.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.netcon;
  16. import java.io.BufferedInputStream;
  17. import java.io.BufferedOutputStream;
  18. import java.io.IOException;
  19. import java.net.Socket;
  20. import java.net.UnknownHostException;
  21. import com.l2jserver.communityserver.network.netcon.crypt.NewCrypt;
  22. /**
  23. * @author Forsaiken
  24. */
  25. public abstract class NetConnection extends Thread
  26. {
  27. private static final NewCrypt INITIAL_CRYPT = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
  28. private final NetConnectionConfig _config;
  29. /* TCP */
  30. private Socket _tcpCon;
  31. private BufferedInputStream _tcpIn;
  32. private BufferedOutputStream _tcpOut;
  33. /* CRYPT */
  34. private NewCrypt _crypt;
  35. protected NetConnection(NetConnectionConfig config)
  36. {
  37. _config = config;
  38. }
  39. public final void connect(final String address, final int port) throws UnknownHostException, IOException
  40. {
  41. connect(new Socket(address, port));
  42. }
  43. public final void connect(final Socket remoteConnection) throws IOException
  44. {
  45. if (isConnected())
  46. throw new IOException("TCP Connect: Allready connected.");
  47. _crypt = INITIAL_CRYPT;
  48. _tcpCon = remoteConnection;
  49. _tcpOut = new BufferedOutputStream(_tcpCon.getOutputStream(), _config.TCP_SEND_BUFFER_SIZE);
  50. _tcpIn = new BufferedInputStream(_tcpCon.getInputStream(), _config.TCP_RECEIVE_BUFFER_SIZE);
  51. }
  52. public final boolean isConnected() throws IOException
  53. {
  54. return _tcpCon != null && _tcpCon.isConnected();
  55. }
  56. public final int getConnectionPort() throws IOException
  57. {
  58. if (!isConnected())
  59. throw new IOException("TCP: Not connected.");
  60. return _tcpCon.getPort();
  61. }
  62. public final String getConnectionAddress() throws IOException
  63. {
  64. if (!isConnected())
  65. throw new IOException("TCP: Not connected.");
  66. return _tcpCon.getInetAddress().getHostAddress();
  67. }
  68. protected final byte[] read() throws IOException
  69. {
  70. if (_tcpCon == null)
  71. throw new IOException("TCP Read: Not initialized.");
  72. if (_tcpCon.isClosed())
  73. throw new IOException("TCP Read: Connection closed.");
  74. final int lengthLo = _tcpIn.read();
  75. final int lengthHi = _tcpIn.read();
  76. final int length = lengthHi * 256 + lengthLo;
  77. if (lengthHi < 0)
  78. throw new IOException("TCP Read: Failed reading.");
  79. final byte[] data = new byte[length - 2];
  80. int receivedBytes = 0;
  81. int newBytes = 0;
  82. while (newBytes != -1 && receivedBytes < length - 2)
  83. {
  84. newBytes = _tcpIn.read(data, 0, length - 2);
  85. receivedBytes += newBytes;
  86. }
  87. if (receivedBytes != length - 2)
  88. throw new IOException("TCP Read: Incomplete Packet recived.");
  89. return decrypt(data);
  90. }
  91. protected final void write(final BaseWritePacket packet) throws IOException
  92. {
  93. if (_tcpCon == null)
  94. throw new IOException("TCP Write: Not initialized.");
  95. if (_tcpCon.isClosed())
  96. throw new IOException("TCP Write: Connection closed.");
  97. final byte[] data = crypt(packet.getContent());
  98. final int len = data.length + 2;
  99. synchronized (_tcpOut)
  100. {
  101. _tcpOut.write(len & 0xFF);
  102. _tcpOut.write(len >> 8 & 0xFF);
  103. _tcpOut.write(data);
  104. _tcpOut.flush();
  105. }
  106. }
  107. protected final void close(final BaseWritePacket packet) throws IOException
  108. {
  109. try
  110. {
  111. if (packet != null)
  112. write(packet);
  113. }
  114. finally
  115. {
  116. if (_tcpIn != null)
  117. {
  118. _tcpIn.close();
  119. _tcpIn = null;
  120. }
  121. if (_tcpOut != null)
  122. {
  123. _tcpOut.close();
  124. _tcpOut = null;
  125. }
  126. if (_tcpCon != null)
  127. {
  128. _tcpCon.close();
  129. _tcpCon = null;
  130. }
  131. }
  132. }
  133. public final void setCrypt(final NewCrypt crypt)
  134. {
  135. _crypt = crypt;
  136. }
  137. private final byte[] decrypt(byte[] data) throws IOException
  138. {
  139. data = _crypt.decrypt(data);
  140. if (!NewCrypt.verifyChecksum(data))
  141. throw new IOException("CRYPT: Incorrect packet checksum.");
  142. return data;
  143. }
  144. private final byte[] crypt(final byte[] data) throws IOException
  145. {
  146. NewCrypt.appendChecksum(data);
  147. return _crypt.crypt(data);
  148. }
  149. public abstract void run();
  150. }