Socket.hx 7.6 KB

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