Skip to content

Commit 2defb1b

Browse files
committed
Try to make Server connector a bit more nice
1 parent a265ee9 commit 2defb1b

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/Server.php

Lines changed: 19 additions & 14 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -23,32 +23,37 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
23
'unix' => array(),
23
'unix' => array(),
24
);
24
);
25

25

26+
$server = $this->getServer($uri, $loop, $context);
27+
$this->server = $server;
28+
29+
$that = $this;
30+
$server->on('connection', function (ConnectionInterface $conn) use ($that) {
31+
$that->emit('connection', array($conn));
32+
});
33+
$server->on('error', function (\Exception $error) use ($that) {
34+
$that->emit('error', array($error));
35+
});
36+
}
37+
38+
private function getServer($uri, $loop, $context) {
26
$scheme = 'tcp';
39
$scheme = 'tcp';
27
$pos = strpos($uri, '://');
40
$pos = strpos($uri, '://');
28
if ($pos !== false) {
41
if ($pos !== false) {
29
$scheme = substr($uri, 0, $pos);
42
$scheme = substr($uri, 0, $pos);
30
}
43
}
31

44

32
if ($scheme === 'unix') {
45
if ($scheme === 'unix') {
33-
$server = new UnixServer($uri, $loop, $context['unix']);
46+
return new UnixServer($uri, $loop, $context['unix']);
34
}
47
}
35-
else {
36-
$server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);
37

48

38-
if ($scheme === 'tls') {
49+
$server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);
39-
$server = new SecureServer($server, $loop, $context['tls']);
50+
40-
}
51+
if ($scheme === 'tls') {
52+
$server = new SecureServer($server, $loop, $context['tls']);
41
}
53
}
42

54

43-
$this->server = $server;
55+
return $server;
44

56

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
}
57
}
53

58

54
public function getAddress()
59
public function getAddress()

0 commit comments

Comments
 (0)