1 | <?php |
||
21 | abstract class AbstractSocket implements SocketInterface |
||
22 | { |
||
23 | /** |
||
24 | * Tcp socket type |
||
25 | */ |
||
26 | const SOCKET_TYPE_TCP = 'tcp'; |
||
27 | |||
28 | /** |
||
29 | * Udp socket type |
||
30 | */ |
||
31 | const SOCKET_TYPE_UDP = 'udp'; |
||
32 | |||
33 | /** |
||
34 | * Unix socket type |
||
35 | */ |
||
36 | const SOCKET_TYPE_UNIX = 'unix'; |
||
37 | |||
38 | /** |
||
39 | * Unix datagram socket type |
||
40 | */ |
||
41 | const SOCKET_TYPE_UDG = 'udg'; |
||
42 | |||
43 | /** |
||
44 | * Unknown type of socket |
||
45 | */ |
||
46 | const SOCKET_TYPE_UNKNOWN = ''; |
||
47 | |||
48 | /** |
||
49 | * This socket resource |
||
50 | * |
||
51 | * @var resource |
||
52 | */ |
||
53 | private $resource; |
||
54 | |||
55 | /** |
||
56 | * I/O interface |
||
57 | * |
||
58 | * @var IoInterface |
||
59 | */ |
||
60 | private $ioInterface; |
||
61 | |||
62 | /** |
||
63 | * AbstractSocket constructor. |
||
64 | */ |
||
65 | 83 | public function __construct() |
|
69 | |||
70 | /** |
||
71 | * Destructor |
||
72 | */ |
||
73 | 4 | public function __destruct() |
|
77 | |||
78 | /** |
||
79 | * Create certain socket resource |
||
80 | * |
||
81 | * @param string $address Network address to open in form transport://path:port |
||
82 | * @param resource $context Valid stream context created by function stream_context_create or null |
||
83 | * |
||
84 | * @return resource |
||
85 | */ |
||
86 | abstract protected function createSocketResource($address, $context); |
||
87 | |||
88 | /** |
||
89 | * Create I/O interface for socket |
||
90 | * |
||
91 | * @param string $type Type of this socket, one of SOCKET_TYPE_* consts |
||
92 | * @param string $address Address passed to open method |
||
93 | * |
||
94 | * @return IoInterface |
||
95 | */ |
||
96 | abstract protected function createIoInterface($type, $address); |
||
97 | |||
98 | /** {@inheritdoc} */ |
||
99 | 47 | public function open($address, $context = null) |
|
125 | |||
126 | /** {@inheritdoc} */ |
||
127 | 47 | public function close() |
|
136 | |||
137 | /** {@inheritdoc} */ |
||
138 | 22 | public function read(FramePickerInterface $picker) |
|
147 | |||
148 | /** {@inheritdoc} */ |
||
149 | 8 | public function write($data) |
|
158 | |||
159 | /** {@inheritdoc} */ |
||
160 | 31 | public function getStreamResource() |
|
164 | |||
165 | /** |
||
166 | * Get current socket type |
||
167 | * |
||
168 | * @return string One of SOCKET_TYPE_* consts |
||
169 | */ |
||
170 | 43 | private function resolveSocketType() |
|
194 | |||
195 | /** |
||
196 | * Set disconnected state for socket |
||
197 | * |
||
198 | * @return void |
||
199 | */ |
||
200 | 83 | private function setDisconnectedState() |
|
204 | } |
||
205 |