ConnectionInterface.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace React\Socket;
  3. use React\Stream\DuplexStreamInterface;
  4. /**
  5. * Any incoming and outgoing connection is represented by this interface,
  6. * such as a normal TCP/IP connection.
  7. *
  8. * An incoming or outgoing connection is a duplex stream (both readable and
  9. * writable) that implements React's
  10. * [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
  11. * It contains additional properties for the local and remote address (client IP)
  12. * where this connection has been established to/from.
  13. *
  14. * Most commonly, instances implementing this `ConnectionInterface` are emitted
  15. * by all classes implementing the [`ServerInterface`](#serverinterface) and
  16. * used by all classes implementing the [`ConnectorInterface`](#connectorinterface).
  17. *
  18. * Because the `ConnectionInterface` implements the underlying
  19. * [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface)
  20. * you can use any of its events and methods as usual:
  21. *
  22. * ```php
  23. * $connection->on('data', function ($chunk) {
  24. * echo $chunk;
  25. * });
  26. *
  27. * $connection->on('end', function () {
  28. * echo 'ended';
  29. * });
  30. *
  31. * $connection->on('error', function (Exception $e) {
  32. * echo 'error: ' . $e->getMessage();
  33. * });
  34. *
  35. * $connection->on('close', function () {
  36. * echo 'closed';
  37. * });
  38. *
  39. * $connection->write($data);
  40. * $connection->end($data = null);
  41. * $connection->close();
  42. * // …
  43. * ```
  44. *
  45. * For more details, see the
  46. * [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
  47. *
  48. * @see DuplexStreamInterface
  49. * @see ServerInterface
  50. * @see ConnectorInterface
  51. */
  52. interface ConnectionInterface extends DuplexStreamInterface
  53. {
  54. /**
  55. * Returns the full remote address (URI) where this connection has been established with
  56. *
  57. * ```php
  58. * $address = $connection->getRemoteAddress();
  59. * echo 'Connection with ' . $address . PHP_EOL;
  60. * ```
  61. *
  62. * If the remote address can not be determined or is unknown at this time (such as
  63. * after the connection has been closed), it MAY return a `NULL` value instead.
  64. *
  65. * Otherwise, it will return the full address (URI) as a string value, such
  66. * as `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`,
  67. * `unix://example.sock` or `unix:///path/to/example.sock`.
  68. * Note that individual URI components are application specific and depend
  69. * on the underlying transport protocol.
  70. *
  71. * If this is a TCP/IP based connection and you only want the remote IP, you may
  72. * use something like this:
  73. *
  74. * ```php
  75. * $address = $connection->getRemoteAddress();
  76. * $ip = trim(parse_url($address, PHP_URL_HOST), '[]');
  77. * echo 'Connection with ' . $ip . PHP_EOL;
  78. * ```
  79. *
  80. * @return ?string remote address (URI) or null if unknown
  81. */
  82. public function getRemoteAddress();
  83. /**
  84. * Returns the full local address (full URI with scheme, IP and port) where this connection has been established with
  85. *
  86. * ```php
  87. * $address = $connection->getLocalAddress();
  88. * echo 'Connection with ' . $address . PHP_EOL;
  89. * ```
  90. *
  91. * If the local address can not be determined or is unknown at this time (such as
  92. * after the connection has been closed), it MAY return a `NULL` value instead.
  93. *
  94. * Otherwise, it will return the full address (URI) as a string value, such
  95. * as `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`,
  96. * `unix://example.sock` or `unix:///path/to/example.sock`.
  97. * Note that individual URI components are application specific and depend
  98. * on the underlying transport protocol.
  99. *
  100. * This method complements the [`getRemoteAddress()`](#getremoteaddress) method,
  101. * so they should not be confused.
  102. *
  103. * If your `TcpServer` instance is listening on multiple interfaces (e.g. using
  104. * the address `0.0.0.0`), you can use this method to find out which interface
  105. * actually accepted this connection (such as a public or local interface).
  106. *
  107. * If your system has multiple interfaces (e.g. a WAN and a LAN interface),
  108. * you can use this method to find out which interface was actually
  109. * used for this connection.
  110. *
  111. * @return ?string local address (URI) or null if unknown
  112. * @see self::getRemoteAddress()
  113. */
  114. public function getLocalAddress();
  115. }