1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace AsyncSockets\Socket; |
12
|
|
|
|
13
|
|
|
use AsyncSockets\Exception\ConnectionException; |
14
|
|
|
use AsyncSockets\Frame\FramePickerInterface; |
15
|
|
|
use AsyncSockets\Socket\Io\AbstractIo; |
16
|
|
|
use AsyncSockets\Socket\Io\Context; |
17
|
|
|
use AsyncSockets\Socket\Io\DisconnectedIo; |
18
|
|
|
use AsyncSockets\Socket\Io\IoInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AbstractSocket |
22
|
|
|
*/ |
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() |
82
|
|
|
{ |
83
|
126 |
|
$this->setDisconnectedState(); |
84
|
126 |
|
$this->context = new Context(); |
85
|
126 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set disconnected state for socket |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
126 |
|
private function setDisconnectedState() |
93
|
|
|
{ |
94
|
126 |
|
$this->ioInterface = new DisconnectedIo($this); |
95
|
126 |
|
} |
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) |
143
|
|
|
{ |
144
|
|
|
try { |
145
|
31 |
|
return $this->ioInterface->read($picker, $this->context, $isOutOfBand); |
146
|
10 |
|
} catch (ConnectionException $e) { |
147
|
5 |
|
$this->setDisconnectedState(); |
148
|
5 |
|
throw $e; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** {@inheritdoc} */ |
153
|
10 |
|
public function write($data, $isOutOfBand = false) |
154
|
|
|
{ |
155
|
|
|
try { |
156
|
10 |
|
return $this->ioInterface->write($data, $this->context, $isOutOfBand); |
157
|
10 |
|
} catch (ConnectionException $e) { |
158
|
5 |
|
$this->setDisconnectedState(); |
159
|
5 |
|
throw $e; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** {@inheritdoc} */ |
164
|
46 |
|
public function getStreamResource() |
165
|
|
|
{ |
166
|
46 |
|
return $this->resource; |
167
|
|
|
} |
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() |
209
|
|
|
{ |
210
|
64 |
|
$info = stream_get_meta_data($this->resource); |
211
|
64 |
|
if (!isset($info['stream_type'])) { |
212
|
4 |
|
return self::SOCKET_TYPE_UNKNOWN; |
213
|
|
|
} |
214
|
|
|
|
215
|
60 |
|
$parts = explode('/', $info['stream_type']); |
216
|
|
|
$map = [ |
217
|
60 |
|
'tcp' => self::SOCKET_TYPE_TCP, |
218
|
60 |
|
'udp' => self::SOCKET_TYPE_UDP, |
219
|
60 |
|
'udg' => self::SOCKET_TYPE_UDG, |
220
|
60 |
|
'unix' => self::SOCKET_TYPE_UNIX, |
221
|
60 |
|
]; |
222
|
|
|
|
223
|
60 |
|
$regexp = '#^('. implode('|', array_keys($map)) . ')_socket$#'; |
224
|
60 |
|
foreach ($parts as $part) { |
225
|
60 |
|
if (preg_match($regexp, $part, $pockets)) { |
226
|
45 |
|
return $map[$pockets[1]]; |
227
|
|
|
} |
228
|
15 |
|
} |
229
|
|
|
|
230
|
15 |
|
return self::SOCKET_TYPE_UNKNOWN; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|