2
0

Socket.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 lua.lib.luasocket.Socket as LuaSocket;
  24. import lua.lib.luasocket.socket.*;
  25. import lua.*;
  26. import haxe.io.Bytes;
  27. import haxe.io.Error;
  28. class Socket {
  29. public var input(default, null):haxe.io.Input;
  30. public var output(default, null):haxe.io.Output;
  31. var custom:Dynamic;
  32. var _socket:LuaSocket;
  33. var blocking = false;
  34. var timeout = null;
  35. public function new():Void {}
  36. public function close():Void {
  37. _socket.close();
  38. }
  39. public function read():String {
  40. return input.readAll().toString();
  41. }
  42. public function write(content:String):Void {
  43. output.writeString(content);
  44. }
  45. public function connect(host:Host, port:Int):Void {
  46. var res = LuaSocket.connect(host.host, port);
  47. if (res.message != null)
  48. throw 'Socket Error : ${res.message}';
  49. input = new SocketInput(res.result);
  50. output = new SocketOutput(res.result);
  51. _socket = res.result;
  52. _socket.settimeout(timeout);
  53. }
  54. public function listen(connections:Int):Void {
  55. var res = LuaSocket.tcp();
  56. if (res.message != null)
  57. throw 'Socket Listen Error : ${res.message}';
  58. res.result.listen(connections);
  59. _socket = res.result;
  60. _socket.settimeout(timeout);
  61. }
  62. public function shutdown(read:Bool, write:Bool):Void {
  63. var client:TcpClient = cast _socket;
  64. switch [read, write] {
  65. case [true, true]:
  66. client.shutdown(Both);
  67. case [true, false]:
  68. client.shutdown(Receive);
  69. case [false, true]:
  70. client.shutdown(Send);
  71. default:
  72. null;
  73. }
  74. }
  75. public function bind(host:Host, port:Int):Void {
  76. var res = LuaSocket.bind(host.host, port);
  77. if (res.message != null)
  78. throw 'Socket Bind Error : ${res.message}';
  79. _socket = res.result;
  80. }
  81. public function accept():Socket {
  82. var server:TcpServer = cast _socket;
  83. var res = server.accept();
  84. if (res.message != null)
  85. throw 'Error : ${res.message}';
  86. var sock = new Socket();
  87. sock._socket = res.result;
  88. sock.input = new SocketInput(res.result);
  89. sock.output = new SocketOutput(res.result);
  90. return sock;
  91. }
  92. public function peer():{host:Host, port:Int} {
  93. var client:TcpClient = cast _socket;
  94. var res = client.getpeername();
  95. var host = new Host(res.address);
  96. return {host: host, port: Std.parseInt(res.port)};
  97. }
  98. public function host():{host:Host, port:Int} {
  99. var server:TcpServer = cast _socket;
  100. var res = server.getsockname();
  101. var host = new Host(res.address);
  102. return {host: host, port: Std.parseInt(res.port)};
  103. }
  104. public inline function setTimeout(timeout:Float):Void {
  105. this.timeout = timeout;
  106. if (_socket != null) {
  107. var client:TcpClient = cast _socket;
  108. client.settimeout(timeout);
  109. }
  110. }
  111. public function waitForRead():Void {
  112. select([this], null, null);
  113. }
  114. public function setBlocking(b:Bool):Void {
  115. blocking = b;
  116. }
  117. public function setFastSend(b:Bool):Void {
  118. var client:TcpClient = cast _socket;
  119. client.setoption(TcpNoDelay, true);
  120. }
  121. static public function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
  122. ?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
  123. var read_tbl = read == null ? Table.create() : Table.fromArray([for (r in read) cast r._socket]);
  124. var write_tbl = write == null ? Table.create() : Table.fromArray(([for (r in write) cast r._socket]));
  125. var res = LuaSocket.select(read_tbl, write_tbl, timeout);
  126. var convert_socket = function(x:LuaSocket) {
  127. var sock = new Socket();
  128. sock.input = new SocketInput(cast x);
  129. sock.output = new SocketOutput(cast x);
  130. return sock;
  131. }
  132. var read_arr = res.read == null ? [] : Table.toArray(res.read).map(convert_socket);
  133. var write_arr = res.write == null ? [] : Table.toArray(res.write).map(convert_socket);
  134. return {read: read_arr, write: write_arr, others: []};
  135. }
  136. }