| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Socket; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use React\EventLoop\LoopInterface; |
| 7 |
use Exception; |
| 8 |
|
| 9 |
final class Server extends EventEmitter implements ServerInterface |
| 10 |
{ |
| 11 |
private $server; |
| 12 |
|
| 13 |
public function __construct($uri, LoopInterface $loop, array $context = array()) |
| 14 |
{ |
| 15 |
// sanitize TCP context options if not properly wrapped |
| 16 |
if ($context && (!isset($context['tcp']) && !isset($context['tls']) && !isset($context['unix']))) { |
| 17 |
$context = array('tcp' => $context); |
| 18 |
} |
| 19 |
|
| 20 |
// apply default options if not explicitly given |
| 21 |
$context += array( |
| 22 |
'tcp' => array(), |
| 23 |
'tls' => array(), |
| 24 |
'unix' => array() |
| 25 |
); |
| 26 |
|
| 27 |
$scheme = 'tcp'; |
| 28 |
$pos = \strpos($uri, '://'); |
| 29 |
if ($pos !== false) { |
| 30 |
$scheme = \substr($uri, 0, $pos); |
| 31 |
} |
| 32 |
|
| 33 |
if ($scheme === 'unix') { |
| 34 |
$server = new UnixServer($uri, $loop, $context['unix']); |
| 35 |
} else { |
| 36 |
$server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']); |
| 37 |
|
| 38 |
if ($scheme === 'tls') { |
| 39 |
$server = new SecureServer($server, $loop, $context['tls']); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
$this->server = $server; |
| 44 |
|
| 45 |
$that = $this; |
| 46 |
$server->on('connection', function (ConnectionInterface $conn) use ($that) { |
| 47 |
$that->emit('connection', array($conn)); |
| 48 |
}); |
| 49 |
$server->on('error', function (Exception $error) use ($that) { |
| 50 |
$that->emit('error', array($error)); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
public function getAddress() |
| 55 |
{ |
| 56 |
return $this->server->getAddress(); |
| 57 |
} |
| 58 |
|
| 59 |
public function pause() |
| 60 |
{ |
| 61 |
$this->server->pause(); |
| 62 |
} |
| 63 |
|
| 64 |
public function resume() |
| 65 |
{ |
| 66 |
$this->server->resume(); |
| 67 |
} |
| 68 |
|
| 69 |
public function close() |
| 70 |
{ |
| 71 |
$this->server->close(); |
| 72 |
} |
| 73 |
} |