-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathConnection.php
More file actions
58 lines (49 loc) · 1.78 KB
/
Copy pathConnection.php
File metadata and controls
58 lines (49 loc) · 1.78 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
<?php
namespace React\Socket;
use React\Stream\Stream;
/**
* The actual connection implementation for ConnectionInterface
*
* This class should only be used internally, see ConnectionInterface instead.
*
* @see ConnectionInterface
* @internal
*/
class Connection extends Stream implements ConnectionInterface
{
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)
{
// work around https://bugs.php.net/bug.php?id=74458 by checking if stream_socket_get_name has returned null string instead of address
if ($address === false || $address === "\0") {
return null;
}
// 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 $address;
}
}