|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Yiisoft\Yii\Debug\DevServer; |
|
8
|
|
|
|
|
9
|
|
|
use Closure; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
use Socket; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
final class Connection |
|
15
|
|
|
{ |
|
16
|
|
|
public const DEFAULT_SOCKET_DIR = '/tmp/var-dumper'; |
|
17
|
|
|
public const DEFAULT_SOCKET_URL = '/tmp/var-dumper-%d.sock'; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct( |
|
20
|
|
|
private Socket $socket, |
|
21
|
|
|
) { |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function create(): self |
|
25
|
|
|
{ |
|
26
|
|
|
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0); |
|
27
|
|
|
|
|
28
|
|
|
return new self( |
|
29
|
|
|
$socket, |
|
|
|
|
|
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function bind(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$n = random_int(0, PHP_INT_MAX); |
|
36
|
|
|
$file = sprintf(self::DEFAULT_SOCKET_URL, $n); |
|
37
|
|
|
if (!socket_bind($this->socket, $file)) { |
|
38
|
|
|
$socket_last_error = socket_last_error($this->socket); |
|
39
|
|
|
|
|
40
|
|
|
throw new RuntimeException( |
|
41
|
|
|
sprintf( |
|
42
|
|
|
'An error occurred while reading the socket. "socket_last_error" returned %d: "%s".', |
|
43
|
|
|
$socket_last_error, |
|
44
|
|
|
socket_strerror($socket_last_error), |
|
45
|
|
|
), |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function broadcast(string $data): void |
|
51
|
|
|
{ |
|
52
|
|
|
$files = glob(self::DEFAULT_SOCKET_DIR . '-*.sock', GLOB_NOSORT); |
|
53
|
|
|
//echo 'Files: ' . implode(', ', $files) . "\n"; |
|
54
|
|
|
foreach ($files as $file) { |
|
55
|
|
|
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0); |
|
56
|
|
|
if (!@socket_connect($socket, $file)) { |
|
57
|
|
|
@unlink($file); |
|
|
|
|
|
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
try { |
|
61
|
|
|
socket_send($socket, $data, strlen($data), 0); |
|
62
|
|
|
} catch (Throwable $e) { |
|
63
|
|
|
unlink($file); |
|
64
|
|
|
throw $e; |
|
65
|
|
|
} finally { |
|
66
|
|
|
socket_close($socket); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function close(): void |
|
72
|
|
|
{ |
|
73
|
|
|
@socket_getsockname($this->socket, $path); |
|
|
|
|
|
|
74
|
|
|
@socket_close($this->socket); |
|
|
|
|
|
|
75
|
|
|
@unlink($path); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function read(Closure $onSuccess, ?Closure $onError = null): \Generator |
|
79
|
|
|
{ |
|
80
|
|
|
while (true) { |
|
81
|
|
|
if (!socket_recvfrom($this->socket, $buffer, 32768, MSG_DONTWAIT, $ip, $port)) { |
|
82
|
|
|
$socket_last_error = socket_last_error($this->socket); |
|
83
|
|
|
if ($socket_last_error === 35) { |
|
84
|
|
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
$this->close(); |
|
87
|
|
|
if ($onError !== null) { |
|
88
|
|
|
yield $onError($socket_last_error); |
|
89
|
|
|
break; |
|
90
|
|
|
} |
|
91
|
|
|
throw new RuntimeException( |
|
92
|
|
|
sprintf( |
|
93
|
|
|
'An error occurred while reading the socket. socket_last_error returned %d: "%s".', |
|
94
|
|
|
$socket_last_error, |
|
95
|
|
|
socket_strerror($socket_last_error) |
|
96
|
|
|
), |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
yield $onSuccess($buffer, $ip, $port); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|