-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloop_io_stream_pipe.php
More file actions
77 lines (64 loc) · 2.31 KB
/
loop_io_stream_pipe.php
File metadata and controls
77 lines (64 loc) · 2.31 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* ---------------------------------------------------------------------------------------------------------------------
* DESCRIPTION
* ---------------------------------------------------------------------------------------------------------------------
* This file contains the example of using streams in loop.
*
* ---------------------------------------------------------------------------------------------------------------------
* USAGE
* ---------------------------------------------------------------------------------------------------------------------
* To run this example in CLI from project root use following syntax
*
* $> php ./example/loop_io_stream_pipe.php
*
* Following flags are supported to test example with different configurations:
*
* --model : define model to use, default: select, supported: [ select ]
*
* Ex:
* $> php ./example/loop_io_stream_pipe.php --model=select
*
* IMPORTANT!!!
*
* For the best result, use loop_io_stream_source.php and loop_io_stream_pipe.php together.
*
* Ex:
* $> php ./example/loop_io_stream_source.php | php ./example/loop_io_stream_pipe.php
* ---------------------------------------------------------------------------------------------------------------------
*/
$model = '';
require_once __DIR__ . '/bootstrap/autoload.php';
use Dazzle\Loop\Loop;
$loop = new Loop(new $model);
if (stream_set_blocking(STDIN, false) !== true) {
fwrite(STDERR, "ERROR: Unable to set STDIN non-blocking.\n");
exit(1);
}
if (stream_set_blocking(STDOUT, false) !== true) {
fwrite(STDERR, "ERROR: Unable to set STDOUT non-blocking.\n");
exit(1);
}
$buffer = '';
$loop->addReadStream(STDIN, function($stream) use($loop, &$buffer) {
$chunk = fread($stream, 64 * 1024);
// reading nothing means we reached EOF
if ($chunk === '') {
$loop->removeReadStream($stream);
return;
}
$buffer .= $chunk;
});
$loop->addWriteStream(STDOUT, function($stream) use($loop, &$buffer) {
// try to write to output
$r = fwrite($stream, $buffer);
// nothing could be written, so stream is closed
if ($buffer !== '' && $r === 0) {
$loop->removeWriteStream($stream);
fclose($stream);
fwrite(STDERR, "Stopped because STDOUT closed\n");
return;
}
$buffer = substr($buffer, $r);
});
$loop->start();