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