/source/org/limewire/rudp/UDPConnection.java

https://github.com/zootella/Lost-in-the-Space · Java · 235 lines · 169 code · 46 blank · 20 comment · 0 complexity · a686446297a7fb98dc855df0574a821f MD5 · raw file

  1. package org.limewire.rudp;
  2. import java.io.IOException;
  3. import java.net.InetAddress;
  4. import java.net.InetSocketAddress;
  5. import java.net.SocketAddress;
  6. import java.net.SocketException;
  7. import java.nio.channels.SocketChannel;
  8. import org.limewire.nio.AbstractNBSocket;
  9. import org.limewire.nio.channel.InterestReadableByteChannel;
  10. import org.limewire.nio.channel.InterestWritableByteChannel;
  11. /**
  12. * A reliable UDP connection.
  13. */
  14. class UDPConnection extends AbstractNBSocket implements RUDPSocket {
  15. /** Channel backing the socket. */
  16. private final UDPSocketChannel channel;
  17. /** The default read timeout. */
  18. private int soTimeout = 1 * 60 * 1000; // default to 1 minute.
  19. /** The context surrounding this connection . */
  20. private final RUDPContext context;
  21. /**
  22. * Creates an unconnected <code>UDPConnection</code>. You must call {@link #connect(SocketAddress) connect(...)} to connect.
  23. */
  24. UDPConnection(RUDPContext context, UDPSocketChannel channel) {
  25. this.context = context;
  26. this.channel = channel;
  27. setInitialReader();
  28. setInitialWriter();
  29. }
  30. /** Returns the <code>UDPSocketChannel</code>, since it already implements
  31. * <code>InterestReadChannel</code>. */
  32. @Override
  33. protected InterestReadableByteChannel getBaseReadChannel() {
  34. return channel;
  35. }
  36. /** Returns the <code>UDPSocketChannel</code>, since it already implements
  37. * <code>InterestWriteChannel</code>. */
  38. @Override
  39. protected InterestWritableByteChannel getBaseWriteChannel() {
  40. return channel;
  41. }
  42. /** Doesn't do anything. */
  43. @Override
  44. protected void shutdownImpl() {
  45. }
  46. /** Sets the read timeout this socket should use. */
  47. @Override
  48. public void setSoTimeout(int timeout) {
  49. soTimeout = timeout;
  50. }
  51. /** Returns the timeout this socket uses when reading. */
  52. @Override
  53. public int getSoTimeout() {
  54. return soTimeout;
  55. }
  56. /** Returns the local address this socket uses. */
  57. @Override
  58. public InetAddress getLocalAddress() {
  59. return context.getUDPService().getStableListeningAddress();
  60. }
  61. @Override
  62. public SocketAddress getRemoteSocketAddress() {
  63. return channel.getRemoteSocketAddress();
  64. }
  65. @Override
  66. public InetAddress getInetAddress() {
  67. return ((InetSocketAddress)getRemoteSocketAddress()).getAddress();
  68. }
  69. @Override
  70. public int getPort() {
  71. return ((InetSocketAddress)getRemoteSocketAddress()).getPort();
  72. }
  73. @Override
  74. public int getLocalPort() {
  75. return context.getUDPService().getStableListeningPort();
  76. }
  77. @Override
  78. public SocketAddress getLocalSocketAddress() {
  79. return new InetSocketAddress(getLocalAddress(), getLocalPort());
  80. }
  81. @Override
  82. public SocketChannel getChannel() {
  83. return channel;
  84. }
  85. @Override
  86. public String toString() {
  87. return "UDPConnection:" + channel;
  88. }
  89. @Override
  90. public boolean isConnected() {
  91. return channel.isConnected();
  92. }
  93. @Override
  94. public boolean isBound() {
  95. return true;
  96. }
  97. @Override
  98. public boolean isClosed() {
  99. return !channel.isOpen();
  100. }
  101. @Override
  102. public boolean isInputShutdown() {
  103. return !channel.isOpen();
  104. }
  105. @Override
  106. public boolean isOutputShutdown() {
  107. return !channel.isOpen();
  108. }
  109. @Override
  110. public void bind(SocketAddress bindpoint) throws IOException {
  111. throw new UnsupportedOperationException("not implemented");
  112. }
  113. @Override
  114. public void setTcpNoDelay(boolean on) throws SocketException {
  115. // does nothing
  116. }
  117. @Override
  118. public boolean getTcpNoDelay() throws SocketException {
  119. return true;
  120. }
  121. @Override
  122. public void setSoLinger(boolean on, int linger) throws SocketException {
  123. // does nothing
  124. }
  125. @Override
  126. public int getSoLinger() throws SocketException {
  127. return -1;
  128. }
  129. @Override
  130. public void sendUrgentData(int data) throws IOException {
  131. throw new UnsupportedOperationException("not implemented");
  132. }
  133. @Override
  134. public void setOOBInline(boolean on) throws SocketException {
  135. throw new UnsupportedOperationException("not implemented");
  136. }
  137. @Override
  138. public boolean getOOBInline() throws SocketException {
  139. throw new UnsupportedOperationException("not implemented");
  140. }
  141. @Override
  142. public synchronized void setSendBufferSize(int size) throws SocketException {
  143. throw new UnsupportedOperationException("not implemented");
  144. }
  145. @Override
  146. public synchronized int getSendBufferSize() throws SocketException {
  147. return UDPConnectionProcessor.DATA_CHUNK_SIZE * UDPConnectionProcessor.DATA_WINDOW_SIZE;
  148. }
  149. @Override
  150. public synchronized void setReceiveBufferSize(int size) throws SocketException {
  151. throw new UnsupportedOperationException("not implemented");
  152. }
  153. @Override
  154. public synchronized int getReceiveBufferSize() throws SocketException {
  155. return UDPConnectionProcessor.MAX_DATA_SIZE * UDPConnectionProcessor.DATA_WINDOW_SIZE;
  156. }
  157. @Override
  158. public void setKeepAlive(boolean on) throws SocketException {
  159. // ignore
  160. }
  161. @Override
  162. public boolean getKeepAlive() throws SocketException {
  163. return true;
  164. }
  165. @Override
  166. public void setTrafficClass(int tc) throws SocketException {
  167. throw new UnsupportedOperationException("not implemented");
  168. }
  169. @Override
  170. public int getTrafficClass() throws SocketException {
  171. throw new UnsupportedOperationException("not implemented");
  172. }
  173. @Override
  174. public void setReuseAddress(boolean on) throws SocketException {
  175. throw new UnsupportedOperationException("not implemented");
  176. }
  177. @Override
  178. public boolean getReuseAddress() throws SocketException {
  179. throw new UnsupportedOperationException("not implemented");
  180. }
  181. @Override
  182. public void shutdownInput() throws IOException {
  183. throw new UnsupportedOperationException("not implemented");
  184. }
  185. @Override
  186. public void shutdownOutput() throws IOException {
  187. throw new UnsupportedOperationException("not implemented");
  188. }
  189. }