|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Tarantool Client package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Connection; |
|
15
|
|
|
|
|
16
|
|
|
use Tarantool\Client\Exception\CommunicationFailed; |
|
17
|
|
|
use Tarantool\Client\Exception\ConnectionFailed; |
|
18
|
|
|
use Tarantool\Client\IProto; |
|
19
|
|
|
use Tarantool\Client\Packer\PackUtils; |
|
20
|
|
|
|
|
21
|
|
|
final class Stream implements Connection |
|
22
|
|
|
{ |
|
23
|
|
|
private const DEFAULT_URI = 'tcp://127.0.0.1:3301'; |
|
24
|
|
|
|
|
25
|
|
|
private $uri; |
|
26
|
|
|
|
|
27
|
|
|
private $options = [ |
|
28
|
|
|
'connect_timeout' => 5, |
|
29
|
|
|
'socket_timeout' => 5, |
|
30
|
|
|
'tcp_nodelay' => true, |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
private $stream; |
|
34
|
|
|
|
|
35
|
126 |
|
public function __construct(string $uri = self::DEFAULT_URI, array $options = []) |
|
36
|
|
|
{ |
|
37
|
126 |
|
$this->uri = $uri; |
|
38
|
|
|
|
|
39
|
126 |
|
if ($options) { |
|
|
|
|
|
|
40
|
4 |
|
$this->options = $options + $this->options; |
|
41
|
|
|
} |
|
42
|
126 |
|
} |
|
43
|
|
|
|
|
44
|
126 |
|
public function open() : string |
|
45
|
|
|
{ |
|
46
|
126 |
|
$this->close(); |
|
47
|
|
|
|
|
48
|
126 |
|
$stream = @\stream_socket_client( |
|
49
|
126 |
|
$this->uri, |
|
50
|
126 |
|
$errorCode, |
|
51
|
126 |
|
$errorMessage, |
|
52
|
126 |
|
(float) $this->options['connect_timeout'], |
|
53
|
126 |
|
\STREAM_CLIENT_CONNECT, |
|
54
|
126 |
|
\stream_context_create(['socket' => ['tcp_nodelay' => (bool) $this->options['tcp_nodelay']]]) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
126 |
|
if (false === $stream) { |
|
58
|
3 |
|
throw ConnectionFailed::fromUriAndReason($this->uri, $errorMessage); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
123 |
|
$this->stream = $stream; |
|
62
|
123 |
|
\stream_set_timeout($this->stream, $this->options['socket_timeout']); |
|
63
|
|
|
|
|
64
|
123 |
|
$greeting = $this->read(IProto::GREETING_SIZE, 'Unable to read greeting.'); |
|
65
|
|
|
|
|
66
|
120 |
|
return IProto::parseGreeting($greeting); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
126 |
|
public function close() : void |
|
70
|
|
|
{ |
|
71
|
126 |
|
if ($this->stream) { |
|
72
|
2 |
|
\fclose($this->stream); |
|
73
|
2 |
|
$this->stream = null; |
|
74
|
|
|
} |
|
75
|
126 |
|
} |
|
76
|
|
|
|
|
77
|
106 |
|
public function isClosed() : bool |
|
78
|
|
|
{ |
|
79
|
106 |
|
return !\is_resource($this->stream); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
105 |
|
public function send(string $data) : string |
|
83
|
|
|
{ |
|
84
|
105 |
|
if (!\fwrite($this->stream, $data)) { |
|
85
|
|
|
throw new CommunicationFailed('Unable to write request.'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
105 |
|
$length = $this->read(IProto::LENGTH_SIZE, 'Unable to read response length.'); |
|
89
|
103 |
|
$length = PackUtils::unpackLength($length); |
|
90
|
|
|
|
|
91
|
103 |
|
return $this->read($length, 'Unable to read response.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
123 |
|
private function read(int $length, string $errorMessage) : string |
|
95
|
|
|
{ |
|
96
|
123 |
|
if ($data = \stream_get_contents($this->stream, $length)) { |
|
97
|
120 |
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
7 |
|
$meta = \stream_get_meta_data($this->stream); |
|
101
|
7 |
|
throw new CommunicationFailed($meta['timed_out'] ? 'Read timed out.' : $errorMessage); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.