/
pipe.php
48 lines (40 loc) 路 1.24 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
<?php
declare(strict_types=1);
namespace Psl\IO;
use Psl;
use Psl\Internal;
use function error_get_last;
use function stream_socket_pair;
use const STREAM_IPPROTO_IP;
use const STREAM_PF_UNIX;
use const STREAM_SOCK_STREAM;
/**
* Create a pair of handles, where writes to the WriteHandle can be read from the ReadHandle.
*
* @throws Exception\BlockingException If unable to set one of the handles to non-blocking mode.
*
* @return array{0: CloseReadHandleInterface, 1: CloseWriteHandleInterface}
*/
function pipe(): array
{
$sockets = Internal\suppress(
/**
* @return array{0: resource, 1: resource}
*/
static function (): array {
$sockets = stream_socket_pair(STREAM_PF_UNIX, 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 Stream\CloseReadHandle($sockets[0]),
new Stream\CloseWriteHandle($sockets[1]),
];
}