1 | <?php |
||
19 | class ServerSocket extends AbstractSocket |
||
20 | { |
||
21 | /** {@inheritdoc} */ |
||
22 | 5 | protected function createSocketResource($address, $context) |
|
23 | { |
||
24 | 5 | $type = $this->getSocketScheme($address); |
|
25 | 5 | $resource = stream_socket_server( |
|
26 | 5 | $address, |
|
27 | 5 | $errno, |
|
28 | 5 | $errstr, |
|
29 | 5 | $this->getServerFlagsByType($type), |
|
30 | $context |
||
31 | 5 | ); |
|
32 | |||
33 | 5 | if ($errno || $resource === false) { |
|
34 | 2 | throw new ConnectionException($this, $errstr, $errno); |
|
35 | } |
||
36 | |||
37 | 3 | return $resource; |
|
38 | } |
||
39 | |||
40 | /** {@inheritdoc} */ |
||
41 | 3 | protected function createIoInterface($type, $address) |
|
42 | { |
||
43 | switch ($type) { |
||
44 | 3 | case self::SOCKET_TYPE_UNIX: |
|
45 | return new StreamedServerIo($this); |
||
46 | 3 | case self::SOCKET_TYPE_TCP: |
|
47 | 2 | return new StreamedServerIo($this); |
|
48 | 1 | case self::SOCKET_TYPE_UDG: |
|
49 | return new DatagramServerIo($this, true); |
||
50 | 1 | case self::SOCKET_TYPE_UDP: |
|
51 | return new DatagramServerIo($this, false); |
||
52 | 1 | default: |
|
53 | 1 | throw new \LogicException("Unsupported socket resource type {$type}"); |
|
54 | 1 | } |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Return socket scheme |
||
59 | * |
||
60 | * @param string $address Address in form scheme://host:port |
||
61 | * |
||
62 | * @return string|null |
||
63 | */ |
||
64 | 5 | private function getSocketScheme($address) |
|
73 | |||
74 | /** |
||
75 | * Return flags for connection |
||
76 | * |
||
77 | * @param string $scheme Socket type being created |
||
78 | * |
||
79 | * @return int |
||
80 | */ |
||
81 | 5 | private function getServerFlagsByType($scheme) |
|
92 | |||
93 | /** |
||
94 | * @inheritDoc |
||
95 | */ |
||
96 | 3 | public function isServer() |
|
100 | |||
101 | /** |
||
102 | * @inheritDoc |
||
103 | */ |
||
104 | 1 | public function isClient() |
|
108 | } |
||
109 |