forked from reactphp/stream
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuffer.php
More file actions
133 lines (106 loc) · 3.69 KB
/
Copy pathBuffer.php
File metadata and controls
133 lines (106 loc) · 3.69 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace React\Stream;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
/** @event full-drain */
class Buffer extends EventEmitter implements WritableStreamInterface
{
public $stream;
public $listening = false;
public $softLimit = 65536;
private $writable = true;
private $loop;
private $data = '';
public function __construct($stream, LoopInterface $loop)
{
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
}
$this->stream = $stream;
$this->loop = $loop;
}
public function isWritable()
{
return $this->writable;
}
public function write($data)
{
if (!$this->writable) {
return;
}
$this->data .= $data;
if (!$this->listening && $this->data !== '') {
$this->listening = true;
$this->loop->addWriteStream($this->stream, array($this, 'handleWrite'));
$this->handleWrite();
}
return !isset($this->data[$this->softLimit - 1]);
}
public function end($data = null)
{
if (null !== $data) {
$this->write($data);
}
$this->writable = false;
if ($this->listening) {
$this->on('full-drain', array($this, 'close'));
} else {
$this->close();
}
}
public function close()
{
$this->writable = false;
$this->listening = false;
$this->data = '';
$this->emit('close', array($this));
}
public function handleWrite()
{
$error = null;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
$error = array(
'message' => $errstr,
'number' => $errno,
'file' => $errfile,
'line' => $errline
);
});
$sent = fwrite($this->stream, $this->data);
restore_error_handler();
// Only report errors if *nothing* could be sent.
// Any hard (permanent) error will fail to send any data at all.
// Sending excessive amounts of data will only flush *some* data and then
// report a temporary error (EAGAIN) which we do not raise here in order
// to keep the stream open for further tries to write.
// Should this turn out to be a permanent error later, it will eventually
// send *nothing* and we can detect this.
if ($sent === 0 || $sent === false) {
if ($error === null) {
$error = new \RuntimeException('Send failed');
} else {
$error = new \ErrorException(
$error['message'],
0,
$error['number'],
$error['file'],
$error['line']
);
}
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error->getMessage(), 0, $error), $this));
return;
}
$exceeded = isset($this->data[$this->softLimit - 1]);
$this->data = (string) substr($this->data, $sent);
// buffer has been above limit and is now below limit
if ($exceeded && !isset($this->data[$this->softLimit - 1])) {
$this->emit('drain', array($this));
}
// buffer is now completely empty (and not closed already)
if ($this->data === '' && $this->listening) {
$this->loop->removeWriteStream($this->stream);
$this->listening = false;
$this->emit('full-drain', array($this));
}
}
}