ConnectorInterface.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace React\Socket;
  3. /**
  4. * The `ConnectorInterface` is responsible for providing an interface for
  5. * establishing streaming connections, such as a normal TCP/IP connection.
  6. *
  7. * This is the main interface defined in this package and it is used throughout
  8. * React's vast ecosystem.
  9. *
  10. * Most higher-level components (such as HTTP, database or other networking
  11. * service clients) accept an instance implementing this interface to create their
  12. * TCP/IP connection to the underlying networking service.
  13. * This is usually done via dependency injection, so it's fairly simple to actually
  14. * swap this implementation against any other implementation of this interface.
  15. *
  16. * The interface only offers a single `connect()` method.
  17. *
  18. * @see ConnectionInterface
  19. */
  20. interface ConnectorInterface
  21. {
  22. /**
  23. * Creates a streaming connection to the given remote address
  24. *
  25. * If returns a Promise which either fulfills with a stream implementing
  26. * `ConnectionInterface` on success or rejects with an `Exception` if the
  27. * connection is not successful.
  28. *
  29. * ```php
  30. * $connector->connect('google.com:443')->then(
  31. * function (React\Socket\ConnectionInterface $connection) {
  32. * // connection successfully established
  33. * },
  34. * function (Exception $error) {
  35. * // failed to connect due to $error
  36. * }
  37. * );
  38. * ```
  39. *
  40. * The returned Promise MUST be implemented in such a way that it can be
  41. * cancelled when it is still pending. Cancelling a pending promise MUST
  42. * reject its value with an Exception. It SHOULD clean up any underlying
  43. * resources and references as applicable.
  44. *
  45. * ```php
  46. * $promise = $connector->connect($uri);
  47. *
  48. * $promise->cancel();
  49. * ```
  50. *
  51. * @param string $uri
  52. * @return \React\Promise\PromiseInterface<ConnectionInterface>
  53. * Resolves with a `ConnectionInterface` on success or rejects with an `Exception` on error.
  54. * @see ConnectionInterface
  55. */
  56. public function connect($uri);
  57. }