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