1 | <?php |
||
23 | abstract class AbstractSocket implements SocketInterface |
||
24 | { |
||
25 | /** |
||
26 | * Tcp socket type |
||
27 | */ |
||
28 | const SOCKET_TYPE_TCP = 'tcp'; |
||
29 | |||
30 | /** |
||
31 | * Udp socket type |
||
32 | */ |
||
33 | const SOCKET_TYPE_UDP = 'udp'; |
||
34 | |||
35 | /** |
||
36 | * Unix socket type |
||
37 | */ |
||
38 | const SOCKET_TYPE_UNIX = 'unix'; |
||
39 | |||
40 | /** |
||
41 | * Unix datagram socket type |
||
42 | */ |
||
43 | const SOCKET_TYPE_UDG = 'udg'; |
||
44 | |||
45 | /** |
||
46 | * Unknown type of socket |
||
47 | */ |
||
48 | const SOCKET_TYPE_UNKNOWN = ''; |
||
49 | |||
50 | /** |
||
51 | * This socket resource |
||
52 | * |
||
53 | * @var resource |
||
54 | */ |
||
55 | private $resource; |
||
56 | |||
57 | /** |
||
58 | * I/O interface |
||
59 | * |
||
60 | * @var IoInterface |
||
61 | */ |
||
62 | private $ioInterface; |
||
63 | |||
64 | /** |
||
65 | * Socket address |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $remoteAddress; |
||
70 | |||
71 | /** |
||
72 | * Context for this socket |
||
73 | * |
||
74 | * @var Context |
||
75 | */ |
||
76 | private $context; |
||
77 | |||
78 | /** |
||
79 | * AbstractSocket constructor. |
||
80 | */ |
||
81 | 113 | public function __construct() |
|
86 | |||
87 | /** |
||
88 | * Set disconnected state for socket |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | 113 | private function setDisconnectedState() |
|
96 | |||
97 | /** {@inheritdoc} */ |
||
98 | 69 | public function open($address, $context = null) |
|
128 | |||
129 | /** {@inheritdoc} */ |
||
130 | 6 | public function close() |
|
140 | |||
141 | /** {@inheritdoc} */ |
||
142 | 31 | public function read(FramePickerInterface $picker, $isOutOfBand = false) |
|
151 | |||
152 | /** {@inheritdoc} */ |
||
153 | 10 | public function write($data, $isOutOfBand = false) |
|
162 | |||
163 | /** {@inheritdoc} */ |
||
164 | 40 | public function getStreamResource() |
|
168 | |||
169 | /** |
||
170 | * @inheritDoc |
||
171 | */ |
||
172 | 10 | public function __toString() |
|
182 | |||
183 | /** |
||
184 | * Create certain socket resource |
||
185 | * |
||
186 | * @param string $address Network address to open in form transport://path:port |
||
187 | * @param resource $context Valid stream context created by function stream_context_create or null |
||
188 | * |
||
189 | * @return resource |
||
190 | */ |
||
191 | abstract protected function createSocketResource($address, $context); |
||
192 | |||
193 | /** |
||
194 | * Create I/O interface for socket |
||
195 | * |
||
196 | * @param string $type Type of this socket, one of SOCKET_TYPE_* consts |
||
197 | * @param string $address Address passed to open method |
||
198 | * |
||
199 | * @return IoInterface |
||
200 | */ |
||
201 | abstract protected function createIoInterface($type, $address); |
||
202 | |||
203 | /** |
||
204 | * Get current socket type |
||
205 | * |
||
206 | * @return string One of SOCKET_TYPE_* consts |
||
207 | */ |
||
208 | 58 | private function resolveSocketType() |
|
232 | } |
||
233 |