2
0

Socket.hx 4.3 KB

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