Socket.hx 7.2 KB

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