-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSecureStream.php
More file actions
98 lines (77 loc) · 2.16 KB
/
SecureStream.php
File metadata and controls
98 lines (77 loc) · 2.16 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace React\SocketClient;
use Evenement\EventEmitterTrait;
use React\EventLoop\LoopInterface;
use React\Stream\WritableStreamInterface;
use React\Stream\Stream;
use React\Stream\Util;
class SecureStream extends Stream
{
// use EventEmitterTrait;
public $stream;
public $decorating;
protected $loop;
public function __construct(Stream $stream, LoopInterface $loop) {
$this->stream = $stream->stream;
$this->decorating = $stream;
$this->loop = $loop;
$that = $this;
$stream->on('error', function($error) use ($that) {
$that->emit('error', array($error, $that));
});
$stream->on('end', function() use ($that) {
$that->emit('end', array($that));
});
$stream->on('close', function() use ($that) {
$that->emit('close', array($that));
});
$stream->on('drain', function() use ($that) {
$that->emit('drain', array($that));
});
$stream->pause();
$this->resume();
}
public function handleData($stream)
{
$data = stream_get_contents($stream);
$this->emit('data', array($data, $this));
if (!is_resource($stream) || feof($stream)) {
$this->end();
}
}
public function pause()
{
$this->loop->removeReadStream($this->decorating->stream);
}
public function resume()
{
if ($this->isReadable()) {
$this->loop->addReadStream($this->decorating->stream, array($this, 'handleData'));
}
}
public function isReadable()
{
return $this->decorating->isReadable();
}
public function isWritable()
{
return $this->decorating->isWritable();
}
public function write($data)
{
return $this->decorating->write($data);
}
public function close()
{
return $this->decorating->close();
}
public function end($data = null)
{
return $this->decorating->end($data);
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
Util::pipe($this, $dest, $options);
return $dest;
}
}