Socket.hx 5.4 KB

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