forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.php
More file actions
173 lines (144 loc) · 5.15 KB
/
Connection.php
File metadata and controls
173 lines (144 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace React\Socket;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Stream\DuplexResourceStream;
use React\Stream\Stream;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;
/**
* The actual connection implementation for ConnectionInterface
*
* This class should only be used internally, see ConnectionInterface instead.
*
* @see ConnectionInterface
* @internal
*/
class Connection extends EventEmitter implements ConnectionInterface
{
/**
* Internal flag whether this is a Unix domain socket (UDS) connection
*
* @internal
*/
public $unix = false;
/**
* Internal flag whether encryption has been enabled on this connection
*
* Mostly used by internal StreamEncryption so that connection returns
* `tls://` scheme for encrypted connections instead of `tcp://`.
*
* @internal
*/
public $encryptionEnabled = false;
/** @internal */
public $stream;
private $input;
public function __construct($resource, LoopInterface $loop)
{
// PHP < 5.6.8 suffers from a buffer indicator bug on secure TLS connections
// as a work-around we always read the complete buffer until its end.
// The buffer size is limited due to TCP/IP buffers anyway, so this
// should not affect usage otherwise.
// See https://bugs.php.net/bug.php?id=65137
// https://bugs.php.net/bug.php?id=41631
// https://github.com/reactphp/socket-client/issues/24
$clearCompleteBuffer = (version_compare(PHP_VERSION, '5.6.8', '<'));
// @codeCoverageIgnoreStart
if (class_exists('React\Stream\Stream')) {
// legacy react/stream < 0.7 requires additional buffer property
$this->input = new Stream($resource, $loop);
if ($clearCompleteBuffer) {
$this->input->bufferSize = null;
}
} else {
// preferred react/stream >= 0.7 accepts buffer parameter
$this->input = new DuplexResourceStream($resource, $loop, $clearCompleteBuffer ? -1 : null);
}
// @codeCoverageIgnoreEnd
$this->stream = $resource;
Util::forwardEvents($this->input, $this, array('data', 'end', 'error', 'close', 'pipe', 'drain'));
$this->input->on('close', array($this, 'close'));
}
public function isReadable()
{
return $this->input->isReadable();
}
public function isWritable()
{
return $this->input->isWritable();
}
public function pause()
{
$this->input->pause();
}
public function resume()
{
$this->input->resume();
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
return $this->input->pipe($dest, $options);
}
public function write($data)
{
return $this->input->write($data);
}
public function end($data = null)
{
$this->input->end($data);
}
public function close()
{
$this->input->close();
$this->handleClose();
$this->removeAllListeners();
}
public function handleClose()
{
if (!is_resource($this->stream)) {
return;
}
// Try to cleanly shut down socket and ignore any errors in case other
// side already closed. Shutting down may return to blocking mode on
// some legacy versions, so reset to non-blocking just in case before
// continuing to close the socket resource.
@stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
stream_set_blocking($this->stream, false);
fclose($this->stream);
}
public function getRemoteAddress()
{
return $this->parseAddress(@stream_socket_get_name($this->stream, true));
}
public function getLocalAddress()
{
return $this->parseAddress(@stream_socket_get_name($this->stream, false));
}
private function parseAddress($address)
{
if ($address === false) {
return null;
}
if ($this->unix) {
// remove trailing colon from address for HHVM < 3.19: https://3v4l.org/5C1lo
// note that techncially ":" is a valid address, so keep this in place otherwise
if (substr($address, -1) === ':' && defined('HHVM_VERSION_ID') && HHVM_VERSION_ID < 31900) {
$address = (string)substr($address, 0, -1);
}
// work around unknown addresses should return null value: https://3v4l.org/5C1lo
// PHP uses "\0" string and HHVM uses empty string (colon removed above)
if ($address === "\x00" || $address === '') {
return null;
}
return 'unix://' . $address;
}
// check if this is an IPv6 address which includes multiple colons but no square brackets
$pos = strrpos($address, ':');
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
$port = substr($address, $pos + 1);
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
}
return ($this->encryptionEnabled ? 'tls' : 'tcp') . '://' . $address;
}
}