Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stream.php
1<?php
2
11
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 // $context can be FALSE
64 if ($context)
65 {
66 $res = stream_socket_client($this->address, $errno, $errstr, $this->socketTimeout, $flags, $context);
67 }
68 else
69 {
70 $res = stream_socket_client($this->address, $errno, $errstr, $this->socketTimeout, $flags);
71 }
72
73 if (is_resource($res))
74 {
75 $this->resource = $res;
76
77 if ($this->streamTimeout > 0)
78 {
79 stream_set_timeout($this->resource, $this->streamTimeout);
80 $this->lastTime = time();
81 }
82
83 $this->setBlocking(false);
84 }
85 else
86 {
87 throw new \RuntimeException($errno > 0 ? "[{$errno}] {$errstr}" : 'Socket connection error.');
88 }
89 }
90
94 public function gets()
95 {
96 $result = fgets($this->resource);
97
98 if ($result !== false && $this->streamTimeout > 0)
99 {
100 $this->lastTime = time();
101 }
102
103 if ($this->timedOut())
104 {
105 throw new \RuntimeException('Stream reading timeout has been reached.');
106 }
107
108 return $result;
109 }
110
114 public function read(int $length): string
115 {
116 $result = parent::read($length);
117
118 if ($result !== '' && $this->streamTimeout > 0)
119 {
120 $this->lastTime = time();
121 }
122
123 if ($this->timedOut())
124 {
125 throw new \RuntimeException('Stream reading timeout has been reached.');
126 }
127
128 return $result;
129 }
130
134 public function write(string $string): int
135 {
136 // Handle E_NOTICE from fwrite()
137 set_error_handler(function ($errno, $errstr) {
138 throw new \RuntimeException($errstr);
139 });
140
141 try
142 {
143 $result = parent::write($string);
144 }
145 finally
146 {
147 restore_error_handler();
148 }
149
150 if ($this->streamTimeout > 0)
151 {
152 $this->lastTime = time();
153 }
154
155 if ($this->timedOut())
156 {
157 throw new \RuntimeException('Stream writing timeout has been reached.');
158 }
159
160 return $result;
161 }
162
169 public function setBlocking(bool $enable = true): bool
170 {
171 return stream_set_blocking($this->resource, $enable);
172 }
173
177 public function getResource()
178 {
179 return $this->resource;
180 }
181
188 public function enableCrypto(bool $enable = true)
189 {
190 return stream_socket_enable_crypto($this->resource, $enable, STREAM_CRYPTO_METHOD_ANY_CLIENT);
191 }
192
198 public function timedOut(): bool
199 {
200 if ($this->streamTimeout > 0)
201 {
202 if ($this->getMetadata('timed_out'))
203 {
204 return true;
205 }
206
207 if (time() > $this->lastTime + $this->streamTimeout)
208 {
209 return true;
210 }
211 }
212
213 return false;
214 }
215}
enableCrypto(bool $enable=true)
Definition stream.php:188
__construct(string $address, array $options=[])
Definition stream.php:27
setBlocking(bool $enable=true)
Definition stream.php:169
getMetadata(?string $key=null)
Definition stream.php:267