Socket.hx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #if doc_gen
  25. @:noDoc enum SocketHandle {}
  26. #else
  27. @:noDoc typedef SocketHandle = hl.Abstract<"hl_socket">;
  28. #end
  29. private class SocketOutput extends haxe.io.Output {
  30. var sock:Socket;
  31. public function new(s) {
  32. this.sock = s;
  33. }
  34. public override function writeByte(c:Int) {
  35. var k = socket_send_char(@:privateAccess sock.__s, c);
  36. if (k < 0) {
  37. if (k == -1)
  38. throw Blocked;
  39. throw new haxe.io.Eof();
  40. }
  41. }
  42. public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
  43. if (pos < 0 || len < 0 || pos + len > buf.length)
  44. throw haxe.io.Error.OutsideBounds;
  45. var n = socket_send(@:privateAccess sock.__s, buf.getData().bytes, pos, len);
  46. if (n < 0) {
  47. if (n == -1)
  48. throw Blocked;
  49. throw new haxe.io.Eof();
  50. }
  51. return n;
  52. }
  53. public override function close() {
  54. sock.close();
  55. }
  56. @:hlNative("std", "socket_send_char") static function socket_send_char(s:SocketHandle, c:Int):Int {
  57. return 0;
  58. }
  59. @:hlNative("std", "socket_send") static function socket_send(s:SocketHandle, bytes:hl.Bytes, pos:Int, len:Int):Int {
  60. return 0;
  61. }
  62. }
  63. private class SocketInput extends haxe.io.Input {
  64. var sock:Socket;
  65. public function new(s) {
  66. sock = s;
  67. }
  68. public override function readByte():Int {
  69. var c = socket_recv_char(@:privateAccess sock.__s);
  70. if (c < 0) {
  71. if (c == -1)
  72. throw Blocked;
  73. throw new haxe.io.Eof();
  74. }
  75. return c;
  76. }
  77. public override function readBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
  78. if (pos < 0 || len < 0 || pos + len > buf.length)
  79. throw haxe.io.Error.OutsideBounds;
  80. var r = socket_recv(@:privateAccess sock.__s, buf.getData().bytes, pos, len);
  81. if (r <= 0) {
  82. if (r == -1)
  83. throw Blocked;
  84. throw new haxe.io.Eof();
  85. }
  86. return r;
  87. }
  88. public override function close() {
  89. sock.close();
  90. }
  91. @:hlNative("std", "socket_recv") static function socket_recv(s:SocketHandle, bytes:hl.Bytes, pos:Int, len:Int):Int {
  92. return 0;
  93. }
  94. @:hlNative("std", "socket_recv_char") static function socket_recv_char(s:SocketHandle):Int {
  95. return 0;
  96. }
  97. }
  98. @:coreApi
  99. @:keepInit
  100. class Socket {
  101. private var __s:SocketHandle;
  102. public var input(default, null):haxe.io.Input;
  103. public var output(default, null):haxe.io.Output;
  104. public var custom:Dynamic;
  105. static function __init__():Void {
  106. socket_init();
  107. }
  108. public function new():Void {
  109. init();
  110. }
  111. function init():Void {
  112. if (__s == null)
  113. __s = socket_new(false);
  114. input = new SocketInput(this);
  115. output = new SocketOutput(this);
  116. }
  117. public function close():Void {
  118. if (__s != null) {
  119. socket_close(__s);
  120. __s = null;
  121. }
  122. }
  123. public function read():String {
  124. return input.readAll().toString();
  125. }
  126. public function write(content:String):Void {
  127. output.writeString(content);
  128. }
  129. public function connect(host:Host, port:Int):Void {
  130. if (!socket_connect(__s, host.ip, port))
  131. throw new Sys.SysError("Failed to connect on " + host.toString() + ":" + port);
  132. }
  133. public function listen(connections:Int):Void {
  134. if (!socket_listen(__s, connections))
  135. throw new Sys.SysError("listen() failure");
  136. }
  137. public function shutdown(read:Bool, write:Bool):Void {
  138. if (!socket_shutdown(__s, read, write))
  139. throw new Sys.SysError("shutdown() failure");
  140. }
  141. public function bind(host:Host, port:Int):Void {
  142. if (!socket_bind(__s, host.ip, port))
  143. throw new Sys.SysError("Cannot bind socket on " + host + ":" + port);
  144. }
  145. public function accept():Socket {
  146. var c = socket_accept(__s);
  147. if (c == null)
  148. return null;
  149. var s:Socket = untyped $new(Socket);
  150. s.__s = c;
  151. s.input = new SocketInput(s);
  152. s.output = new SocketOutput(s);
  153. return s;
  154. }
  155. public function peer():{host:Host, port:Int} {
  156. var ip = 0, port = 0;
  157. if (!socket_peer(__s, ip, port))
  158. return null;
  159. var h:Host = untyped $new(Host);
  160. @:privateAccess h.ip = ip;
  161. return {host: h, port: port};
  162. }
  163. public function host():{host:Host, port:Int} {
  164. var ip = 0, port = 0;
  165. if (!socket_host(__s, ip, port))
  166. return null;
  167. var h:Host = untyped $new(Host);
  168. @:privateAccess h.ip = ip;
  169. return {host: h, port: port};
  170. }
  171. public function setTimeout(timeout:Float):Void {
  172. if (!socket_set_timeout(__s, timeout))
  173. throw new Sys.SysError("setTimeout() failure");
  174. }
  175. public function waitForRead():Void {
  176. select([this], null, null, null);
  177. }
  178. public function setBlocking(b:Bool):Void {
  179. if (!socket_set_blocking(__s, b))
  180. throw new Sys.SysError("setBlocking() failure");
  181. }
  182. public function setFastSend(b:Bool):Void {
  183. if (!socket_set_fast_send(__s, b))
  184. throw new Sys.SysError("setFastSend() failure");
  185. }
  186. static function makeArray(a:Array<Socket>):hl.NativeArray<SocketHandle> {
  187. if (a == null)
  188. return null;
  189. var arr = new hl.NativeArray(a.length);
  190. for (i in 0...a.length)
  191. arr[i] = a[i].__s;
  192. return arr;
  193. }
  194. static function outArray(a:hl.NativeArray<SocketHandle>, original:Array<Socket>):Array<Socket> {
  195. var out = [];
  196. if (a == null)
  197. return out;
  198. var i = 0, p = 0;
  199. var max = original.length;
  200. while (i < max) {
  201. var sh = a[i++];
  202. if (sh == null)
  203. break;
  204. while (original[p].__s != sh)
  205. p++;
  206. out.push(original[p++]);
  207. }
  208. return out;
  209. }
  210. static var TMP = new sys.thread.Tls<haxe.io.Bytes>();
  211. public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
  212. ?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
  213. var sread = makeArray(read);
  214. var swrite = makeArray(write);
  215. var sothers = makeArray(others);
  216. var tmpSize = 0;
  217. if (sread != null)
  218. tmpSize += socket_fd_size(sread.length);
  219. if (swrite != null)
  220. tmpSize += socket_fd_size(swrite.length);
  221. if (sothers != null)
  222. tmpSize += socket_fd_size(sothers.length);
  223. var cur = TMP.value;
  224. if (cur == null || tmpSize > cur.length) {
  225. cur = haxe.io.Bytes.alloc(tmpSize);
  226. TMP.value = cur;
  227. }
  228. if (!socket_select(sread, swrite, sothers, cur, cur.length, timeout == null ? -1 : timeout))
  229. throw "Error while waiting on socket";
  230. return {
  231. read: outArray(sread, read),
  232. write: outArray(swrite, write),
  233. others: outArray(sothers, others),
  234. };
  235. }
  236. static function clearCache() : Void {
  237. TMP.value = null;
  238. }
  239. @:hlNative("std", "socket_init") static function socket_init():Void {}
  240. @:hlNative("std", "socket_new") static function socket_new(udp:Bool):SocketHandle {
  241. return null;
  242. }
  243. @:hlNative("std", "socket_close") static function socket_close(s:SocketHandle):Void {}
  244. @:hlNative("std", "socket_connect") static function socket_connect(s:SocketHandle, host:Int, port:Int):Bool {
  245. return true;
  246. }
  247. @:hlNative("std", "socket_listen") static function socket_listen(s:SocketHandle, count:Int):Bool {
  248. return true;
  249. }
  250. @:hlNative("std", "socket_bind") static function socket_bind(s:SocketHandle, host:Int, port:Int):Bool {
  251. return true;
  252. }
  253. @:hlNative("std", "socket_accept") static function socket_accept(s:SocketHandle):SocketHandle {
  254. return null;
  255. }
  256. @:hlNative("std", "socket_peer") static function socket_peer(s:SocketHandle, host:hl.Ref<Int>, port:hl.Ref<Int>):Bool {
  257. return true;
  258. }
  259. @:hlNative("std", "socket_host") static function socket_host(s:SocketHandle, host:hl.Ref<Int>, port:hl.Ref<Int>):Bool {
  260. return true;
  261. }
  262. @:hlNative("std", "socket_set_timeout") static function socket_set_timeout(s:SocketHandle, timeout:Float):Bool {
  263. return true;
  264. }
  265. @:hlNative("std", "socket_shutdown") static function socket_shutdown(s:SocketHandle, read:Bool, write:Bool):Bool {
  266. return true;
  267. }
  268. @:hlNative("std", "socket_set_blocking") static function socket_set_blocking(s:SocketHandle, b:Bool):Bool {
  269. return true;
  270. }
  271. @:hlNative("std", "socket_set_fast_send") static function socket_set_fast_send(s:SocketHandle, b:Bool):Bool {
  272. return true;
  273. }
  274. @:hlNative("std", "socket_fd_size") static function socket_fd_size(count:Int):Int {
  275. return 0;
  276. }
  277. @:hlNative("std", "socket_select") static function socket_select(read:hl.NativeArray<SocketHandle>, write:hl.NativeArray<SocketHandle>,
  278. other:hl.NativeArray<SocketHandle>, tmpData:hl.Bytes, tmpSize:Int, timeout:Float):Bool {
  279. return false;
  280. }
  281. }