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