Skip to content

Commit

Permalink
Try to make Server connector a bit more nice
Browse files Browse the repository at this point in the history
  • Loading branch information
the-eater committed Oct 11, 2017
1 parent a265ee9 commit 2defb1b
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/Server.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,32 +23,37 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
'unix' => array(), 'unix' => array(),
); );


$server = $this->getServer($uri, $loop, $context);
$this->server = $server;

$that = $this;
$server->on('connection', function (ConnectionInterface $conn) use ($that) {
$that->emit('connection', array($conn));
});
$server->on('error', function (\Exception $error) use ($that) {
$that->emit('error', array($error));
});
}

private function getServer($uri, $loop, $context) {
$scheme = 'tcp'; $scheme = 'tcp';
$pos = strpos($uri, '://'); $pos = strpos($uri, '://');
if ($pos !== false) { if ($pos !== false) {
$scheme = substr($uri, 0, $pos); $scheme = substr($uri, 0, $pos);
} }


if ($scheme === 'unix') { if ($scheme === 'unix') {
$server = new UnixServer($uri, $loop, $context['unix']); return new UnixServer($uri, $loop, $context['unix']);
} }
else {
$server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);


if ($scheme === 'tls') { $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);
$server = new SecureServer($server, $loop, $context['tls']);
} if ($scheme === 'tls') {
$server = new SecureServer($server, $loop, $context['tls']);
} }


$this->server = $server; return $server;


$that = $this;
$server->on('connection', function (ConnectionInterface $conn) use ($that) {
$that->emit('connection', array($conn));
});
$server->on('error', function (\Exception $error) use ($that) {
$that->emit('error', array($error));
});
} }


public function getAddress() public function getAddress()
Expand Down

0 comments on commit 2defb1b

Please sign in to comment.