PageRenderTime 120ms CodeModel.GetById 18ms RepoModel.GetById 7ms app.codeStats 0ms

/src/main/java/com/pmeade/websocket/net/WebSocket.java

https://gitlab.com/math4youbyusgroupillinois/websocket
Java | 290 lines | 204 code | 47 blank | 39 comment | 6 complexity | ff8475e3ff340ea03469c5f940610be3 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * WebSocket.java
  3. * Copyright 2014 Patrick Meade.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.pmeade.websocket.net;
  19. import com.pmeade.websocket.io.WebSocketServerInputStream;
  20. import com.pmeade.websocket.io.WebSocketServerOutputStream;
  21. import java.io.IOException;
  22. import java.net.InetAddress;
  23. import java.net.Socket;
  24. import java.net.SocketAddress;
  25. import java.net.SocketException;
  26. import java.nio.channels.SocketChannel;
  27. /**
  28. * WebSocket decorates Socket to provide the server side of a Socket that
  29. * speaks the WebSocket (RFC 6455) protocol.
  30. * @author veloxi
  31. */
  32. public class WebSocket extends Socket {
  33. /**
  34. * Construct a WebSocket. WebSocket decorates a Socket for WebSocket
  35. * (RFC 6455) behavior.
  36. * @param s Socket to be decorated with WebSocket behavior.
  37. */
  38. public WebSocket(final Socket s) {
  39. this.socket = s;
  40. }
  41. @Override
  42. public final void connect(final SocketAddress endpoint) throws IOException {
  43. socket.connect(endpoint);
  44. }
  45. @Override
  46. public final void connect(final SocketAddress endpoint, final int timeout)
  47. throws IOException {
  48. socket.connect(endpoint, timeout);
  49. }
  50. @Override
  51. public final void bind(final SocketAddress bindpoint) throws IOException {
  52. socket.bind(bindpoint);
  53. }
  54. @Override
  55. public final InetAddress getInetAddress() {
  56. return socket.getInetAddress();
  57. }
  58. @Override
  59. public final InetAddress getLocalAddress() {
  60. return socket.getLocalAddress();
  61. }
  62. @Override
  63. public final int getPort() {
  64. return socket.getPort();
  65. }
  66. @Override
  67. public final int getLocalPort() {
  68. return socket.getLocalPort();
  69. }
  70. @Override
  71. public final SocketAddress getRemoteSocketAddress() {
  72. return socket.getRemoteSocketAddress();
  73. }
  74. @Override
  75. public final SocketAddress getLocalSocketAddress() {
  76. return socket.getLocalSocketAddress();
  77. }
  78. @Override
  79. public final SocketChannel getChannel() {
  80. throw new UnsupportedOperationException();
  81. }
  82. @Override
  83. public final WebSocketServerInputStream getInputStream()
  84. throws IOException {
  85. if (wssos == null) {
  86. this.getOutputStream();
  87. }
  88. if (wssis == null) {
  89. wssis = new WebSocketServerInputStream(
  90. socket.getInputStream(), wssos);
  91. }
  92. return wssis;
  93. }
  94. @Override
  95. public final WebSocketServerOutputStream getOutputStream()
  96. throws IOException {
  97. if (wssos == null) {
  98. wssos = new WebSocketServerOutputStream(socket.getOutputStream());
  99. }
  100. return wssos;
  101. }
  102. @Override
  103. public final void setTcpNoDelay(final boolean on) throws SocketException {
  104. socket.setTcpNoDelay(on);
  105. }
  106. @Override
  107. public final boolean getTcpNoDelay() throws SocketException {
  108. return socket.getTcpNoDelay();
  109. }
  110. @Override
  111. public final void setSoLinger(final boolean on, final int linger)
  112. throws SocketException {
  113. socket.setSoLinger(on, linger);
  114. }
  115. @Override
  116. public final int getSoLinger() throws SocketException {
  117. return socket.getSoLinger();
  118. }
  119. @Override
  120. public final void sendUrgentData(final int data)
  121. throws IOException {
  122. socket.sendUrgentData(data);
  123. }
  124. @Override
  125. public final void setOOBInline(final boolean on)
  126. throws SocketException {
  127. socket.setOOBInline(on);
  128. }
  129. @Override
  130. public final boolean getOOBInline() throws SocketException {
  131. return socket.getOOBInline();
  132. }
  133. @Override
  134. public final synchronized void setSoTimeout(final int timeout)
  135. throws SocketException {
  136. socket.setSoTimeout(timeout);
  137. }
  138. @Override
  139. public final synchronized int getSoTimeout() throws SocketException {
  140. return socket.getSoTimeout();
  141. }
  142. @Override
  143. public final synchronized void setSendBufferSize(final int size)
  144. throws SocketException {
  145. socket.setSendBufferSize(size);
  146. }
  147. @Override
  148. public final synchronized int getSendBufferSize() throws SocketException {
  149. return socket.getSendBufferSize();
  150. }
  151. @Override
  152. public final synchronized void setReceiveBufferSize(final int size)
  153. throws SocketException {
  154. socket.setReceiveBufferSize(size);
  155. }
  156. @Override
  157. public final synchronized int getReceiveBufferSize()
  158. throws SocketException {
  159. return socket.getReceiveBufferSize();
  160. }
  161. @Override
  162. public final void setKeepAlive(final boolean on) throws SocketException {
  163. socket.setKeepAlive(on);
  164. }
  165. @Override
  166. public final boolean getKeepAlive() throws SocketException {
  167. return socket.getKeepAlive();
  168. }
  169. @Override
  170. public final void setTrafficClass(final int tc) throws SocketException {
  171. socket.setTrafficClass(tc);
  172. }
  173. @Override
  174. public final int getTrafficClass() throws SocketException {
  175. return socket.getTrafficClass();
  176. }
  177. @Override
  178. public final void setReuseAddress(final boolean on) throws SocketException {
  179. socket.setReuseAddress(on);
  180. }
  181. @Override
  182. public final boolean getReuseAddress() throws SocketException {
  183. return socket.getReuseAddress();
  184. }
  185. @Override
  186. public final synchronized void close() throws IOException {
  187. socket.close();
  188. }
  189. @Override
  190. public final void shutdownInput() throws IOException {
  191. socket.shutdownInput();
  192. }
  193. @Override
  194. public final void shutdownOutput() throws IOException {
  195. socket.shutdownOutput();
  196. }
  197. @Override
  198. public final String toString() {
  199. return socket.toString();
  200. }
  201. @Override
  202. public final boolean isConnected() {
  203. return socket.isConnected();
  204. }
  205. @Override
  206. public final boolean isBound() {
  207. return socket.isBound();
  208. }
  209. @Override
  210. public final boolean isClosed() {
  211. return socket.isClosed();
  212. }
  213. @Override
  214. public final boolean isInputShutdown() {
  215. return socket.isInputShutdown();
  216. }
  217. @Override
  218. public final boolean isOutputShutdown() {
  219. return socket.isOutputShutdown();
  220. }
  221. @Override
  222. public final void setPerformancePreferences(
  223. final int connectionTime,
  224. final int latency,
  225. final int bandwidth) {
  226. socket.setPerformancePreferences(connectionTime, latency, bandwidth);
  227. }
  228. /**
  229. * Socket to be decorated for WebSocket behavior.
  230. */
  231. private final Socket socket;
  232. /**
  233. * WebSocketServerInputStream that decorates the InputStream for
  234. * this Socket. Created on the first call to <code>getInputStream()</code>.
  235. */
  236. private WebSocketServerInputStream wssis = null;
  237. /**
  238. * WebSocketServerOutputStream that decorates the OutputStream for
  239. * this Socket. Created on the first call to <code>getInputStream()</code>
  240. * or <code>getOutputStream()</code>.
  241. */
  242. private WebSocketServerOutputStream wssos = null;
  243. }