Socket.hx 7.0 KB

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