1 | <?php |
||
24 | class StreamSocketClient |
||
25 | { |
||
26 | /** |
||
27 | * @deprecated deprecated since v1.4.0 |
||
28 | */ |
||
29 | const SOCKET_TIMEOUT = 30; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $host; |
||
35 | |||
36 | /** |
||
37 | * @var integer |
||
38 | */ |
||
39 | protected $port; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $scheme; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $context; |
||
50 | |||
51 | /** |
||
52 | * @var resource |
||
53 | */ |
||
54 | protected $socket; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $connectTimeout = self::SOCKET_TIMEOUT; |
||
60 | |||
61 | /** |
||
62 | * @param string $scheme |
||
63 | * @param string $host |
||
64 | * @param integer $port |
||
65 | * @param array $context |
||
66 | */ |
||
67 | 41 | public function __construct($scheme, $host, $port, array $context = array()) |
|
74 | |||
75 | /** |
||
76 | * Destructor, closes socket if possible |
||
77 | */ |
||
78 | 41 | public function __destruct() |
|
82 | |||
83 | /** |
||
84 | * Initializes socket-client |
||
85 | * |
||
86 | * @deprecated deprecated since v1.4.0 |
||
87 | * |
||
88 | * @param string $scheme like "udp" or "tcp" |
||
89 | * @param string $host |
||
90 | * @param integer $port |
||
91 | * @param array $context |
||
92 | * |
||
93 | * @return resource |
||
94 | * |
||
95 | * @throws RuntimeException on connection-failure |
||
96 | */ |
||
97 | protected static function initSocket($scheme, $host, $port, array $context) |
||
127 | |||
128 | |||
129 | /** |
||
130 | * Internal function mimicking the behaviour of static::initSocket |
||
131 | * which will get new functionality instead of the deprecated |
||
132 | * "factory" |
||
133 | * |
||
134 | * @return resource |
||
135 | * |
||
136 | * @throws RuntimeException on connection-failure |
||
137 | */ |
||
138 | 14 | private function buildSocket() |
|
139 | { |
||
140 | 14 | $socketDescriptor = sprintf( |
|
141 | 14 | "%s://%s:%d", |
|
142 | 14 | $this->scheme, |
|
143 | 14 | $this->host, |
|
144 | 14 | $this->port |
|
145 | ); |
||
146 | |||
147 | 14 | $socket = @stream_socket_client( |
|
148 | 14 | $socketDescriptor, |
|
149 | 14 | $errNo, |
|
150 | 14 | $errStr, |
|
151 | 14 | $this->connectTimeout, |
|
152 | 14 | \STREAM_CLIENT_CONNECT, |
|
153 | 14 | stream_context_create($this->context) |
|
154 | ); |
||
155 | |||
156 | 14 | if ($socket === false) { |
|
157 | 1 | throw new RuntimeException( |
|
158 | 1 | sprintf( |
|
159 | 1 | "Failed to create socket-client for %s: %s (%s)", |
|
160 | 1 | $socketDescriptor, |
|
161 | 1 | $errStr, |
|
162 | 1 | $errNo |
|
163 | ) |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | // set non-blocking for UDP |
||
168 | 13 | if (strcasecmp("udp", $this->scheme) == 0) { |
|
169 | 3 | stream_set_blocking($socket, 0); |
|
170 | } |
||
171 | |||
172 | 13 | return $socket; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Returns raw-socket-resource |
||
177 | * |
||
178 | * @return resource |
||
179 | */ |
||
180 | 14 | public function getSocket() |
|
181 | { |
||
182 | // lazy initializing of socket-descriptor |
||
183 | 14 | if (!$this->socket) { |
|
184 | 14 | $this->socket = $this->buildSocket(); |
|
185 | } |
||
186 | |||
187 | 13 | return $this->socket; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Writes a given string to the socket and returns the |
||
192 | * number of written bytes |
||
193 | * |
||
194 | * @param string $buffer |
||
195 | * |
||
196 | * @return int |
||
197 | * |
||
198 | * @throws RuntimeException on write-failure |
||
199 | */ |
||
200 | 8 | public function write($buffer) |
|
201 | { |
||
202 | 8 | $buffer = (string) $buffer; |
|
203 | 8 | $bufLen = Binary::safeStrlen($buffer); |
|
204 | |||
205 | 8 | $socket = $this->getSocket(); |
|
206 | 8 | $written = 0; |
|
207 | |||
208 | 8 | while ($written < $bufLen) { |
|
209 | // PHP's fwrite does not behave nice in regards to errors, so we wrap |
||
210 | // it with a temporary error handler and treat every warning/notice as |
||
211 | // a error |
||
212 | 8 | $failed = false; |
|
213 | 8 | $errorMessage = "Failed to write to socket"; |
|
214 | 8 | set_error_handler(function ($errno, $errstr) use (&$failed, &$errorMessage) { |
|
215 | 1 | $failed = true; |
|
216 | 1 | $errorMessage .= ": $errstr ($errno)"; |
|
217 | 8 | }); |
|
218 | 8 | $byteCount = fwrite($socket, Binary::safeSubstr($buffer, $written)); |
|
219 | 8 | restore_error_handler(); |
|
220 | |||
221 | 8 | if ($byteCount === 0 && defined('HHVM_VERSION')) { |
|
222 | $failed = true; |
||
223 | } |
||
224 | |||
225 | 8 | if ($failed || $byteCount === false) { |
|
226 | 1 | throw new \RuntimeException($errorMessage); |
|
227 | } |
||
228 | |||
229 | 8 | $written += $byteCount; |
|
230 | } |
||
231 | |||
232 | |||
233 | 8 | return $written; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Reads a given number of bytes from the socket |
||
238 | * |
||
239 | * @param integer $byteCount |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | 2 | public function read($byteCount) |
|
247 | |||
248 | /** |
||
249 | * Closes underlying socket explicitly |
||
250 | */ |
||
251 | 41 | public function close() |
|
260 | |||
261 | /** |
||
262 | * Returns the current connect-timeout |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | 1 | public function getConnectTimeout() |
|
270 | |||
271 | /** |
||
272 | * Sets the connect-timeout |
||
273 | * |
||
274 | * @param int $timeout |
||
275 | */ |
||
276 | 1 | public function setConnectTimeout($timeout) |
|
280 | } |
||
281 |