Socket.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2005-2012, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. *
  25. */
  26. package sys.net;
  27. /**
  28. 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.
  29. **/
  30. extern class Socket {
  31. /**
  32. The stream on which you can read available data. By default the stream is blocking until the requested data is available,
  33. use [setBlocking(false)] or [setTimeout] to prevent infinite waiting.
  34. **/
  35. var input(default,null) : haxe.io.Input;
  36. /**
  37. 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.
  38. **/
  39. var output(default,null) : haxe.io.Output;
  40. /**
  41. A custom value that can be associated with the socket. Can be used to retreive your custom infos after a [select].
  42. ***/
  43. var custom : Dynamic;
  44. /**
  45. Creates a new unconnected socket.
  46. **/
  47. function new() : Void;
  48. /**
  49. Closes the socket : make sure to properly close all your sockets or you will crash when you run out of file descriptors.
  50. **/
  51. function close() : Void;
  52. /**
  53. Read the whole data available on the socket.
  54. **/
  55. function read() : String;
  56. /**
  57. Write the whole data to the socket output.
  58. **/
  59. function write( content : String ) : Void;
  60. /**
  61. Connect to the given server host/port. Throw an exception in case we couldn't sucessfully connect.
  62. **/
  63. function connect( host : Host, port : Int ) : Void;
  64. /**
  65. 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.
  66. **/
  67. function listen( connections : Int ) : Void;
  68. /**
  69. Shutdown the socket, either for reading or writing.
  70. **/
  71. function shutdown( read : Bool, write : Bool ) : Void;
  72. /**
  73. Bind the socket to the given host/port so it can afterwards listen for connections there.
  74. **/
  75. function bind( host : Host, port : Int ) : Void;
  76. /**
  77. Accept a new connected client. This will return a connected socket on which you can read/write some data.
  78. **/
  79. function accept() : Socket;
  80. /**
  81. Return the informations about the other side of a connected socket.
  82. **/
  83. function peer() : { host : Host, port : Int };
  84. /**
  85. Return the informations about our side of a connected socket.
  86. **/
  87. function host() : { host : Host, port : Int };
  88. /**
  89. Gives a timeout after which blocking socket operations (such as reading and writing) will abort and throw an exception.
  90. **/
  91. function setTimeout( timeout : Float ) : Void;
  92. /**
  93. Block until some data is available for read on the socket.
  94. **/
  95. function waitForRead() : Void;
  96. /**
  97. Change the blocking mode of the socket. A blocking socket is the default behavior. A non-blocking socket will abort blocking operations immediatly by throwing a haxe.io.Error.Blocking value.
  98. **/
  99. function setBlocking( b : Bool ) : Void;
  100. /**
  101. Allows the socket to immediatly 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.
  102. **/
  103. function setFastSend( b : Bool ) : Void;
  104. /**
  105. Wait until one of the sockets groups is ready for the given operation :
  106. [read] contains sockets on which we want to wait for available data to be read,
  107. [write] contains sockets on which we want to wait until we are allowed to write some data to their output buffers,
  108. [others] contains sockets on which we want to wait for exceptional conditions.
  109. [select] will block until one of the condition is met, in which case it will return the sockets for which the condition was true.
  110. In case a [timeout] (in seconds) is specified, select might wait at worse until the timeout expires.
  111. **/
  112. static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> };
  113. }