Socket.hx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C)2005-2018 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 haxe.io.Error;
  24. import cpp.NativeSocket;
  25. private class SocketInput extends haxe.io.Input {
  26. var __s : Dynamic;
  27. public function new(s:Dynamic) {
  28. __s = s;
  29. }
  30. public override function readByte() {
  31. return try {
  32. NativeSocket.socket_recv_char(__s);
  33. } catch( e : Dynamic ) {
  34. if( e == "Blocking" )
  35. throw Blocked;
  36. else if( __s == null )
  37. throw Custom(e);
  38. else
  39. throw new haxe.io.Eof();
  40. }
  41. }
  42. public override function readBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  43. var r;
  44. if (__s==null)
  45. throw "Invalid handle";
  46. try {
  47. r = NativeSocket.socket_recv(__s,buf.getData(),pos,len);
  48. } catch( e : Dynamic ) {
  49. if( e == "Blocking" )
  50. throw Blocked;
  51. else
  52. throw Custom(e);
  53. }
  54. if( r == 0 )
  55. throw new haxe.io.Eof();
  56. return r;
  57. }
  58. public override function close() {
  59. super.close();
  60. if( __s != null ) NativeSocket.socket_close(__s);
  61. }
  62. }
  63. private class SocketOutput extends haxe.io.Output {
  64. var __s : Dynamic;
  65. public function new(s:Dynamic) {
  66. __s = s;
  67. }
  68. public override function writeByte( c : Int ) {
  69. if (__s==null)
  70. throw "Invalid handle";
  71. try {
  72. NativeSocket.socket_send_char(__s, c);
  73. } catch( e : Dynamic ) {
  74. if( e == "Blocking" )
  75. throw Blocked;
  76. else
  77. throw Custom(e);
  78. }
  79. }
  80. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
  81. return try {
  82. NativeSocket.socket_send(__s, buf.getData(), pos, len);
  83. } catch( e : Dynamic ) {
  84. if( e == "Blocking" )
  85. throw Blocked;
  86. else if (e == "EOF")
  87. throw new haxe.io.Eof();
  88. else
  89. throw Custom(e);
  90. }
  91. }
  92. public override function close() {
  93. super.close();
  94. if( __s != null ) NativeSocket.socket_close(__s);
  95. }
  96. }
  97. @:coreApi
  98. class Socket {
  99. private var __s : Dynamic;
  100. public var input(default,null) : haxe.io.Input;
  101. public var output(default,null) : haxe.io.Output;
  102. public var custom : Dynamic;
  103. public function new() : Void {
  104. init();
  105. }
  106. private function init() : Void {
  107. if( __s == null )__s = NativeSocket.socket_new(false);
  108. input = new SocketInput(__s);
  109. output = new SocketOutput(__s);
  110. }
  111. public function close() : Void {
  112. NativeSocket.socket_close(__s);
  113. untyped {
  114. var input : SocketInput = cast input;
  115. var output : SocketOutput = cast output;
  116. input.__s = null;
  117. output.__s = null;
  118. }
  119. input.close();
  120. output.close();
  121. }
  122. public function read() : String {
  123. var bytes:haxe.io.BytesData = NativeSocket.socket_read(__s);
  124. if (bytes==null) return "";
  125. return bytes.toString();
  126. }
  127. public function write( content : String ) : Void {
  128. NativeSocket.socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
  129. }
  130. public function connect(host : Host, port : Int) : Void {
  131. try {
  132. if (host.ip==0 && host.host!="0.0.0.0") {
  133. // hack, hack, hack
  134. var ipv6:haxe.io.BytesData = Reflect.field(host,"ipv6");
  135. if (ipv6!=null)
  136. {
  137. close();
  138. __s = NativeSocket.socket_new_ip(false,true);
  139. init();
  140. NativeSocket.socket_connect_ipv6(__s, ipv6, port);
  141. }
  142. else
  143. throw "Unresolved host";
  144. }
  145. else
  146. NativeSocket.socket_connect(__s, host.ip, port);
  147. } catch( s : String ) {
  148. if( s == "Invalid socket handle" )
  149. throw "Failed to connect on "+host.toString()+":"+port;
  150. else if (s == "Blocking") {
  151. // Do nothing, this is not a real error, it simply indicates
  152. // that a non-blocking connect is in progress
  153. }
  154. else
  155. cpp.Lib.rethrow(s);
  156. }
  157. }
  158. public function listen(connections : Int) : Void {
  159. NativeSocket.socket_listen(__s, connections);
  160. }
  161. public function shutdown( read : Bool, write : Bool ) : Void {
  162. NativeSocket.socket_shutdown(__s,read,write);
  163. }
  164. public function bind(host : Host, port : Int) : Void {
  165. if (host.ip==0 && host.host!="0.0.0.0")
  166. {
  167. var ipv6:haxe.io.BytesData = Reflect.field(host,"ipv6");
  168. if (ipv6!=null)
  169. {
  170. close();
  171. __s = NativeSocket.socket_new_ip(false,true);
  172. init();
  173. NativeSocket.socket_bind_ipv6(__s, ipv6, port);
  174. }
  175. else
  176. throw "Unresolved host";
  177. }
  178. else
  179. NativeSocket.socket_bind(__s, host.ip, port);
  180. }
  181. public function accept() : Socket {
  182. var c = NativeSocket.socket_accept(__s);
  183. var s = Type.createEmptyInstance(Socket);
  184. s.__s = c;
  185. s.input = new SocketInput(c);
  186. s.output = new SocketOutput(c);
  187. return s;
  188. }
  189. public function peer() : { host : Host, port : Int } {
  190. var a : Dynamic = NativeSocket.socket_peer(__s);
  191. if (a == null) {
  192. return null;
  193. }
  194. var h = new Host("127.0.0.1");
  195. untyped h.ip = a[0];
  196. return { host : h, port : a[1] };
  197. }
  198. public function host() : { host : Host, port : Int } {
  199. var a : Dynamic = NativeSocket.socket_host(__s);
  200. if (a == null) {
  201. return null;
  202. }
  203. var h = new Host("127.0.0.1");
  204. untyped h.ip = a[0];
  205. return { host : h, port : a[1] };
  206. }
  207. public function setTimeout( timeout : Float ) : Void {
  208. NativeSocket.socket_set_timeout(__s, timeout);
  209. }
  210. public function waitForRead() : Void {
  211. select([this],null,null,null);
  212. }
  213. public function setBlocking( b : Bool ) : Void {
  214. NativeSocket.socket_set_blocking(__s,b);
  215. }
  216. public function setFastSend( b : Bool ) : Void {
  217. NativeSocket.socket_set_fast_send(__s,b);
  218. }
  219. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float ) : {read: Array<Socket>,write: Array<Socket>,others: Array<Socket>} {
  220. var neko_array = NativeSocket.socket_select(read,write,others, timeout);
  221. if (neko_array==null)
  222. throw "Select error";
  223. return @:fixed {
  224. read: neko_array[0],
  225. write: neko_array[1],
  226. others: neko_array[2]
  227. };
  228. }
  229. }