Socket.hx 7.1 KB

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