Socket.hx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C)2005-2016 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. import cpp.NativeSocket;
  25. private class SocketInput extends haxe.io.Input {
  26. var __s : Dynamic;
  27. public function new(s) {
  28. __s = s;
  29. }
  30. public override function readByte() {
  31. return try {
  32. NativeSocket.socket_recv_char(__s);
  33. } catch( e : Dynamic ) {
  34. if( e == "Blocking" )
  35. throw Blocked;
  36. else if( __s == null )
  37. throw Custom(e);
  38. else
  39. throw new haxe.io.Eof();
  40. }
  41. }
  42. public override function readBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  43. var r;
  44. if (__s==null)
  45. throw "Invalid handle";
  46. try {
  47. r = NativeSocket.socket_recv(__s,buf.getData(),pos,len);
  48. } catch( e : Dynamic ) {
  49. if( e == "Blocking" )
  50. throw Blocked;
  51. else
  52. throw Custom(e);
  53. }
  54. if( r == 0 )
  55. throw new haxe.io.Eof();
  56. return r;
  57. }
  58. public override function close() {
  59. super.close();
  60. if( __s != null ) NativeSocket.socket_close(__s);
  61. }
  62. }
  63. private class SocketOutput extends haxe.io.Output {
  64. var __s : Dynamic;
  65. public function new(s) {
  66. __s = s;
  67. }
  68. public override function writeByte( c : Int ) {
  69. if (__s==null)
  70. throw "Invalid handle";
  71. try {
  72. NativeSocket.socket_send_char(__s, c);
  73. } catch( e : Dynamic ) {
  74. if( e == "Blocking" )
  75. throw Blocked;
  76. else
  77. throw Custom(e);
  78. }
  79. }
  80. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
  81. return try {
  82. NativeSocket.socket_send(__s, buf.getData(), pos, len);
  83. } catch( e : Dynamic ) {
  84. if( e == "Blocking" )
  85. throw Blocked;
  86. else if (e == "EOF")
  87. throw new haxe.io.Eof();
  88. else
  89. throw Custom(e);
  90. }
  91. }
  92. public override function close() {
  93. super.close();
  94. if( __s != null ) NativeSocket.socket_close(__s);
  95. }
  96. }
  97. @:coreApi
  98. class Socket {
  99. private var __s : Dynamic;
  100. public var input(default,null) : haxe.io.Input;
  101. public var output(default,null) : haxe.io.Output;
  102. public var custom : Dynamic;
  103. public function new() : Void {
  104. init();
  105. }
  106. private function init() : Void {
  107. if( __s == null )__s = NativeSocket.socket_new(false);
  108. input = new SocketInput(__s);
  109. output = new SocketOutput(__s);
  110. }
  111. public function close() : Void {
  112. NativeSocket.socket_close(__s);
  113. untyped {
  114. var input : SocketInput = cast input;
  115. var output : SocketOutput = cast output;
  116. input.__s = null;
  117. output.__s = null;
  118. }
  119. input.close();
  120. output.close();
  121. }
  122. public function read() : String {
  123. var bytes:haxe.io.BytesData = NativeSocket.socket_read(__s);
  124. if (bytes==null) return "";
  125. return bytes.toString();
  126. }
  127. public function write( content : String ) : Void {
  128. NativeSocket.socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
  129. }
  130. public function connect(host : Host, port : Int) : Void {
  131. try {
  132. NativeSocket.socket_connect(__s, host.ip, port);
  133. } catch( s : String ) {
  134. if( s == "Invalid socket handle" )
  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. cpp.Lib.rethrow(s);
  142. }
  143. }
  144. public function listen(connections : Int) : Void {
  145. NativeSocket.socket_listen(__s, connections);
  146. }
  147. public function shutdown( read : Bool, write : Bool ) : Void {
  148. NativeSocket.socket_shutdown(__s,read,write);
  149. }
  150. public function bind(host : Host, port : Int) : Void {
  151. NativeSocket.socket_bind(__s, host.ip, port);
  152. }
  153. public function accept() : Socket {
  154. var c = NativeSocket.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 = NativeSocket.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 = NativeSocket.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. NativeSocket.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. NativeSocket.socket_set_blocking(__s,b);
  187. }
  188. public function setFastSend( b : Bool ) : Void {
  189. NativeSocket.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 neko_array = NativeSocket.socket_select(read,write,others, timeout);
  193. if (neko_array==null)
  194. throw "Select error";
  195. return @:fixed {
  196. read: neko_array[0],
  197. write: neko_array[1],
  198. others: neko_array[2]
  199. };
  200. }
  201. }