Socket.hx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. *
  25. * Contributor: Lee McColl Sylvester
  26. */
  27. package sys.net;
  28. import haxe.io.Error;
  29. private typedef SocketHandle = Dynamic;
  30. private class SocketInput extends haxe.io.Input {
  31. var __s : SocketHandle;
  32. public function new(s) {
  33. __s = s;
  34. }
  35. public override function readByte() {
  36. return try {
  37. socket_recv_char(__s);
  38. } catch( e : Dynamic ) {
  39. if( e == "Blocking" )
  40. throw Blocked;
  41. else if( __s == null )
  42. throw Custom(e);
  43. else
  44. throw new haxe.io.Eof();
  45. }
  46. }
  47. public override function readBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  48. var r;
  49. if (__s==null)
  50. throw "Invalid handle";
  51. try {
  52. r = socket_recv(__s,buf.getData(),pos,len);
  53. } catch( e : Dynamic ) {
  54. if( e == "Blocking" )
  55. throw Blocked;
  56. else
  57. throw Custom(e);
  58. }
  59. if( r == 0 )
  60. throw new haxe.io.Eof();
  61. return r;
  62. }
  63. public override function close() {
  64. super.close();
  65. if( __s != null ) socket_close(__s);
  66. }
  67. private static var socket_recv = cpp.Lib.load("std","socket_recv",4);
  68. private static var socket_recv_char = cpp.Lib.load("std","socket_recv_char",1);
  69. private static var socket_close = cpp.Lib.load("std","socket_close",1);
  70. }
  71. private class SocketOutput extends haxe.io.Output {
  72. var __s : SocketHandle;
  73. public function new(s) {
  74. __s = s;
  75. }
  76. public override function writeByte( c : Int ) {
  77. if (__s==null)
  78. throw "Invalid handle";
  79. try {
  80. socket_send_char(__s, c);
  81. } catch( e : Dynamic ) {
  82. if( e == "Blocking" )
  83. throw Blocked;
  84. else
  85. throw Custom(e);
  86. }
  87. }
  88. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
  89. return try {
  90. socket_send(__s, buf.getData(), pos, len);
  91. } catch( e : Dynamic ) {
  92. if( e == "Blocking" )
  93. throw Blocked;
  94. else
  95. throw Custom(e);
  96. }
  97. }
  98. public override function close() {
  99. super.close();
  100. if( __s != null ) socket_close(__s);
  101. }
  102. private static var socket_close = cpp.Lib.load("std","socket_close",1);
  103. private static var socket_send_char = cpp.Lib.load("std","socket_send_char",2);
  104. private static var socket_send = cpp.Lib.load("std","socket_send",4);
  105. }
  106. @:core_api
  107. class Socket {
  108. private var __s : SocketHandle;
  109. public var input(default,null) : haxe.io.Input;
  110. public var output(default,null) : haxe.io.Output;
  111. public var custom : Dynamic;
  112. public function new() : Void {
  113. __s = socket_new(false);
  114. input = new SocketInput(__s);
  115. output = new SocketOutput(__s);
  116. }
  117. public function close() : Void {
  118. socket_close(__s);
  119. untyped {
  120. input.__s = null;
  121. output.__s = null;
  122. }
  123. input.close();
  124. output.close();
  125. }
  126. public function read() : String {
  127. var bytes:haxe.io.BytesData = socket_read(__s);
  128. if (bytes==null) return "";
  129. return bytes.toString();
  130. }
  131. public function write( content : String ) : Void {
  132. socket_write(__s, haxe.io.Bytes.ofString(content).getData() );
  133. }
  134. public function connect(host : Host, port : Int) : Void {
  135. try {
  136. socket_connect(__s, host.ip, port);
  137. } catch( s : String ) {
  138. if( s == "std@socket_connect" )
  139. throw "Failed to connect on "+(try host.reverse() catch( e : Dynamic ) host.toString())+":"+port;
  140. else
  141. cpp.Lib.rethrow(s);
  142. }
  143. }
  144. public function listen(connections : Int) : Void {
  145. socket_listen(__s, connections);
  146. }
  147. public function shutdown( read : Bool, write : Bool ) : Void {
  148. socket_shutdown(__s,read,write);
  149. }
  150. public function bind(host : Host, port : Int) : Void {
  151. socket_bind(__s, host.ip, port);
  152. }
  153. public function accept() : Socket {
  154. var c = 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 = socket_peer(__s);
  163. var h = new Host("127.0.0.1");
  164. untyped h.ip = a[0];
  165. return { host : h, port : a[1] };
  166. }
  167. public function host() : { host : Host, port : Int } {
  168. var a : Dynamic = socket_host(__s);
  169. var h = new Host("127.0.0.1");
  170. untyped h.ip = a[0];
  171. return { host : h, port : a[1] };
  172. }
  173. public function setTimeout( timeout : Float ) : Void {
  174. socket_set_timeout(__s, timeout);
  175. }
  176. public function waitForRead() : Void {
  177. select([this],null,null,null);
  178. }
  179. public function setBlocking( b : Bool ) : Void {
  180. socket_set_blocking(__s,b);
  181. }
  182. public function setFastSend( b : Bool ) : Void {
  183. throw "Not implemented";
  184. }
  185. public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float ) : {read: Array<Socket>,write: Array<Socket>,others: Array<Socket>} {
  186. var neko_array = socket_select(read,write,others, timeout);
  187. if (neko_array==null)
  188. throw "Select error";
  189. return {
  190. read: neko_array[0],
  191. write: neko_array[1],
  192. others: neko_array[2]
  193. };
  194. }
  195. private static var socket_new = cpp.Lib.load("std","socket_new",1);
  196. private static var socket_close = cpp.Lib.load("std","socket_close",1);
  197. private static var socket_write = cpp.Lib.load("std","socket_write",2);
  198. private static var socket_read = cpp.Lib.load("std","socket_read",1);
  199. private static var socket_connect = cpp.Lib.load("std","socket_connect",3);
  200. private static var socket_listen = cpp.Lib.load("std","socket_listen",2);
  201. private static var socket_select = cpp.Lib.load("std","socket_select",4);
  202. private static var socket_bind = cpp.Lib.load("std","socket_bind",3);
  203. private static var socket_accept = cpp.Lib.load("std","socket_accept",1);
  204. private static var socket_peer = cpp.Lib.load("std","socket_peer",1);
  205. private static var socket_host = cpp.Lib.load("std","socket_host",1);
  206. private static var socket_set_timeout = cpp.Lib.load("std","socket_set_timeout",2);
  207. private static var socket_shutdown = cpp.Lib.load("std","socket_shutdown",3);
  208. private static var socket_set_blocking = cpp.Lib.load("std","socket_set_blocking",2);
  209. }