1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
stream.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Web\Http\Socket;
11
12use Bitrix\Main\Web\Http;
13
14class Stream extends Http\Stream
15{
16 protected string $address;
17 protected int $socketTimeout = 30;
18 protected int $streamTimeout = 60;
19 protected array $contextOptions = [];
20 protected bool $async = false;
21 protected int $lastTime = 0;
22
27 public function __construct(string $address, array $options = [])
28 {
29 $this->address = $address;
30
31 if (isset($options['socketTimeout']))
32 {
33 $this->socketTimeout = (int)$options['socketTimeout'];
34 }
35 if (isset($options['streamTimeout']))
36 {
37 $this->streamTimeout = (int)$options['streamTimeout'];
38 }
39 if (isset($options['contextOptions']))
40 {
41 $this->contextOptions = $options['contextOptions'];
42 }
43 if (isset($options['async']))
44 {
45 $this->async = (bool)$options['async'];
46 }
47 }
48
53 public function connect(): void
54 {
55 $context = stream_context_create($this->contextOptions);
56
57 $flags = STREAM_CLIENT_CONNECT;
58 if ($this->async)
59 {
60 $flags |= STREAM_CLIENT_ASYNC_CONNECT;
61 }
62
63 // Handle E_WARNING from stream_socket_client()
64 set_error_handler(function ($errno, $errstr) {
65 throw new \RuntimeException("[{$errno}] {$errstr}");
66 });
67
68 try
69 {
70 // $context can be FALSE
71 if ($context)
72 {
73 $res = stream_socket_client($this->address, $errno, $errstr, $this->socketTimeout, $flags, $context);
74 }
75 else
76 {
77 $res = stream_socket_client($this->address, $errno, $errstr, $this->socketTimeout, $flags);
78 }
79 }
80 finally
81 {
82 restore_error_handler();
83 }
84
85 if (is_resource($res))
86 {
87 $this->resource = $res;
88
89 if ($this->streamTimeout > 0)
90 {
91 stream_set_timeout($this->resource, $this->streamTimeout);
92 $this->lastTime = time();
93 }
94
95 if ($this->async)
96 {
97 $this->setBlocking(false);
98 }
99 }
100 else
101 {
102 throw new \RuntimeException($errno > 0 ? "[{$errno}] {$errstr}" : 'Socket connection error.');
103 }
104 }
105
109 public function gets()
110 {
111 $result = fgets($this->resource);
112
113 if ($result !== false && $this->streamTimeout > 0)
114 {
115 $this->lastTime = time();
116 }
117
118 if ($this->timedOut())
119 {
120 throw new \RuntimeException('Stream reading timeout has been reached.');
121 }
122
123 return $result;
124 }
125
129 public function read(int $length): string
130 {
131 $result = parent::read($length);
132
133 if ($result !== '' && $this->streamTimeout > 0)
134 {
135 $this->lastTime = time();
136 }
137
138 if ($this->timedOut())
139 {
140 throw new \RuntimeException('Stream reading timeout has been reached.');
141 }
142
143 return $result;
144 }
145
149 public function write(string $string): int
150 {
151 // Handle E_NOTICE from fwrite()
152 set_error_handler(function ($errno, $errstr) {
153 throw new \RuntimeException($errstr);
154 });
155
156 try
157 {
158 $result = parent::write($string);
159 }
160 finally
161 {
162 restore_error_handler();
163 }
164
165 if ($this->streamTimeout > 0)
166 {
167 $this->lastTime = time();
168 }
169
170 if ($this->timedOut())
171 {
172 throw new \RuntimeException('Stream writing timeout has been reached.');
173 }
174
175 return $result;
176 }
177
184 public function setBlocking(bool $enable = true): bool
185 {
186 return stream_set_blocking($this->resource, $enable);
187 }
188
192 public function getResource()
193 {
194 return $this->resource;
195 }
196
203 public function enableCrypto(bool $enable = true)
204 {
205 return stream_socket_enable_crypto($this->resource, $enable, STREAM_CRYPTO_METHOD_ANY_CLIENT);
206 }
207
213 public function timedOut(): bool
214 {
215 if ($this->streamTimeout > 0)
216 {
217 if ($this->getMetadata('timed_out'))
218 {
219 return true;
220 }
221
222 if (time() > $this->lastTime + $this->streamTimeout)
223 {
224 return true;
225 }
226 }
227
228 return false;
229 }
230
236 public function getAddress(): string
237 {
238 return $this->address;
239 }
240}
array $contextOptions
Определения stream.php:19
enableCrypto(bool $enable=true)
Определения stream.php:203
read(int $length)
Определения stream.php:129
__construct(string $address, array $options=[])
Определения stream.php:27
setBlocking(bool $enable=true)
Определения stream.php:184
string $address
Определения stream.php:16
write(string $string)
Определения stream.php:149
getMetadata(?string $key=null)
Определения stream.php:267
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223