Socket.hx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // TODO : use TLS when multithread added
  187. static var tmp:hl.Bytes = null;
  188. static var curTmpSize = 0;
  189. static function makeArray(a:Array<Socket>):hl.NativeArray<SocketHandle> {
  190. if (a == null)
  191. return null;
  192. var arr = new hl.NativeArray(a.length);
  193. for (i in 0...a.length)
  194. arr[i] = a[i].__s;
  195. return arr;
  196. }
  197. static function outArray(a:hl.NativeArray<SocketHandle>, original:Array<Socket>):Array<Socket> {
  198. var out = [];
  199. if (a == null)
  200. return out;
  201. var i = 0, p = 0;
  202. var max = original.length;
  203. while (i < max) {
  204. var sh = a[i++];
  205. if (sh == null)
  206. break;
  207. while (original[p].__s != sh)
  208. p++;
  209. out.push(original[p++]);
  210. }
  211. return out;
  212. }
  213. public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
  214. ?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
  215. var sread = makeArray(read);
  216. var swrite = makeArray(write);
  217. var sothers = makeArray(others);
  218. var tmpSize = 0;
  219. if (sread != null)
  220. tmpSize += socket_fd_size(sread.length);
  221. if (swrite != null)
  222. tmpSize += socket_fd_size(swrite.length);
  223. if (sothers != null)
  224. tmpSize += socket_fd_size(sothers.length);
  225. if (tmpSize > curTmpSize) {
  226. tmp = new hl.Bytes(tmpSize);
  227. curTmpSize = tmpSize;
  228. }
  229. if (!socket_select(sread, swrite, sothers, tmp, curTmpSize, timeout == null ? -1 : timeout))
  230. throw "Error while waiting on socket";
  231. return {
  232. read: outArray(sread, read),
  233. write: outArray(swrite, write),
  234. others: outArray(sothers, others),
  235. };
  236. }
  237. @:hlNative("std", "socket_init") static function socket_init():Void {}
  238. @:hlNative("std", "socket_new") static function socket_new(udp:Bool):SocketHandle {
  239. return null;
  240. }
  241. @:hlNative("std", "socket_close") static function socket_close(s:SocketHandle):Void {}
  242. @:hlNative("std", "socket_connect") static function socket_connect(s:SocketHandle, host:Int, port:Int):Bool {
  243. return true;
  244. }
  245. @:hlNative("std", "socket_listen") static function socket_listen(s:SocketHandle, count:Int):Bool {
  246. return true;
  247. }
  248. @:hlNative("std", "socket_bind") static function socket_bind(s:SocketHandle, host:Int, port:Int):Bool {
  249. return true;
  250. }
  251. @:hlNative("std", "socket_accept") static function socket_accept(s:SocketHandle):SocketHandle {
  252. return null;
  253. }
  254. @:hlNative("std", "socket_peer") static function socket_peer(s:SocketHandle, host:hl.Ref<Int>, port:hl.Ref<Int>):Bool {
  255. return true;
  256. }
  257. @:hlNative("std", "socket_host") static function socket_host(s:SocketHandle, host:hl.Ref<Int>, port:hl.Ref<Int>):Bool {
  258. return true;
  259. }
  260. @:hlNative("std", "socket_set_timeout") static function socket_set_timeout(s:SocketHandle, timeout:Float):Bool {
  261. return true;
  262. }
  263. @:hlNative("std", "socket_shutdown") static function socket_shutdown(s:SocketHandle, read:Bool, write:Bool):Bool {
  264. return true;
  265. }
  266. @:hlNative("std", "socket_set_blocking") static function socket_set_blocking(s:SocketHandle, b:Bool):Bool {
  267. return true;
  268. }
  269. @:hlNative("std", "socket_set_fast_send") static function socket_set_fast_send(s:SocketHandle, b:Bool):Bool {
  270. return true;
  271. }
  272. @:hlNative("std", "socket_fd_size") static function socket_fd_size(count:Int):Int {
  273. return 0;
  274. }
  275. @:hlNative("std", "socket_select") static function socket_select(read:hl.NativeArray<SocketHandle>, write:hl.NativeArray<SocketHandle>,
  276. other:hl.NativeArray<SocketHandle>, tmpData:hl.Bytes, tmpSize:Int, timeout:Float):Bool {
  277. return false;
  278. }
  279. }