Socket.hx 4.6 KB

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