Server.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, LoopInterface $loop = null, array $context = array())
  49. {
  50. $loop = $loop ?: Loop::get();
  51. // sanitize TCP context options if not properly wrapped
  52. if ($context && (!isset($context['tcp']) && !isset($context['tls']) && !isset($context['unix']))) {
  53. $context = array('tcp' => $context);
  54. }
  55. // apply default options if not explicitly given
  56. $context += array(
  57. 'tcp' => array(),
  58. 'tls' => array(),
  59. 'unix' => array()
  60. );
  61. $scheme = 'tcp';
  62. $pos = \strpos($uri, '://');
  63. if ($pos !== false) {
  64. $scheme = \substr($uri, 0, $pos);
  65. }
  66. if ($scheme === 'unix') {
  67. $server = new UnixServer($uri, $loop, $context['unix']);
  68. } else {
  69. $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);
  70. if ($scheme === 'tls') {
  71. $server = new SecureServer($server, $loop, $context['tls']);
  72. }
  73. }
  74. $this->server = $server;
  75. $that = $this;
  76. $server->on('connection', function (ConnectionInterface $conn) use ($that) {
  77. $that->emit('connection', array($conn));
  78. });
  79. $server->on('error', function (Exception $error) use ($that) {
  80. $that->emit('error', array($error));
  81. });
  82. }
  83. public function getAddress()
  84. {
  85. return $this->server->getAddress();
  86. }
  87. public function pause()
  88. {
  89. $this->server->pause();
  90. }
  91. public function resume()
  92. {
  93. $this->server->resume();
  94. }
  95. public function close()
  96. {
  97. $this->server->close();
  98. }
  99. }