Socket.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /**
  24. A TCP socket class : allow you to both connect to a given server and exchange messages or start your own server and wait for connections.
  25. **/
  26. extern class Socket {
  27. /**
  28. The stream on which you can read available data. By default the stream is blocking until the requested data is available,
  29. use `setBlocking(false)` or `setTimeout` to prevent infinite waiting.
  30. **/
  31. var input(default, null):haxe.io.Input;
  32. /**
  33. The stream on which you can send data. Please note that in case the output buffer you will block while writing the data, use `setBlocking(false)` or `setTimeout` to prevent that.
  34. **/
  35. var output(default, null):haxe.io.Output;
  36. /**
  37. A custom value that can be associated with the socket. Can be used to retrieve your custom infos after a `select`.
  38. ***/
  39. var custom:Dynamic;
  40. /**
  41. Creates a new unconnected socket.
  42. **/
  43. function new():Void;
  44. /**
  45. Closes the socket : make sure to properly close all your sockets or you will crash when you run out of file descriptors.
  46. **/
  47. function close():Void;
  48. /**
  49. Read the whole data available on the socket.
  50. *Note*: this is **not** meant to be used together with `setBlocking(false)`,
  51. as it will always throw `haxe.io.Error.Blocked`. `input` methods should be used directly instead.
  52. **/
  53. function read():String;
  54. /**
  55. Write the whole data to the socket output.
  56. *Note*: this is **not** meant to be used together with `setBlocking(false)`, as
  57. `haxe.io.Error.Blocked` may be thrown mid-write with no indication of how many bytes have been written.
  58. `output.writeBytes()` should be used instead as it returns this information.
  59. **/
  60. function write(content:String):Void;
  61. /**
  62. Connect to the given server host/port. Throw an exception in case we couldn't successfully connect.
  63. **/
  64. function connect(host:Host, port:Int):Void;
  65. /**
  66. Allow the socket to listen for incoming questions. The parameter tells how many pending connections we can have until they get refused. Use `accept()` to accept incoming connections.
  67. **/
  68. function listen(connections:Int):Void;
  69. /**
  70. Shutdown the socket, either for reading or writing.
  71. **/
  72. function shutdown(read:Bool, write:Bool):Void;
  73. /**
  74. Bind the socket to the given host/port so it can afterwards listen for connections there.
  75. **/
  76. function bind(host:Host, port:Int):Void;
  77. /**
  78. Accept a new connected client. This will return a connected socket on which you can read/write some data.
  79. **/
  80. function accept():Socket;
  81. /**
  82. Return the information about the other side of a connected socket.
  83. **/
  84. function peer():{host:Host, port:Int};
  85. /**
  86. Return the information about our side of a connected socket.
  87. **/
  88. function host():{host:Host, port:Int};
  89. /**
  90. Gives a timeout (in seconds) after which blocking socket operations (such as reading and writing) will abort and throw an exception.
  91. **/
  92. function setTimeout(timeout:Float):Void;
  93. /**
  94. Block until some data is available for read on the socket.
  95. **/
  96. function waitForRead():Void;
  97. /**
  98. Change the blocking mode of the socket. A blocking socket is the default behavior. A non-blocking socket will abort blocking operations immediately by throwing a haxe.io.Error.Blocked value.
  99. **/
  100. function setBlocking(b:Bool):Void;
  101. /**
  102. Allows the socket to immediately send the data when written to its output : this will cause less ping but might increase the number of packets / data size, especially when doing a lot of small writes.
  103. **/
  104. function setFastSend(b:Bool):Void;
  105. /**
  106. Wait until one of the sockets group is ready for the given operation:
  107. - `read` contains sockets on which we want to wait for available data to be read,
  108. - `write` contains sockets on which we want to wait until we are allowed to write some data to their output buffers,
  109. - `others` contains sockets on which we want to wait for exceptional conditions.
  110. - `select` will block until one of the condition is met, in which case it will return the sockets for which the condition was true.
  111. In case a `timeout` (in seconds) is specified, select might wait at worst until the timeout expires.
  112. **/
  113. static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
  114. ?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>};
  115. }