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
|
|
|
namespace AsyncSockets\Configuration; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Configuration |
14
|
|
|
*/ |
15
|
|
|
class Configuration |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Connection timeout |
19
|
|
|
* |
20
|
|
|
* @var double |
21
|
|
|
*/ |
22
|
|
|
private $connectTimeout; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* I/O operations timeout |
26
|
|
|
* |
27
|
|
|
* @var double |
28
|
|
|
*/ |
29
|
|
|
private $ioTimeout; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Aliases of preferred RequestExecutor engines |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
private $preferredEngines = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Default stream context for sockets |
40
|
|
|
* |
41
|
|
|
* @var StreamContext |
42
|
|
|
*/ |
43
|
|
|
private $streamContext; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Minimum receive speed |
47
|
|
|
* |
48
|
|
|
* @var int|null |
49
|
|
|
*/ |
50
|
|
|
private $minReceiveSpeed; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Minimum receive speed duration |
54
|
|
|
* |
55
|
|
|
* @var int|null |
56
|
|
|
*/ |
57
|
|
|
private $minReceiveSpeedDuration; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Minimum send speed |
61
|
|
|
* |
62
|
|
|
* @var int|null |
63
|
|
|
*/ |
64
|
|
|
private $minSendSpeed; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Minimum send speed duration |
68
|
|
|
* |
69
|
|
|
* @var int|null |
70
|
|
|
*/ |
71
|
|
|
private $minSendSpeedDuration; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Configuration constructor. |
75
|
|
|
* |
76
|
|
|
* @param array $options { |
77
|
|
|
* Array with options |
78
|
|
|
* |
79
|
|
|
* @var double $connectTimeout Connection timeout, in seconds |
80
|
|
|
* @var double $ioTimeout Timeout on I/O operations, in seconds |
81
|
|
|
* @var string[] $preferredEngines Array with aliases of preferred RequestExecutor engines |
82
|
|
|
* @var array|resource|null $streamContext Any valid stream context created by stream_context_create function |
83
|
|
|
* or null or array with options. Will be passed to the socket open method. If array value is used, |
84
|
|
|
* then it should contain two nested keys: "options" and "params", which will be passed to |
85
|
|
|
* stream_context_create parameters respectively. |
86
|
|
|
* @var int $minReceiveSpeed Minimum speed required for receiving transfer in bytes per second |
87
|
|
|
* @var int $minReceiveSpeedDuration Duration of transfer speed is below than minimum after which request |
88
|
|
|
* should be aborted, in seconds |
89
|
|
|
* @var int $minSendSpeed Minimum speed required for sending transfer in bytes per second |
90
|
|
|
* @var int $minSendSpeedDuration Duration of transfer speed is below than minimum after which request |
91
|
|
|
* should be aborted, in seconds |
92
|
|
|
* } |
93
|
|
|
*/ |
94
|
179 |
|
public function __construct(array $options = []) |
95
|
|
|
{ |
96
|
179 |
|
$options += $this->getDefaultOptions(); |
97
|
|
|
|
98
|
179 |
|
$this->connectTimeout = (double) $options[ 'connectTimeout' ]; |
99
|
179 |
|
$this->ioTimeout = (double) $options[ 'ioTimeout' ]; |
100
|
179 |
|
$this->preferredEngines = (array) $options[ 'preferredEngines' ]; |
101
|
|
|
|
102
|
179 |
|
$this->streamContext = new StreamContext($options[ 'streamContext' ]); |
103
|
179 |
|
foreach (['minReceiveSpeed', 'minReceiveSpeedDuration', 'minSendSpeed', 'minSendSpeedDuration'] as $k) { |
104
|
179 |
|
$this->{$k} = $options[$k] !== null ? (int) $options[$k] : null; |
105
|
179 |
|
} |
106
|
179 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return ConnectTimeout |
110
|
|
|
* |
111
|
|
|
* @return float |
112
|
|
|
*/ |
113
|
162 |
|
public function getConnectTimeout() |
114
|
|
|
{ |
115
|
162 |
|
return $this->connectTimeout; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Return IoTimeout |
120
|
|
|
* |
121
|
|
|
* @return float |
122
|
|
|
*/ |
123
|
162 |
|
public function getIoTimeout() |
124
|
|
|
{ |
125
|
162 |
|
return $this->ioTimeout; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return PreferredEngines |
130
|
|
|
* |
131
|
|
|
* @return string[] |
132
|
|
|
*/ |
133
|
5 |
|
public function getPreferredEngines() |
134
|
|
|
{ |
135
|
5 |
|
return $this->preferredEngines; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return default socket stream context |
140
|
|
|
* |
141
|
|
|
* @return resource |
142
|
|
|
*/ |
143
|
160 |
|
public function getStreamContext() |
144
|
|
|
{ |
145
|
160 |
|
return $this->streamContext->getResource(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Return MinReceiveSpeed |
150
|
|
|
* |
151
|
|
|
* @return int|null |
152
|
|
|
*/ |
153
|
162 |
|
public function getMinReceiveSpeed() |
154
|
|
|
{ |
155
|
162 |
|
return $this->minReceiveSpeed; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Return MinReceiveSpeedDuration |
160
|
|
|
* |
161
|
|
|
* @return int|null |
162
|
|
|
*/ |
163
|
162 |
|
public function getMinReceiveSpeedDuration() |
164
|
|
|
{ |
165
|
162 |
|
return $this->minReceiveSpeedDuration; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return MinSendSpeed |
170
|
|
|
* |
171
|
|
|
* @return int|null |
172
|
|
|
*/ |
173
|
162 |
|
public function getMinSendSpeed() |
174
|
|
|
{ |
175
|
162 |
|
return $this->minSendSpeed; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return MinSendSpeedDuration |
180
|
|
|
* |
181
|
|
|
* @return int|null |
182
|
|
|
*/ |
183
|
162 |
|
public function getMinSendSpeedDuration() |
184
|
|
|
{ |
185
|
162 |
|
return $this->minSendSpeedDuration; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Key-value array with default values for options |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
179 |
|
private function getDefaultOptions() |
194
|
|
|
{ |
195
|
179 |
|
$result = []; |
196
|
179 |
|
$socketTimeout = (double) ini_get('default_socket_timeout'); |
197
|
|
|
|
198
|
179 |
|
$result[ 'connectTimeout' ] = $socketTimeout; |
199
|
179 |
|
$result[ 'ioTimeout' ] = $socketTimeout; |
200
|
179 |
|
$result[ 'preferredEngines' ] = [ 'libevent', 'native' ]; |
201
|
179 |
|
$result[ 'streamContext' ] = null; |
202
|
179 |
|
$result[ 'minReceiveSpeed' ] = null; |
203
|
179 |
|
$result[ 'minReceiveSpeedDuration' ] = null; |
204
|
179 |
|
$result[ 'minSendSpeed' ] = null; |
205
|
179 |
|
$result[ 'minSendSpeedDuration' ] = null; |
206
|
|
|
|
207
|
179 |
|
return $result; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|