ServerInterface.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace React\Socket;
  3. use Evenement\EventEmitterInterface;
  4. /**
  5. * The `ServerInterface` is responsible for providing an interface for accepting
  6. * incoming streaming connections, such as a normal TCP/IP connection.
  7. *
  8. * Most higher-level components (such as a HTTP server) accept an instance
  9. * implementing this interface to accept incoming streaming connections.
  10. * This is usually done via dependency injection, so it's fairly simple to actually
  11. * swap this implementation against any other implementation of this interface.
  12. * This means that you SHOULD typehint against this interface instead of a concrete
  13. * implementation of this interface.
  14. *
  15. * Besides defining a few methods, this interface also implements the
  16. * `EventEmitterInterface` which allows you to react to certain events:
  17. *
  18. * connection event:
  19. * The `connection` event will be emitted whenever a new connection has been
  20. * established, i.e. a new client connects to this server socket:
  21. *
  22. * ```php
  23. * $socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
  24. * echo 'new connection' . PHP_EOL;
  25. * });
  26. * ```
  27. *
  28. * See also the `ConnectionInterface` for more details about handling the
  29. * incoming connection.
  30. *
  31. * error event:
  32. * The `error` event will be emitted whenever there's an error accepting a new
  33. * connection from a client.
  34. *
  35. * ```php
  36. * $socket->on('error', function (Exception $e) {
  37. * echo 'error: ' . $e->getMessage() . PHP_EOL;
  38. * });
  39. * ```
  40. *
  41. * Note that this is not a fatal error event, i.e. the server keeps listening for
  42. * new connections even after this event.
  43. *
  44. * @see ConnectionInterface
  45. */
  46. interface ServerInterface extends EventEmitterInterface
  47. {
  48. /**
  49. * Returns the full address (URI) this server is currently listening on
  50. *
  51. * ```php
  52. * $address = $socket->getAddress();
  53. * echo 'Server listening on ' . $address . PHP_EOL;
  54. * ```
  55. *
  56. * If the address can not be determined or is unknown at this time (such as
  57. * after the socket has been closed), it MAY return a `NULL` value instead.
  58. *
  59. * Otherwise, it will return the full address (URI) as a string value, such
  60. * as `tcp://127.0.0.1:8080`, `tcp://[::1]:80` or `tls://127.0.0.1:443`.
  61. * Note that individual URI components are application specific and depend
  62. * on the underlying transport protocol.
  63. *
  64. * If this is a TCP/IP based server and you only want the local port, you may
  65. * use something like this:
  66. *
  67. * ```php
  68. * $address = $socket->getAddress();
  69. * $port = parse_url($address, PHP_URL_PORT);
  70. * echo 'Server listening on port ' . $port . PHP_EOL;
  71. * ```
  72. *
  73. * @return ?string the full listening address (URI) or NULL if it is unknown (not applicable to this server socket or already closed)
  74. */
  75. public function getAddress();
  76. /**
  77. * Pauses accepting new incoming connections.
  78. *
  79. * Removes the socket resource from the EventLoop and thus stop accepting
  80. * new connections. Note that the listening socket stays active and is not
  81. * closed.
  82. *
  83. * This means that new incoming connections will stay pending in the
  84. * operating system backlog until its configurable backlog is filled.
  85. * Once the backlog is filled, the operating system may reject further
  86. * incoming connections until the backlog is drained again by resuming
  87. * to accept new connections.
  88. *
  89. * Once the server is paused, no futher `connection` events SHOULD
  90. * be emitted.
  91. *
  92. * ```php
  93. * $socket->pause();
  94. *
  95. * $socket->on('connection', assertShouldNeverCalled());
  96. * ```
  97. *
  98. * This method is advisory-only, though generally not recommended, the
  99. * server MAY continue emitting `connection` events.
  100. *
  101. * Unless otherwise noted, a successfully opened server SHOULD NOT start
  102. * in paused state.
  103. *
  104. * You can continue processing events by calling `resume()` again.
  105. *
  106. * Note that both methods can be called any number of times, in particular
  107. * calling `pause()` more than once SHOULD NOT have any effect.
  108. * Similarly, calling this after `close()` is a NO-OP.
  109. *
  110. * @see self::resume()
  111. * @return void
  112. */
  113. public function pause();
  114. /**
  115. * Resumes accepting new incoming connections.
  116. *
  117. * Re-attach the socket resource to the EventLoop after a previous `pause()`.
  118. *
  119. * ```php
  120. * $socket->pause();
  121. *
  122. * Loop::addTimer(1.0, function () use ($socket) {
  123. * $socket->resume();
  124. * });
  125. * ```
  126. *
  127. * Note that both methods can be called any number of times, in particular
  128. * calling `resume()` without a prior `pause()` SHOULD NOT have any effect.
  129. * Similarly, calling this after `close()` is a NO-OP.
  130. *
  131. * @see self::pause()
  132. * @return void
  133. */
  134. public function resume();
  135. /**
  136. * Shuts down this listening socket
  137. *
  138. * This will stop listening for new incoming connections on this socket.
  139. *
  140. * Calling this method more than once on the same instance is a NO-OP.
  141. *
  142. * @return void
  143. */
  144. public function close();
  145. }