Server.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace React\Socket;
  3. use Evenement\EventEmitter;
  4. use React\EventLoop\Loop;
  5. use React\EventLoop\LoopInterface;
  6. use Exception;
  7. /**
  8. * @deprecated 1.9.0 See `SocketServer` instead
  9. * @see SocketServer
  10. */
  11. final class Server extends EventEmitter implements ServerInterface
  12. {
  13. private $server;
  14. /**
  15. * [Deprecated] `Server`
  16. *
  17. * This class exists for BC reasons only and should not be used anymore.
  18. *
  19. * ```php
  20. * // deprecated
  21. * $socket = new React\Socket\Server(0);
  22. * $socket = new React\Socket\Server('127.0.0.1:8000');
  23. * $socket = new React\Socket\Server('127.0.0.1:8000', null, $context);
  24. * $socket = new React\Socket\Server('127.0.0.1:8000', $loop, $context);
  25. *
  26. * // new
  27. * $socket = new React\Socket\SocketServer('127.0.0.1:0');
  28. * $socket = new React\Socket\SocketServer('127.0.0.1:8000');
  29. * $socket = new React\Socket\SocketServer('127.0.0.1:8000', $context);
  30. * $socket = new React\Socket\SocketServer('127.0.0.1:8000', $context, $loop);
  31. * ```
  32. *
  33. * This class takes an optional `LoopInterface|null $loop` parameter that can be used to
  34. * pass the event loop instance to use for this object. You can use a `null` value
  35. * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
  36. * This value SHOULD NOT be given unless you're sure you want to explicitly use a
  37. * given event loop instance.
  38. *
  39. * For BC reasons, you can also pass the TCP socket context options as a simple
  40. * array without wrapping this in another array under the `tcp` key.
  41. *
  42. * @param string|int $uri
  43. * @param ?LoopInterface $loop
  44. * @param array $context
  45. * @deprecated 1.9.0 See `SocketServer` instead
  46. * @see SocketServer
  47. */
  48. public function __construct($uri, $loop = null, array $context = array())
  49. {
  50. if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
  51. throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
  52. }
  53. $loop = $loop ?: Loop::get();
  54. // sanitize TCP context options if not properly wrapped
  55. if ($context && (!isset($context['tcp']) && !isset($context['tls']) && !isset($context['unix']))) {
  56. $context = array('tcp' => $context);
  57. }
  58. // apply default options if not explicitly given
  59. $context += array(
  60. 'tcp' => array(),
  61. 'tls' => array(),
  62. 'unix' => array()
  63. );
  64. $scheme = 'tcp';
  65. $pos = \strpos($uri, '://');
  66. if ($pos !== false) {
  67. $scheme = \substr($uri, 0, $pos);
  68. }
  69. if ($scheme === 'unix') {
  70. $server = new UnixServer($uri, $loop, $context['unix']);
  71. } else {
  72. $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);
  73. if ($scheme === 'tls') {
  74. $server = new SecureServer($server, $loop, $context['tls']);
  75. }
  76. }
  77. $this->server = $server;
  78. $that = $this;
  79. $server->on('connection', function (ConnectionInterface $conn) use ($that) {
  80. $that->emit('connection', array($conn));
  81. });
  82. $server->on('error', function (Exception $error) use ($that) {
  83. $that->emit('error', array($error));
  84. });
  85. }
  86. public function getAddress()
  87. {
  88. return $this->server->getAddress();
  89. }
  90. public function pause()
  91. {
  92. $this->server->pause();
  93. }
  94. public function resume()
  95. {
  96. $this->server->resume();
  97. }
  98. public function close()
  99. {
  100. $this->server->close();
  101. }
  102. }