Socket.hx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #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 ) throw Blocked;
  38. throw new haxe.io.Eof();
  39. }
  40. }
  41. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
  42. if( pos < 0 || len < 0 || pos + len > buf.length ) throw haxe.io.Error.OutsideBounds;
  43. var n = socket_send(@:privateAccess sock.__s, buf.getData().bytes, pos, len);
  44. if( n < 0 ) {
  45. if( n == -1 ) throw Blocked;
  46. throw new haxe.io.Eof();
  47. }
  48. return n;
  49. }
  50. public override function close() {
  51. sock.close();
  52. }
  53. @:hlNative("std","socket_send_char") static function socket_send_char( s : SocketHandle, c : Int ) : Int { return 0; }
  54. @:hlNative("std","socket_send") static function socket_send( s : SocketHandle, bytes : hl.Bytes, pos : Int, len : Int ) : Int { return 0; }
  55. }
  56. private class SocketInput extends haxe.io.Input {
  57. var sock : Socket;
  58. public function new(s) {
  59. sock = s;
  60. }
  61. public override function readByte() : Int {
  62. var c = socket_recv_char(@:privateAccess sock.__s);
  63. if( c < 0 ) {
  64. if( c == -1 ) throw Blocked;
  65. throw new haxe.io.Eof();
  66. }
  67. return c;
  68. }
  69. public override function readBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  70. if( pos < 0 || len < 0 || pos + len > buf.length ) throw haxe.io.Error.OutsideBounds;
  71. var r = socket_recv(@:privateAccess sock.__s,buf.getData().bytes,pos,len);
  72. if( r < 0 ) {
  73. if( r == -1 ) throw Blocked;
  74. throw new haxe.io.Eof();
  75. }
  76. return r;
  77. }
  78. public override function close() {
  79. sock.close();
  80. }
  81. @:hlNative("std","socket_recv") static function socket_recv( s : SocketHandle, bytes : hl.Bytes, pos : Int, len : Int ) : Int { return 0; }
  82. @:hlNative("std","socket_recv_char") static function socket_recv_char( s : SocketHandle ) : Int { return 0; }
  83. }
  84. @:coreApi
  85. @:keepInit
  86. class Socket {
  87. private var __s : SocketHandle;
  88. public var input(default,null) : haxe.io.Input;
  89. public var output(default,null) : haxe.io.Output;
  90. public var custom : Dynamic;
  91. static function __init__() : Void {
  92. socket_init();
  93. }
  94. public function new() : Void {
  95. init();
  96. }
  97. function init() : Void {
  98. __s = socket_new(false);
  99. input = new SocketInput(this);
  100. output = new SocketOutput(this);
  101. }
  102. public function close() : Void {
  103. if( __s != null ) {
  104. socket_close(__s);
  105. __s = null;
  106. }
  107. }
  108. public function read() : String {
  109. return input.readAll().toString();
  110. }
  111. public function write( content : String ) : Void {
  112. output.writeString(content);
  113. }
  114. public function connect(host : Host, port : Int) : Void {
  115. if( !socket_connect(__s, host.ip, port) )
  116. throw new Sys.SysError("Failed to connect on "+host.toString()+":"+port);
  117. }
  118. public function listen( connections : Int ) : Void {
  119. if( !socket_listen(__s, connections) )
  120. throw new Sys.SysError("listen() failure");
  121. }
  122. public function shutdown( read : Bool, write : Bool ) : Void {
  123. if( !socket_shutdown(__s, read, write) )
  124. throw new Sys.SysError("shutdown() failure");
  125. }
  126. public function bind(host : Host, port : Int) : Void {
  127. if( !socket_bind(__s, host.ip, port) )
  128. throw new Sys.SysError("Cannot bind socket on " + host + ":" + port);
  129. }
  130. public function accept() : Socket {
  131. var c = socket_accept(__s);
  132. if( c == null )
  133. return null;
  134. var s : Socket = untyped $new(Socket);
  135. s.__s = c;
  136. s.input = new SocketInput(s);
  137. s.output = new SocketOutput(s);
  138. return s;
  139. }
  140. public function peer() : { host : Host, port : Int } {
  141. var ip = 0, port = 0;
  142. if( !socket_peer(__s, ip, port) )
  143. return null;
  144. var h : Host = untyped $new(Host);
  145. @:privateAccess h.ip = ip;
  146. return { host : h, port : port };
  147. }
  148. public function host() : { host : Host, port : Int } {
  149. var ip = 0, port = 0;
  150. if( !socket_host(__s, ip, port) )
  151. return null;
  152. var h : Host = untyped $new(Host);
  153. @:privateAccess h.ip = ip;
  154. return { host : h, port : port };
  155. }
  156. public function setTimeout( timeout : Float ) : Void {
  157. if( !socket_set_timeout(__s, timeout) ) throw new Sys.SysError("setTimeout() failure");
  158. }
  159. public function waitForRead() : Void {
  160. select([this],null,null,null);
  161. }
  162. public function setBlocking( b : Bool ) : Void {
  163. if( !socket_set_blocking(__s, b) ) throw new Sys.SysError("setBlocking() failure");
  164. }
  165. public function setFastSend( b : Bool ) : Void {
  166. if( !socket_set_fast_send(__s,b) ) throw new Sys.SysError("setFastSend() failure");
  167. }
  168. // TODO : use TLS when multithread added
  169. static var tmp : hl.Bytes = null;
  170. static var curTmpSize = 0;
  171. static function makeArray( a : Array<Socket> ) : hl.NativeArray<SocketHandle> {
  172. if( a == null ) return null;
  173. var arr = new hl.NativeArray(a.length);
  174. for( i in 0...a.length )
  175. arr[i] = a[i].__s;
  176. return arr;
  177. }
  178. static function outArray( a : hl.NativeArray<SocketHandle>, original : Array<Socket> ) : Array<Socket> {
  179. var out = [];
  180. if( a == null ) return out;
  181. var i = 0, p = 0;
  182. var max = original.length;
  183. while( i < max ) {
  184. var sh = a[i++];
  185. if( sh == null ) break;
  186. while( original[p].__s != sh ) p++;
  187. out.push(original[p++]);
  188. }
  189. return out;
  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 sread = makeArray(read);
  193. var swrite = makeArray(write);
  194. var sothers = makeArray(others);
  195. var tmpSize = 0;
  196. if( sread != null ) tmpSize += socket_fd_size(sread.length);
  197. if( swrite != null ) tmpSize += socket_fd_size(swrite.length);
  198. if( sothers != null ) tmpSize += socket_fd_size(sothers.length);
  199. if( tmpSize > curTmpSize ) {
  200. tmp = new hl.Bytes(tmpSize);
  201. curTmpSize = tmpSize;
  202. }
  203. if( !socket_select(sread, swrite, sothers, tmp, curTmpSize, timeout == null ? -1 : timeout) )
  204. throw "Error while waiting on socket";
  205. return {
  206. read : outArray(sread,read),
  207. write : outArray(swrite,write),
  208. others : outArray(sothers,others),
  209. };
  210. }
  211. @:hlNative("std", "socket_init") static function socket_init() : Void {}
  212. @:hlNative("std", "socket_new") static function socket_new( udp : Bool ) : SocketHandle { return null; }
  213. @:hlNative("std", "socket_close") static function socket_close( s : SocketHandle ) : Void { }
  214. @:hlNative("std", "socket_connect") static function socket_connect( s : SocketHandle, host : Int, port : Int ) : Bool { return true; }
  215. @:hlNative("std", "socket_listen") static function socket_listen( s : SocketHandle, count : Int ) : Bool { return true; }
  216. @:hlNative("std", "socket_bind") static function socket_bind( s : SocketHandle, host : Int, port : Int ) : Bool { return true; }
  217. @:hlNative("std", "socket_accept") static function socket_accept( s : SocketHandle ) : SocketHandle { return null; }
  218. @:hlNative("std", "socket_peer") static function socket_peer( s : SocketHandle, host : hl.Ref<Int>, port : hl.Ref<Int> ) : Bool { return true; }
  219. @:hlNative("std", "socket_host") static function socket_host( s : SocketHandle, host : hl.Ref<Int>, port : hl.Ref<Int> ) : Bool { return true; }
  220. @:hlNative("std", "socket_set_timeout") static function socket_set_timeout( s : SocketHandle, timeout : Float ) : Bool { return true; }
  221. @:hlNative("std", "socket_shutdown") static function socket_shutdown( s : SocketHandle, read : Bool, write : Bool ) : Bool { return true; }
  222. @:hlNative("std", "socket_set_blocking") static function socket_set_blocking( s : SocketHandle, b : Bool ) : Bool { return true; }
  223. @:hlNative("std", "socket_set_fast_send") static function socket_set_fast_send( s : SocketHandle, b : Bool ) : Bool { return true; }
  224. @:hlNative("std", "socket_fd_size") static function socket_fd_size( count : Int) : Int { return 0; }
  225. @:hlNative("std", "socket_select") static function socket_select( read : hl.NativeArray<SocketHandle>, write : hl.NativeArray<SocketHandle>, other : hl.NativeArray<SocketHandle>, tmpData : hl.Bytes, tmpSize : Int, timeout : Float ) : Bool { return false; }
  226. }