Socket.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 cs.NativeArray;
  24. import cs.system.collections.ArrayList;
  25. import cs.system.net.IPEndPoint;
  26. import cs.system.net.sockets.AddressFamily;
  27. import cs.system.net.sockets.NetworkStream;
  28. import cs.system.net.sockets.ProtocolType;
  29. import cs.system.net.sockets.SocketFlags;
  30. import cs.system.net.sockets.SocketShutdown;
  31. import cs.system.net.sockets.SocketType;
  32. import cs.system.threading.Thread;
  33. import cs.system.net.sockets.Socket in NativeSocket;
  34. import cs.types.UInt8;
  35. import haxe.io.Bytes;
  36. import haxe.io.Error;
  37. import haxe.io.Input;
  38. import haxe.io.Output;
  39. @:coreApi
  40. class Socket {
  41. private var sock:NativeSocket = null;
  42. public var input(default, null):haxe.io.Input;
  43. public var output(default, null):haxe.io.Output;
  44. public var custom:Dynamic;
  45. /**
  46. Creates a new unconnected socket.
  47. **/
  48. public function new():Void {
  49. init();
  50. }
  51. private function init():Void {
  52. sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  53. sock.Blocking = true;
  54. }
  55. public function close():Void {
  56. sock.Close();
  57. input = null;
  58. output = null;
  59. }
  60. public function read():String {
  61. return input.readAll().toString();
  62. }
  63. public function write(content:String):Void {
  64. output.writeString(content);
  65. }
  66. public function connect(host:Host, port:Int):Void {
  67. sock.Connect(host.ipAddress, port);
  68. if (sock.Connected) {
  69. this.output = new cs.io.NativeOutput(new NetworkStream(sock));
  70. this.input = new cs.io.NativeInput(new NetworkStream(sock));
  71. } else {
  72. throw "Connection failed.";
  73. }
  74. }
  75. public function listen(connections:Int):Void {
  76. sock.Listen(connections);
  77. }
  78. public function shutdown(read:Bool, write:Bool):Void {
  79. if (read && write) {
  80. sock.Shutdown(SocketShutdown.Both);
  81. input = null;
  82. output = null;
  83. } else if (read) {
  84. sock.Shutdown(SocketShutdown.Receive);
  85. input = null;
  86. } else if (write) {
  87. sock.Shutdown(SocketShutdown.Send);
  88. output = null;
  89. }
  90. }
  91. public function bind(host:Host, port:Int):Void {
  92. sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  93. sock.Bind(new IPEndPoint(host.ipAddress, port));
  94. }
  95. public function accept():Socket {
  96. var r = new Socket();
  97. r.sock = sock.Accept();
  98. r.output = new cs.io.NativeOutput(new NetworkStream(r.sock));
  99. r.input = new cs.io.NativeInput(new NetworkStream(r.sock));
  100. return r;
  101. }
  102. public function peer():{host:Host, port:Int} {
  103. var remoteIP = cast(sock.RemoteEndPoint, IPEndPoint);
  104. return {host: new Host(remoteIP.Address.ToString()), port: remoteIP.Port};
  105. }
  106. public function host():{host:Host, port:Int} {
  107. var localIP = cast(sock.LocalEndPoint, IPEndPoint);
  108. return {host: new Host(localIP.Address.ToString()), port: localIP.Port};
  109. }
  110. public function setTimeout(timeout:Float):Void {
  111. sock.ReceiveTimeout = sock.SendTimeout = Math.round(timeout * 1000);
  112. }
  113. public function waitForRead():Void {
  114. var end = Date.now().getTime() + ((sock.ReceiveTimeout <= 0) ? Math.POSITIVE_INFINITY : sock.ReceiveTimeout);
  115. while (sock.Available == 0 && Date.now().getTime() < end) {
  116. Thread.Sleep(5);
  117. }
  118. }
  119. public function setBlocking(b:Bool):Void {
  120. sock.Blocking = b;
  121. }
  122. public function setFastSend(b:Bool):Void {
  123. sock.NoDelay = b;
  124. }
  125. static public function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
  126. ?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
  127. var map:Map<Int, Socket> = new Map();
  128. inline function addSockets(sockets:Array<Socket>) {
  129. if (sockets != null)
  130. for (s in sockets)
  131. map[s.sock.Handle.ToInt32()] = s;
  132. }
  133. inline function getRaw(sockets:Array<Socket>):ArrayList {
  134. var a = new ArrayList(sockets == null ? 0 : sockets.length);
  135. if (sockets != null)
  136. for (s in sockets) {
  137. a.Add(s.sock);
  138. }
  139. return a;
  140. }
  141. inline function getOriginal(result:ArrayList) {
  142. var a:Array<Socket> = [];
  143. for (i in 0...result.Count) {
  144. var s:NativeSocket = result[i];
  145. a.push(map[s.Handle.ToInt32()]);
  146. }
  147. return a;
  148. }
  149. addSockets(read);
  150. addSockets(write);
  151. addSockets(others);
  152. // unwrap Sockets into native sockets
  153. var rawRead:ArrayList = getRaw(read),
  154. rawWrite:ArrayList = getRaw(write),
  155. rawOthers:ArrayList = getRaw(others);
  156. var microsec = timeout == null ? -1 : Std.int(timeout * 1000000);
  157. NativeSocket.Select(rawRead, rawWrite, rawOthers, microsec);
  158. // convert native sockets back to Socket objects
  159. return {
  160. read: getOriginal(rawRead),
  161. write: getOriginal(rawWrite),
  162. others: getOriginal(rawOthers),
  163. }
  164. }
  165. }