1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Yiisoft\Yii\Debug\DebugServer; |
8
|
|
|
|
9
|
|
|
use Generator; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Socket; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* List of socket errors: {@see https://www.ibm.com/docs/en/zos/2.4.0?topic=calls-sockets-return-codes-errnos} |
16
|
|
|
*/ |
17
|
|
|
final class Connection |
18
|
|
|
{ |
19
|
|
|
public const DEFAULT_TIMEOUT = 10 * 1000; // 10 milliseconds |
20
|
|
|
public const DEFAULT_BUFFER_SIZE = 1 * 1024; // 1 kilobyte |
21
|
|
|
|
22
|
|
|
public const TYPE_RESULT = 0x001B; |
23
|
|
|
public const TYPE_ERROR = 0x002B; |
24
|
|
|
|
25
|
|
|
public const MESSAGE_TYPE_VAR_DUMPER = 0x001B; |
26
|
|
|
public const MESSAGE_TYPE_LOGGER = 0x002B; |
27
|
|
|
|
28
|
|
|
private string $uri; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
private Socket $socket, |
32
|
|
|
) { |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function create(): self |
36
|
|
|
{ |
37
|
|
|
$socket = socket_create(AF_UNIX, SOCK_DGRAM, 0); |
38
|
|
|
|
39
|
|
|
$socket_last_error = socket_last_error($socket); |
40
|
|
|
|
41
|
|
|
if ($socket_last_error) { |
42
|
|
|
throw new RuntimeException( |
43
|
|
|
sprintf( |
44
|
|
|
'"socket_last_error" returned %d: "%s".', |
45
|
|
|
$socket_last_error, |
46
|
|
|
socket_strerror($socket_last_error), |
47
|
|
|
), |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return new self( |
52
|
|
|
$socket, |
|
|
|
|
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function bind(): void |
57
|
|
|
{ |
58
|
|
|
$n = random_int(0, PHP_INT_MAX); |
59
|
|
|
$file = sprintf(sys_get_temp_dir() . '/yii-dev-server-%d.sock', $n); |
60
|
|
|
$this->uri = $file; |
61
|
|
|
if (!socket_bind($this->socket, $file)) { |
62
|
|
|
$socket_last_error = socket_last_error($this->socket); |
63
|
|
|
|
64
|
|
|
throw new RuntimeException( |
65
|
|
|
sprintf( |
66
|
|
|
'An error occurred while reading the socket. "socket_last_error" returned %d: "%s".', |
67
|
|
|
$socket_last_error, |
68
|
|
|
socket_strerror($socket_last_error), |
69
|
|
|
), |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Generator<int, array{0: self::TYPE_ERROR|self::TYPE_RESULT, 1: string, 2: int|string, 3?: int}> |
76
|
|
|
*/ |
77
|
|
|
public function read(): Generator |
78
|
|
|
{ |
79
|
|
|
while (true) { |
80
|
|
|
if (!socket_recvfrom($this->socket, $header, self::DEFAULT_BUFFER_SIZE, MSG_DONTWAIT, $ip, $port)) { |
81
|
|
|
$socket_last_error = socket_last_error($this->socket); |
82
|
|
|
if ($socket_last_error === 35) { |
83
|
|
|
usleep(self::DEFAULT_TIMEOUT); |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
$this->close(); |
87
|
|
|
yield [self::TYPE_ERROR, $socket_last_error, socket_strerror($socket_last_error)]; |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$length = unpack('N', $header); |
92
|
|
|
$localBuffer = ''; |
93
|
|
|
$bytesToRead = $length[1]; |
94
|
|
|
$bytesRead = 0; |
95
|
|
|
while ($bytesRead < $bytesToRead) { |
96
|
|
|
if (!$bufferLength = socket_recvfrom($this->socket, $buffer, self::DEFAULT_BUFFER_SIZE, MSG_DONTWAIT, $ip, $port)) { |
97
|
|
|
$socket_last_error = socket_last_error($this->socket); |
98
|
|
|
if ($socket_last_error === 35) { |
99
|
|
|
usleep(self::DEFAULT_TIMEOUT); |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
$this->close(); |
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$localBuffer .= $buffer; |
107
|
|
|
$bytesRead += $bufferLength; |
108
|
|
|
} |
109
|
|
|
yield [self::TYPE_RESULT, base64_decode($localBuffer), $ip, $port]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function broadcast(int $type, string $data): array |
114
|
|
|
{ |
115
|
|
|
$files = glob(sys_get_temp_dir() . '/yii-dev-server-*.sock', GLOB_NOSORT); |
116
|
|
|
//echo 'Files: ' . implode(', ', $files) . "\n"; |
117
|
|
|
$uniqueErrors = []; |
118
|
|
|
$payload = base64_encode(json_encode([$type, $data], JSON_THROW_ON_ERROR)); |
119
|
|
|
$payloadLength = strlen($payload); |
|
|
|
|
120
|
|
|
foreach ($files as $file) { |
121
|
|
|
$socket = @fsockopen('udg://' . $file, -1, $errno, $errstr); |
122
|
|
|
if ($errno === 61) { |
123
|
|
|
@unlink($file); |
|
|
|
|
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
if ($errno !== 0) { |
127
|
|
|
$uniqueErrors[$errno] = $errstr; |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
try { |
131
|
|
|
if (!$this->fwriteStream($socket, $payload)) { |
|
|
|
|
132
|
|
|
$uniqueErrors[] = error_get_last(); |
133
|
|
|
/** |
134
|
|
|
* Connection is closed. |
135
|
|
|
*/ |
136
|
|
|
continue; |
137
|
|
|
} |
138
|
|
|
} catch (Throwable $e) { |
139
|
|
|
//@unlink($file); |
140
|
|
|
throw $e; |
141
|
|
|
} finally { |
142
|
|
|
//fflush($socket); |
143
|
|
|
fclose($socket); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
return $uniqueErrors; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getUri(): string |
150
|
|
|
{ |
151
|
|
|
return $this->uri; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function close(): void |
155
|
|
|
{ |
156
|
|
|
@socket_getsockname($this->socket, $path); |
|
|
|
|
157
|
|
|
@socket_close($this->socket); |
|
|
|
|
158
|
|
|
@unlink($path); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param resource $fp |
163
|
|
|
*/ |
164
|
|
|
private function fwriteStream($fp, string $data): int|false |
165
|
|
|
{ |
166
|
|
|
$strlen = strlen($data); |
167
|
|
|
fwrite($fp, pack('N', $strlen)); |
168
|
|
|
for ($written = 0; $written < $strlen; $written += $fwrite) { |
169
|
|
|
$fwrite = fwrite($fp, substr($data, $written), self::DEFAULT_BUFFER_SIZE); |
170
|
|
|
//\fflush($fp); |
171
|
|
|
usleep(self::DEFAULT_TIMEOUT / 2); |
172
|
|
|
if ($fwrite === false) { |
173
|
|
|
return $written; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return $written; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|