Socket.hx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C)2005-2019 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. // We need to keep these values so that we can restore
  103. // them if we re-create the socket for ipv6 as in
  104. // connect() and bind() below.
  105. private var __timeout:Float = 0.0;
  106. private var __blocking:Bool = true;
  107. private var __fastSend:Bool = false;
  108. public var input(default,null) : haxe.io.Input;
  109. public var output(default,null) : haxe.io.Output;
  110. public var custom : Dynamic;
  111. public function new() : Void {
  112. init();
  113. }
  114. private function init() : Void {
  115. if( __s == null )__s = NativeSocket.socket_new(false);
  116. // Restore these values if they changed. This can happen
  117. // in connect() and bind() if using an ipv6 address.
  118. setTimeout(__timeout);
  119. setBlocking(__blocking);
  120. setFastSend(__fastSend);
  121. input = new SocketInput(__s);
  122. output = new SocketOutput(__s);
  123. }
  124. public function close() : Void {
  125. NativeSocket.socket_close(__s);
  126. untyped {
  127. var input : SocketInput = cast input;
  128. var output : SocketOutput = cast output;
  129. input.__s = null;
  130. output.__s = null;
  131. }
  132. input.close();
  133. output.close();
  134. }
  135. public function read() : String {
  136. var bytes:haxe.io.BytesData = NativeSocket.socket_read(__s);
  137. if (bytes==null) return "";
  138. var arr:Array<cpp.Char> = cast bytes;
  139. return NativeString.fromPointer(Pointer.ofArray(arr));
  140. }
  141. public function write( content : String ) : Void {
  142. NativeSocket.socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
  143. }
  144. public function connect(host : Host, port : Int) : Void {
  145. try {
  146. if (host.ip==0 && host.host!="0.0.0.0") {
  147. // hack, hack, hack
  148. var ipv6:haxe.io.BytesData = Reflect.field(host,"ipv6");
  149. if (ipv6!=null)
  150. {
  151. close();
  152. __s = NativeSocket.socket_new_ip(false,true);
  153. init();
  154. NativeSocket.socket_connect_ipv6(__s, ipv6, port);
  155. }
  156. else
  157. throw "Unresolved host";
  158. }
  159. else
  160. NativeSocket.socket_connect(__s, host.ip, port);
  161. } catch( s : String ) {
  162. if( s == "Invalid socket handle" )
  163. throw "Failed to connect on "+host.toString()+":"+port;
  164. else if (s == "Blocking") {
  165. // Do nothing, this is not a real error, it simply indicates
  166. // that a non-blocking connect is in progress
  167. }
  168. else
  169. cpp.Lib.rethrow(s);
  170. }
  171. }
  172. public function listen(connections : Int) : Void {
  173. NativeSocket.socket_listen(__s, connections);
  174. }
  175. public function shutdown( read : Bool, write : Bool ) : Void {
  176. NativeSocket.socket_shutdown(__s,read,write);
  177. }
  178. public function bind(host : Host, port : Int) : Void {
  179. if (host.ip==0 && host.host!="0.0.0.0")
  180. {
  181. var ipv6:haxe.io.BytesData = Reflect.field(host,"ipv6");
  182. if (ipv6!=null)
  183. {
  184. close();
  185. __s = NativeSocket.socket_new_ip(false,true);
  186. init();
  187. NativeSocket.socket_bind_ipv6(__s, ipv6, port);
  188. }
  189. else
  190. throw "Unresolved host";
  191. }
  192. else
  193. NativeSocket.socket_bind(__s, host.ip, port);
  194. }
  195. public function accept() : Socket {
  196. var c = NativeSocket.socket_accept(__s);
  197. var s = Type.createEmptyInstance(Socket);
  198. s.__s = c;
  199. s.input = new SocketInput(c);
  200. s.output = new SocketOutput(c);
  201. return s;
  202. }
  203. public function peer() : { host : Host, port : Int } {
  204. var a : Dynamic = NativeSocket.socket_peer(__s);
  205. if (a == null) {
  206. return null;
  207. }
  208. var h = new Host("127.0.0.1");
  209. untyped h.ip = a[0];
  210. return { host : h, port : a[1] };
  211. }
  212. public function host() : { host : Host, port : Int } {
  213. var a : Dynamic = NativeSocket.socket_host(__s);
  214. if (a == null) {
  215. return null;
  216. }
  217. var h = new Host("127.0.0.1");
  218. untyped h.ip = a[0];
  219. return { host : h, port : a[1] };
  220. }
  221. public function setTimeout( timeout : Float ) : Void {
  222. __timeout = timeout;
  223. NativeSocket.socket_set_timeout(__s, timeout);
  224. }
  225. public function waitForRead() : Void {
  226. select([this],null,null,null);
  227. }
  228. public function setBlocking( b : Bool ) : Void {
  229. __blocking = b;
  230. NativeSocket.socket_set_blocking(__s,b);
  231. }
  232. public function setFastSend( b : Bool ) : Void {
  233. __fastSend = b;
  234. NativeSocket.socket_set_fast_send(__s,b);
  235. }
  236. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float ) : {read: Array<Socket>,write: Array<Socket>,others: Array<Socket>} {
  237. var neko_array = NativeSocket.socket_select(read,write,others, timeout);
  238. if (neko_array==null)
  239. throw "Select error";
  240. return @:fixed {
  241. read: neko_array[0],
  242. write: neko_array[1],
  243. others: neko_array[2]
  244. };
  245. }
  246. }