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 | 108 | public function __construct() |
|
81 | { |
||
82 | 108 | $this->setDisconnectedState(); |
|
83 | 108 | $this->context = new Context(); |
|
84 | 108 | } |
|
85 | |||
86 | /** |
||
87 | * Create certain socket resource |
||
88 | * |
||
89 | * @param string $address Network address to open in form transport://path:port |
||
90 | * @param resource $context Valid stream context created by function stream_context_create or null |
||
91 | * |
||
92 | * @return resource |
||
93 | */ |
||
94 | abstract protected function createSocketResource($address, $context); |
||
95 | |||
96 | /** |
||
97 | * Create I/O interface for socket |
||
98 | * |
||
99 | * @param string $type Type of this socket, one of SOCKET_TYPE_* consts |
||
100 | * @param string $address Address passed to open method |
||
101 | * |
||
102 | * @return IoInterface |
||
103 | */ |
||
104 | abstract protected function createIoInterface($type, $address); |
||
105 | |||
106 | /** {@inheritdoc} */ |
||
107 | 65 | public function open($address, $context = null) |
|
135 | |||
136 | /** {@inheritdoc} */ |
||
137 | 12 | public function close() |
|
138 | { |
||
139 | 12 | if ($this->resource) { |
|
140 | 8 | $this->setDisconnectedState(); |
|
141 | 8 | stream_socket_shutdown($this->resource, STREAM_SHUT_RDWR); |
|
142 | 8 | fclose($this->resource); |
|
143 | 8 | $this->resource = null; |
|
144 | 8 | $this->remoteAddress = null; |
|
145 | 8 | } |
|
146 | 12 | } |
|
147 | |||
148 | /** {@inheritdoc} */ |
||
149 | 31 | public function read(FramePickerInterface $picker, $isOutOfBand = false) |
|
158 | |||
159 | /** {@inheritdoc} */ |
||
160 | 10 | public function write($data, $isOutOfBand = false) |
|
169 | |||
170 | /** {@inheritdoc} */ |
||
171 | 41 | public function getStreamResource() |
|
175 | |||
176 | /** |
||
177 | * Get current socket type |
||
178 | * |
||
179 | * @return string One of SOCKET_TYPE_* consts |
||
180 | */ |
||
181 | 59 | private function resolveSocketType() |
|
205 | |||
206 | /** |
||
207 | * Set disconnected state for socket |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | 108 | private function setDisconnectedState() |
|
215 | |||
216 | /** |
||
217 | * @inheritDoc |
||
218 | */ |
||
219 | public function __toString() |
||
223 | } |
||
224 |