2
0

Socket.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 java.net.InetSocketAddress;
  24. @:coreApi
  25. class Socket {
  26. public var input(default, null):haxe.io.Input;
  27. public var output(default, null):haxe.io.Output;
  28. public var custom:Dynamic;
  29. private var sock:java.net.Socket;
  30. private var server:java.net.ServerSocket;
  31. private var boundAddr:java.net.SocketAddress;
  32. public function new():Void {
  33. create();
  34. }
  35. private function create():Void {
  36. this.sock = new java.net.Socket();
  37. try {
  38. this.server = new java.net.ServerSocket();
  39. } catch (e:Dynamic)
  40. throw e;
  41. }
  42. public function close():Void {
  43. try {
  44. if (sock != null)
  45. this.sock.close();
  46. if (server != null)
  47. this.server.close();
  48. } catch (e:Dynamic)
  49. throw e;
  50. }
  51. public function read():String {
  52. return input.readAll().toString();
  53. }
  54. public function write(content:String):Void {
  55. output.writeString(content);
  56. }
  57. public function connect(host:Host, port:Int):Void {
  58. try {
  59. sock.connect(new InetSocketAddress(host.wrapped, port));
  60. this.output = new java.io.NativeOutput(sock.getOutputStream());
  61. this.input = new java.io.NativeInput(sock.getInputStream());
  62. } catch (e:Dynamic)
  63. throw e;
  64. }
  65. public function listen(connections:Int):Void {
  66. if (boundAddr == null)
  67. throw "You must bind the Socket to an address!";
  68. try
  69. server.bind(boundAddr, connections)
  70. catch (e:Dynamic)
  71. throw e;
  72. }
  73. public function shutdown(read:Bool, write:Bool):Void {
  74. try {
  75. if (read)
  76. sock.shutdownInput();
  77. if (write)
  78. sock.shutdownOutput();
  79. } catch (e:Dynamic)
  80. throw e;
  81. }
  82. public function bind(host:Host, port:Int):Void {
  83. if (boundAddr != null) {
  84. if (server.isBound())
  85. throw "Already bound";
  86. }
  87. this.boundAddr = new java.net.InetSocketAddress(host.wrapped, port);
  88. }
  89. public function accept():Socket {
  90. var ret = try server.accept() catch (e:Dynamic) throw e;
  91. var s = new Socket();
  92. s.sock = ret;
  93. s.output = new java.io.NativeOutput(ret.getOutputStream());
  94. s.input = new java.io.NativeInput(ret.getInputStream());
  95. return s;
  96. }
  97. public function peer():{host:Host, port:Int} {
  98. var rem = sock.getInetAddress();
  99. if (rem == null)
  100. return null;
  101. var host = new Host(null);
  102. host.wrapped = rem;
  103. return {host: host, port: sock.getPort()};
  104. }
  105. public function host():{host:Host, port:Int} {
  106. var local = sock.getLocalAddress();
  107. var host = new Host(null);
  108. host.wrapped = local;
  109. if (boundAddr != null) {
  110. return {host: host, port: server.getLocalPort()};
  111. }
  112. return {host: host, port: sock.getLocalPort()};
  113. }
  114. public function setTimeout(timeout:Float):Void {
  115. try
  116. sock.setSoTimeout(Std.int(timeout * 1000))
  117. catch (e:Dynamic)
  118. throw e;
  119. }
  120. public function waitForRead():Void {
  121. throw "Not implemented";
  122. }
  123. public function setBlocking(b:Bool):Void {
  124. throw "Not implemented";
  125. }
  126. public function setFastSend(b:Bool):Void {
  127. try
  128. sock.setTcpNoDelay(b)
  129. catch (e:Dynamic)
  130. throw e;
  131. }
  132. public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
  133. ?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
  134. throw "Not implemented";
  135. return null;
  136. }
  137. }