-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamWrapperClient.php
290 lines (248 loc) · 7.41 KB
/
StreamWrapperClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace Poirot\Stream;
use Poirot\Std\ConfigurableSetter;
use Poirot\Stream\Exception\ConnectionError;
use Poirot\Stream\Interfaces\Context\iContextStream;
use Poirot\Stream\Interfaces\iResourceStream;
use Poirot\Stream\Interfaces\iWrapperStreamClient;
use Poirot\Stream\Interfaces\Resource\iAccessModeToResourceStream;
use Poirot\Stream\Context\ContextStreamSocket;
use Poirot\Stream\Resource\AccessMode;
use Poirot\Stream\Wrapper\RegistryOfWrapperStream;
class StreamWrapperClient
extends ConfigurableSetter
implements iWrapperStreamClient
{
const DEFAULT_ACCESS_MODE = iAccessModeToResourceStream::MODE_RB;
/** @var string */
protected $socketUri;
/** @var iContextStream */
protected $context;
/** @var array */
protected $timeout;
/** @var AccessMode */
protected $accessMode;
/** @var boolean */
protected $noneBlocking;
/**
* Construct
*
* Note: When specifying a numerical IPv6 address (e.g. fe80::1),
* you must enclose the IP in square brackets—for example,
* tcp://[fe80::1]:80
*
* @param string|array $serverAddressOrSetter Socket Uri
* @param iAccessModeToResourceStream|string $accMode iSRAccessMode::MODE_*
* @param iContextStream $context Context Options
*/
function __construct(
$serverAddressOrSetter
, $accMode = null
, $context = null
) {
$setters = array();
if (\Poirot\Std\isStringify($serverAddressOrSetter)) {
## maybe using some stringify like pathuri as input
$setters['server_address'] = (string) $serverAddressOrSetter;
if ($accMode !== null)
$setters['access_mode'] = $accMode;
if ($context !== null)
$setters['context'] = $context;
}
parent::__construct($setters);
}
/**
* Open Socket Connection To Socket Uri
*
* - Initiates a stream or datagram connection to the
* destination specified by socketUri.
* The type of socket created is determined by the
* transport specified using standard URL formatting:
* transport://target
*
* ! For Internet Domain sockets (AF_INET) such as
* TCP and UDP, the target portion of the socketUri
* parameter should consist of a hostname or IP address
* followed by a colon and a port number.
* For Unix domain sockets, the target portion should
* point to the socket file on the filesystem
*
* Note: The stream will by default be opened in blocking mode.
*
* Warning UDP sockets will sometimes appear to have opened without
* an error, even if the remote host is unreachable. The error will
* only become apparent when you read or write data to/from the socket.
* The reason for this is because UDP is a "connectionless" protocol,
* which means that the operating system does not try to establish a
* link for the socket until it actually needs to send or receive data
*
* @throws \Exception On Connection Failed
* @return iResourceStream
*/
function getConnect()
{
$sockUri = $this->getServerAddress();
// knowing transport/wrapper:
$scheme = parse_url($sockUri, PHP_URL_SCHEME);
if (!$scheme)
## /path/to/file.ext
return $this->setServerAddress("file://{$sockUri}")
->getConnect();
if (!RegistryOfWrapperStream::isRegistered($scheme))
throw new \Exception(sprintf(
'Wrapper (%s) not supported.'
, $scheme
));
$resource = $this->_connect_wrapper($sockUri);
return new ResourceStream($resource);
}
// Options:
/**
* Set Socket Uri
*
* Note: When specifying a numerical IPv6 address (e.g. fe80::1),
* you must enclose the IP in square brackets—for example,
* tcp://[fe80::1]:80
*
* @param string $socketUri
*
* @return $this
*/
function setServerAddress($socketUri)
{
$this->socketUri = (string) $socketUri;
return $this;
}
/**
* Get Current Socket Uri That Stream Built With
*
* @return string
*/
function getServerAddress()
{
return $this->socketUri;
}
/**
* Context Options
*
* @param iContextStream|array|resource $context
*
* @throws \InvalidArgumentException
* @return $this
*/
function setContext(iContextStream $context)
{
$this->context = $context;
return $this;
}
/**
* Get Context Options
*
* @return iContextStream
*/
function getContext()
{
if (!$this->context)
$this->setContext(new ContextStreamSocket);
return $this->context;
}
/**
* Set blocking/non-blocking mode on a stream
*
* ! This function works for any stream that supports
* non-blocking mode (currently, regular files and socket streams)
*
* @param bool $flag
*
* @return $this
*/
function setNoneBlocking($flag = true)
{
$this->noneBlocking = (boolean) $flag;
return $this;
}
/**
* Indicate Where Stream Is Built With None-Blocking Mode?
*
* @return boolean
*/
function isNoneBlocking()
{
return $this->noneBlocking;
}
/**
* Set timeout period on a stream
*
* - must store time in float mode
* @see self::getTimeout
*
* @param float|array $seconds In Form Of time.utime
*
* @return $this
*/
function setTimeout($seconds)
{
if (is_array($seconds))
$seconds = implode('.', $seconds);
$this->timeout = $seconds;
return $this;
}
/**
* Get Timeout
*
* @return float
*/
function getTimeout()
{
if (!$this->timeout)
$this->setTimeout(ini_get('default_socket_timeout'));
return $this->timeout;
}
/**
* Open Wrapper R/W Mode
*
* @param iAccessModeToResourceStream|string $mode
*
* @return $this
*/
function setAccessMode($mode)
{
$this->getAccessMode()->fromString((string) $mode);
return $this;
}
/**
* Get Open Access Mode
*
* @return AccessMode
*/
function getAccessMode()
{
if (!$this->accessMode)
$this->accessMode = new AccessMode(self::DEFAULT_ACCESS_MODE);
return $this->accessMode;
}
// ..
/**
* @link http://php.net/manual/en/function.fopen.php
*/
protected function _connect_wrapper($sockUri)
{
$resource = fopen(
$sockUri
, $this->getAccessMode()->toString()
, null
, $this->getContext()->toContext()
);
if (false === $resource)
throw new ConnectionError('Error Connecting to ' . $sockUri);
// set timeout:
$timeOut = explode('.', (string) $this->getTimeout());
(isset($timeOut[1])) ?: $timeOut[1] = null;
@stream_set_timeout($resource, $timeOut[0], $timeOut[1]);
// none blocking mode:
if ($this->isNoneBlocking())
// it will work after connection has made on resource
@stream_set_blocking($resource, 0); // 0 for none-blocking
return $resource;
}
}