NetConnection.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. int left = length - 2;
  83. while ((newBytes != -1) && (receivedBytes < length - 2))
  84. {
  85. if (receivedBytes == 0)
  86. newBytes = this._tcpIn.read(data, 0, left);
  87. else
  88. newBytes = this._tcpIn.read(data, receivedBytes, left);
  89. receivedBytes += newBytes;
  90. left -= newBytes;
  91. }
  92. if (receivedBytes != length - 2)
  93. throw new IOException("TCP Read: Incomplete Packet recived.");
  94. return decrypt(data);
  95. }
  96. protected final void write(final BaseWritePacket packet) throws IOException
  97. {
  98. if (_tcpCon == null)
  99. throw new IOException("TCP Write: Not initialized.");
  100. if (_tcpCon.isClosed())
  101. throw new IOException("TCP Write: Connection closed.");
  102. final byte[] data = crypt(packet.getContent());
  103. final int len = data.length + 2;
  104. synchronized (_tcpOut)
  105. {
  106. _tcpOut.write(len & 0xFF);
  107. _tcpOut.write(len >> 8 & 0xFF);
  108. _tcpOut.write(data);
  109. _tcpOut.flush();
  110. }
  111. }
  112. protected final void close(final BaseWritePacket packet) throws IOException
  113. {
  114. try
  115. {
  116. if (packet != null)
  117. write(packet);
  118. }
  119. finally
  120. {
  121. if (_tcpIn != null)
  122. {
  123. _tcpIn.close();
  124. _tcpIn = null;
  125. }
  126. if (_tcpOut != null)
  127. {
  128. _tcpOut.close();
  129. _tcpOut = null;
  130. }
  131. if (_tcpCon != null)
  132. {
  133. _tcpCon.close();
  134. _tcpCon = null;
  135. }
  136. }
  137. }
  138. public final void setCrypt(final NewCrypt crypt)
  139. {
  140. _crypt = crypt;
  141. }
  142. private final byte[] decrypt(byte[] data) throws IOException
  143. {
  144. data = _crypt.decrypt(data);
  145. if (!NewCrypt.verifyChecksum(data))
  146. throw new IOException("CRYPT: Incorrect packet checksum.");
  147. return data;
  148. }
  149. private final byte[] crypt(final byte[] data) throws IOException
  150. {
  151. NewCrypt.appendChecksum(data);
  152. return _crypt.crypt(data);
  153. }
  154. public abstract void run();
  155. }