Socket.hx 7.6 KB

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