Socket.hx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.net;
  23. import java.net.InetSocketAddress;
  24. @:coreApi
  25. class Socket {
  26. public var input(default,null) : haxe.io.Input;
  27. public var output(default,null) : haxe.io.Output;
  28. /**
  29. A custom value that can be associated with the socket. Can be used to retreive your custom infos after a [select].
  30. ***/
  31. public var custom : Dynamic;
  32. private var sock:java.net.Socket;
  33. private var server:java.net.ServerSocket;
  34. private var boundAddr:java.net.SocketAddress;
  35. /**
  36. Creates a new unconnected socket.
  37. **/
  38. public function new() : Void
  39. {
  40. create();
  41. }
  42. private function create():Void
  43. {
  44. this.sock = new java.net.Socket();
  45. try
  46. {
  47. this.server = new java.net.ServerSocket();
  48. } catch(e:Dynamic) throw e;
  49. }
  50. /**
  51. Closes the socket : make sure to properly close all your sockets or you will crash when you run out of file descriptors.
  52. **/
  53. public function close() : Void
  54. {
  55. try
  56. {
  57. if (sock != null)
  58. this.sock.close();
  59. if (server != null)
  60. this.server.close();
  61. }
  62. catch(e:Dynamic) throw e;
  63. }
  64. /**
  65. Read the whole data available on the socket.
  66. **/
  67. public function read() : String
  68. {
  69. return input.readAll().toString();
  70. }
  71. /**
  72. Write the whole data to the socket output.
  73. **/
  74. public function write( content : String ) : Void
  75. {
  76. output.writeString(content);
  77. }
  78. /**
  79. Connect to the given server host/port. Throw an exception in case we couldn't sucessfully connect.
  80. **/
  81. public function connect( host : Host, port : Int ) : Void
  82. {
  83. try
  84. {
  85. sock.connect(new InetSocketAddress( host.wrapped, port));
  86. this.output = new java.io.NativeOutput(sock.getOutputStream());
  87. this.input = new java.io.NativeInput(sock.getInputStream());
  88. }
  89. catch(e:Dynamic) throw e;
  90. }
  91. /**
  92. Allow the socket to listen for incoming questions. The parameter tells how many pending connections we can have until they get refused. Use [accept()] to accept incoming connections.
  93. **/
  94. public function listen( connections : Int ) : Void
  95. {
  96. if (boundAddr == null) throw "You must bind the Socket to an address!";
  97. try
  98. server.bind(boundAddr,connections)
  99. catch(e:Dynamic) throw e;
  100. }
  101. /**
  102. Shutdown the socket, either for reading or writing.
  103. **/
  104. public function shutdown( read : Bool, write : Bool ) : Void
  105. {
  106. try
  107. {
  108. if (read)
  109. sock.shutdownInput();
  110. if (write)
  111. sock.shutdownOutput();
  112. }
  113. catch(e:Dynamic) throw e;
  114. }
  115. /**
  116. Bind the socket to the given host/port so it can afterwards listen for connections there.
  117. **/
  118. public function bind( host : Host, port : Int ) : Void
  119. {
  120. if (boundAddr != null)
  121. {
  122. if (server.isBound()) throw "Already bound";
  123. }
  124. this.boundAddr = new java.net.InetSocketAddress(host.wrapped, port);
  125. }
  126. /**
  127. Accept a new connected client. This will return a connected socket on which you can read/write some data.
  128. **/
  129. public function accept() : Socket
  130. {
  131. var ret = try server.accept() catch(e:Dynamic) throw e;
  132. var s = new Socket();
  133. s.sock = ret;
  134. return s;
  135. }
  136. /**
  137. Return the informations about the other side of a connected socket.
  138. **/
  139. public function peer() : { host : Host, port : Int }
  140. {
  141. var rem:java.net.InetSocketAddress = cast sock.getInetAddress();
  142. if (rem == null) return null;
  143. var host = new Host(null);
  144. host.wrapped = rem.getAddress();
  145. return { host: host, port: rem.getPort() };
  146. }
  147. /**
  148. Return the informations about our side of a connected socket.
  149. **/
  150. public function host() : { host : Host, port : Int }
  151. {
  152. var local = sock.getLocalAddress();
  153. var host = new Host(null);
  154. host.wrapped = local;
  155. return { host: host, port: sock.getPort() };
  156. }
  157. /**
  158. Gives a timeout after which blocking socket operations (such as reading and writing) will abort and throw an exception.
  159. **/
  160. public function setTimeout( timeout : Float ) : Void
  161. {
  162. try
  163. sock.setSoTimeout( Std.int(timeout * 1000) )
  164. catch(e:Dynamic) throw e;
  165. }
  166. /**
  167. Block until some data is available for read on the socket.
  168. **/
  169. public function waitForRead() : Void
  170. {
  171. throw "Not implemented";
  172. }
  173. /**
  174. Change the blocking mode of the socket. A blocking socket is the default behavior. A non-blocking socket will abort blocking operations immediatly by throwing a haxe.io.Error.Blocking value.
  175. **/
  176. public function setBlocking( b : Bool ) : Void
  177. {
  178. throw "Not implemented";
  179. }
  180. /**
  181. Allows the socket to immediatly send the data when written to its output : this will cause less ping but might increase the number of packets / data size, especially when doing a lot of small writes.
  182. **/
  183. public function setFastSend( b : Bool ) : Void
  184. {
  185. try
  186. sock.setTcpNoDelay(b)
  187. catch(e:Dynamic) throw e;
  188. }
  189. /**
  190. Wait until one of the sockets groups is ready for the given operation :
  191. [read] contains sockets on which we want to wait for available data to be read,
  192. [write] contains sockets on which we want to wait until we are allowed to write some data to their output buffers,
  193. [others] contains sockets on which we want to wait for exceptional conditions.
  194. [select] will block until one of the condition is met, in which case it will return the sockets for which the condition was true.
  195. In case a [timeout] (in seconds) is specified, select might wait at worse until the timeout expires.
  196. **/
  197. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> }
  198. {
  199. throw "Not implemented";
  200. return null;
  201. }
  202. }