1 | <?php |
||
9 | class StreamIO implements IOInterface |
||
10 | { |
||
11 | const DEFAULT_CONNECTION_TIMEOUT = 30; |
||
12 | const DEFAULT_READING_TIMEOUT = 1; |
||
13 | |||
14 | /** |
||
15 | * @var resource|null |
||
16 | */ |
||
17 | private $stream; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $timeoutSec; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $timeoutUsec; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 23 | public function open($protocol, $host, $port, array $parameters = []) |
|
33 | { |
||
34 | 23 | if ($this->isOpen()) { |
|
35 | return $this; |
||
36 | } |
||
37 | |||
38 | 23 | $this->stream = $this->openConnection( |
|
39 | 23 | $protocol, |
|
40 | 23 | $host, |
|
41 | 23 | $port, |
|
42 | $parameters |
||
43 | 23 | ); |
|
44 | |||
45 | 23 | $this->tuneConnection($parameters); |
|
46 | |||
47 | 23 | return $this; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $protocol |
||
52 | * @param string $host |
||
53 | * @param int $port |
||
54 | * @param array $parameters |
||
55 | * |
||
56 | * @return resource |
||
57 | * |
||
58 | * @throws IOException |
||
59 | */ |
||
60 | 23 | private function openConnection($protocol, $host, $port, array $parameters = []) |
|
80 | |||
81 | /** |
||
82 | * @param array $parameters |
||
83 | * |
||
84 | * @return resource |
||
85 | */ |
||
86 | 23 | private function createStreamContext(array $parameters) |
|
109 | |||
110 | /** |
||
111 | * @param array $parameters |
||
112 | */ |
||
113 | 23 | private function tuneConnection(array $parameters) |
|
126 | |||
127 | /** |
||
128 | * @return $this |
||
129 | */ |
||
130 | 22 | public function close() |
|
131 | { |
||
132 | 22 | if (!$this->stream) { |
|
133 | return $this; |
||
134 | } |
||
135 | |||
136 | 22 | fclose($this->stream); |
|
137 | |||
138 | 22 | $this->stream = null; |
|
139 | |||
140 | 22 | return $this; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param string $data |
||
145 | * @param int|null $length |
||
146 | * |
||
147 | * @return $this |
||
148 | * |
||
149 | * @throws IOException |
||
150 | */ |
||
151 | 21 | public function write($data, $length = null) |
|
152 | { |
||
153 | 21 | if ($length === null) { |
|
154 | 21 | $length = strlen($data); |
|
155 | 21 | } |
|
156 | |||
157 | 21 | while ($length > 0) { |
|
158 | 21 | $written = @fwrite($this->stream, $data, $length); |
|
159 | 21 | if (!$written) { |
|
160 | throw new IOException('An error occur while writing to socket'); |
||
161 | } |
||
162 | |||
163 | 21 | $length -= $written; |
|
164 | 21 | $data = $length ? substr($data, $written, $length) : ''; |
|
165 | 21 | } |
|
166 | |||
167 | 21 | return $this; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 22 | public function read($length, $blocking = true) |
|
174 | { |
||
175 | 22 | stream_set_blocking($this->stream, $blocking); |
|
176 | |||
177 | 22 | if (!$this->isOpen()) { |
|
178 | 1 | throw new IOClosedException('Socket is closed or was not open'); |
|
179 | } |
||
180 | |||
181 | 22 | $r = [$this->stream]; |
|
182 | 22 | $w = null; |
|
183 | 22 | $e = null; |
|
184 | |||
185 | 22 | if ($blocking && @stream_select($r, $w, $e, $this->timeoutSec, $this->timeoutUsec) === false) { |
|
186 | throw new IOException('An error occur while selecting stream'); |
||
187 | } |
||
188 | |||
189 | 22 | if (($received = fread($this->stream, $length)) === false) { |
|
190 | throw new IOException('An error occur while reading from the socket'); |
||
191 | } |
||
192 | |||
193 | 22 | return $received; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return bool |
||
198 | */ |
||
199 | 23 | public function isOpen() |
|
203 | } |
||
204 |