Socket.hx 7.6 KB

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