Socket.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. extern private class NativeSocket {
  25. function new():Void;
  26. function accept():NativeSocket;
  27. function bind(host:Int, port:Int):Void;
  28. function close():Void;
  29. function connect(host:Int, port:Int):Void;
  30. function host():{ip:Int, port:Int};
  31. function listen(connections:Int):Void;
  32. function peer():{ip:Int, port:Int};
  33. function receive(buf:haxe.io.Bytes, pos:Int, len:Int):Int;
  34. function receiveChar():Int;
  35. function send(buf:haxe.io.Bytes, pos:Int, len:Int):Int;
  36. function sendChar(char:Int):Void;
  37. function setFastSend(b:Bool):Void;
  38. function setTimeout(timeout:Float):Void;
  39. function shutdown(read:Bool, write:Bool):Void;
  40. public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>, ?timeout:Float):{ read: Array<Socket>,write: Array<Socket>,others: Array<Socket> };
  41. }
  42. private class SocketOutput extends haxe.io.Output {
  43. var socket:NativeSocket;
  44. public function new(socket:NativeSocket) {
  45. this.socket = socket;
  46. }
  47. public override function writeByte(c:Int) {
  48. try {
  49. socket.sendChar(c);
  50. } catch( e : Dynamic ) {
  51. if( e == "Blocking" )
  52. throw Blocked;
  53. else if ( e == "EOF" )
  54. throw new haxe.io.Eof();
  55. else
  56. throw Custom(e);
  57. }
  58. }
  59. public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int) {
  60. return try {
  61. socket.send(buf, pos, len);
  62. } catch( e : Dynamic ) {
  63. if( e == "Blocking" )
  64. throw Blocked;
  65. else
  66. throw Custom(e);
  67. }
  68. }
  69. public override function close() {
  70. super.close();
  71. socket.close();
  72. }
  73. }
  74. private class SocketInput extends haxe.io.Input {
  75. var socket:NativeSocket;
  76. public function new(socket:NativeSocket) {
  77. this.socket = socket;
  78. }
  79. public override function readByte() {
  80. return try {
  81. socket.receiveChar();
  82. } catch( e : Dynamic ) {
  83. if( e == "Blocking" )
  84. throw Blocked;
  85. else
  86. throw new haxe.io.Eof();
  87. }
  88. }
  89. public override function readBytes(buf:haxe.io.Bytes, pos:Int, len:Int) {
  90. var r;
  91. try {
  92. r = socket.receive(buf, pos, len);
  93. } catch( e : Dynamic ) {
  94. if( e == "Blocking" )
  95. throw Blocked;
  96. else
  97. throw Custom(e);
  98. }
  99. if( r == 0 )
  100. throw new haxe.io.Eof();
  101. return r;
  102. }
  103. public override function close() {
  104. super.close();
  105. socket.close();
  106. }
  107. }
  108. @:coreApi
  109. class Socket {
  110. public var input(default,null):haxe.io.Input;
  111. public var output(default,null):haxe.io.Output;
  112. public var custom:Dynamic;
  113. @:ifFeature("sys.net.Socket.select") var socket:NativeSocket;
  114. public function new() {
  115. init(new NativeSocket());
  116. }
  117. private function init(socket:NativeSocket):Void {
  118. this.socket = socket;
  119. input = new SocketInput(socket);
  120. output = new SocketOutput(socket);
  121. }
  122. public function close():Void {
  123. socket.close();
  124. }
  125. public function read():String {
  126. return input.readAll().toString();
  127. }
  128. public function write(content:String):Void {
  129. output.writeString(content);
  130. }
  131. public function connect(host:Host, port:Int):Void {
  132. socket.connect(host.ip, port);
  133. }
  134. public function listen(connections:Int):Void {
  135. socket.listen(connections);
  136. }
  137. public function shutdown(read:Bool, write:Bool):Void {
  138. socket.shutdown(read, write);
  139. }
  140. public function bind(host:Host, port:Int):Void {
  141. socket.bind(host.ip, port);
  142. }
  143. public function accept():Socket {
  144. var nativeSocket = socket.accept();
  145. var socket:Socket = Type.createEmptyInstance(Socket);
  146. socket.init(nativeSocket);
  147. return socket;
  148. }
  149. @:access(sys.net.Host.init)
  150. public function peer():{host:Host, port:Int} {
  151. var info = socket.peer();
  152. var host:Host = Type.createEmptyInstance(Host);
  153. host.init(info.ip);
  154. return { host: host, port: info.port };
  155. }
  156. @:access(sys.net.Host.init)
  157. public function host():{host:Host, port:Int} {
  158. var info = socket.host();
  159. var host:Host = Type.createEmptyInstance(Host);
  160. host.init(info.ip);
  161. return { host: host, port: info.port };
  162. }
  163. public function setTimeout(timeout:Float):Void {
  164. socket.setTimeout(timeout);
  165. }
  166. public function waitForRead():Void {
  167. select([this], null, null, -1);
  168. }
  169. public function setBlocking(b:Bool):Void { } // TODO: Don't know how to implement this...
  170. public function setFastSend(b:Bool):Void {
  171. socket.setFastSend(b);
  172. }
  173. public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>, ?timeout:Float):{ read: Array<Socket>,write: Array<Socket>,others: Array<Socket> } {
  174. return NativeSocket.select(read, write, others, timeout);
  175. }
  176. }