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 | 126 | public function __construct() |
|
86 | |||
87 | /** |
||
88 | * Set disconnected state for socket |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | 126 | private function setDisconnectedState() |
|
96 | |||
97 | /** {@inheritdoc} */ |
||
98 | 75 | public function open($address, $context = null) |
|
99 | { |
||
100 | 75 | $this->resource = $this->createSocketResource( |
|
101 | 75 | $address, |
|
102 | 63 | $context ?: stream_context_get_default() |
|
103 | 75 | ); |
|
104 | |||
105 | 69 | if (!is_resource($this->resource)) { |
|
106 | 5 | throw new ConnectionException( |
|
107 | 5 | $this, |
|
108 | 'Can not allocate socket resource.' |
||
109 | 5 | ); |
|
110 | } |
||
111 | |||
112 | 64 | $this->remoteAddress = $address; |
|
113 | |||
114 | // https://bugs.php.net/bug.php?id=51056 |
||
115 | 64 | stream_set_blocking($this->resource, 0); |
|
116 | |||
117 | // https://bugs.php.net/bug.php?id=52602 |
||
118 | 64 | stream_set_timeout($this->resource, 0, 0); |
|
119 | 64 | stream_set_chunk_size($this->resource, AbstractIo::SOCKET_BUFFER_SIZE); |
|
120 | |||
121 | 64 | $this->ioInterface = $this->createIoInterface( |
|
122 | 64 | $this->resolveSocketType(), |
|
123 | $address |
||
124 | 64 | ); |
|
125 | |||
126 | 60 | $this->context->reset(); |
|
127 | 60 | } |
|
128 | |||
129 | /** {@inheritdoc} */ |
||
130 | 12 | public function close() |
|
131 | { |
||
132 | 12 | if ($this->resource) { |
|
133 | 8 | $this->setDisconnectedState(); |
|
134 | 8 | stream_socket_shutdown($this->resource, STREAM_SHUT_RDWR); |
|
135 | 8 | fclose($this->resource); |
|
136 | 8 | $this->resource = null; |
|
137 | 8 | $this->remoteAddress = null; |
|
138 | 8 | } |
|
139 | 12 | } |
|
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 | 46 | public function getStreamResource() |
|
168 | |||
169 | /** |
||
170 | * @inheritDoc |
||
171 | */ |
||
172 | 10 | public function __toString() |
|
173 | { |
||
174 | 10 | return $this->remoteAddress ? |
|
175 | 10 | sprintf( |
|
176 | 5 | '[#%s, %s]', |
|
177 | 5 | preg_replace('/Resource id #(\d+)/i', '$1', (string) $this->resource), |
|
178 | 5 | $this->remoteAddress |
|
179 | 5 | ) : |
|
180 | 10 | '[closed socket]'; |
|
181 | } |
||
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 | 64 | private function resolveSocketType() |
|
232 | } |
||
233 |