-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
pipe.php
49 lines (41 loc) 路 1.3 KB
/
pipe.php
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
<?php
declare(strict_types=1);
namespace Psl\IO;
use Psl;
use Psl\Internal;
use Psl\OS;
use function error_get_last;
use function stream_socket_pair;
use const STREAM_IPPROTO_IP;
use const STREAM_PF_INET;
use const STREAM_PF_UNIX;
use const STREAM_SOCK_STREAM;
/**
* Create a pair of handles, where writes to the {@see WriteStreamHandleInterface} can be read from the {@see ReadStreamHandleInterface}.
*
* @return array{0: CloseReadStreamHandleInterface, 1: CloseWriteStreamHandleInterface}
*/
function pipe(): array
{
$sockets = Internal\suppress(
/**
* @return array{0: resource, 1: resource}
*/
static function (): array {
$domain = OS\is_windows() ? STREAM_PF_INET : STREAM_PF_UNIX;
$sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
// @codeCoverageIgnoreStart
if ($sockets === false) {
$error = error_get_last();
$message = $error['message'] ?? 'Unable to create a pipe stream.';
Psl\invariant_violation($message);
}
// @codeCoverageIgnoreEnd
return [$sockets[0], $sockets[1]];
},
);
return [
new CloseReadStreamHandle($sockets[0]),
new CloseWriteStreamHandle($sockets[1]),
];
}