AsyncSocket.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. class AsyncSocket {
  27. /**
  28. Creates a new unconnected socket. Use the current event loop by default.
  29. **/
  30. public function new( ?loop : haxe.EventLoop ) {
  31. throw "Not implemented on this platform";
  32. }
  33. /**
  34. Closes the socket : make sure to properly close all your sockets or you will crash when you run out of file descriptors.
  35. **/
  36. public function close() {
  37. }
  38. /**
  39. Write the whole data to the socket output, asynchronously. The buffer can be reused after this call. Will dispatch `onWrite` when data is written.
  40. **/
  41. public function write( bytes : haxe.io.Bytes, pos : Int = 0, len : Int = -1 ) {
  42. }
  43. /**
  44. Connect to the given server host/port. Will onConnect if connection successful and onDisconnect if connection fails or if a later disconnection occurs.
  45. If `ssl` is set, the socket will use SSL authentification and encryption. Certificate errors can generate an onSSLError callback.
  46. **/
  47. public function connect(host:Host, port:Int, ssl = false) {
  48. }
  49. /**
  50. Allows the socket to listen for incoming questions. The parameter tells how many pending connections we can have until they get refused.
  51. Each new connection will trigger a `onConnected` call. Use `accept()` to retrieve the incoming connection socket.
  52. **/
  53. public function listen(connections:Int) {
  54. }
  55. /**
  56. Bind the socket to the given host/port so it can afterwards listen for connections there.
  57. **/
  58. public function bind(host:Host, port:Int, ?ssl : { hostname : String, cert : sys.ssl.Certificate, key : sys.ssl.Key } ) {
  59. }
  60. /**
  61. Accept a new connected client. This will return a connected socket on which you can read/write some data. See `listen`
  62. **/
  63. public function accept():Null<AsyncSocket> {
  64. return null;
  65. }
  66. /**
  67. 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.
  68. **/
  69. public function setFastSend(b:Bool) {
  70. }
  71. /**
  72. See `connect`
  73. **/
  74. public dynamic function onConnect() {
  75. }
  76. /**
  77. See `connect`
  78. **/
  79. public dynamic function onDisconnect() {
  80. }
  81. /**
  82. This callback is called when some data is received on the socket. The bytes must be fully processed in the callback as the data might be overwritten upon return.
  83. **/
  84. public dynamic function onData( bytes : haxe.io.Bytes, pos : Int, len : Int ) {
  85. }
  86. /**
  87. Dispatched when some data is written. Error is true if the data could not be written succesfully. The default behavior is to silently close the socket when such error occurs.
  88. **/
  89. public dynamic function onWrite( error : Bool ) {
  90. if( error ) {
  91. onDisconnect();
  92. close();
  93. }
  94. }
  95. /**
  96. Dispatched when a SSL error occurs. This can be when connecting to a host which fails to authentificate or when starting a server with invalid cert/key.
  97. Authentification can be adjusted with `sys.ssl.Socket.DEFAULT_VERIFY_CERT`
  98. **/
  99. public dynamic function onSSLError( msg : String ) {
  100. }
  101. /**
  102. Similar to `write` but sends a whole string data.
  103. **/
  104. public function writeString( str : String ) {
  105. var buf = haxe.io.Bytes.ofString(str);
  106. write(buf, 0, buf.length);
  107. }
  108. /**
  109. Start a server on given host/port and callback `onClient` everytime a client connects.
  110. Will also call `onClient(null)` in the rare case where the server connection is lost.
  111. **/
  112. public static function startServer( host, port, ?ssl, onClient ) {
  113. var s = new AsyncSocket();
  114. s.onDisconnect = function() {
  115. onClient(null);
  116. };
  117. s.onConnect = function() {
  118. onClient(s.accept());
  119. };
  120. s.bind(new Host(host), port, ssl);
  121. s.listen(1);
  122. return s;
  123. }
  124. }