1 | <?php |
||
15 | class Buffer implements WritableStreamInterface |
||
16 | { |
||
17 | use EventEmitterTrait; |
||
18 | |||
19 | /** |
||
20 | * @var resource |
||
21 | */ |
||
22 | protected $stream; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $listening; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $softLimit; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $writable; |
||
38 | |||
39 | /** |
||
40 | * @var EventLoopInterface |
||
41 | */ |
||
42 | protected $loop; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $data; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $lastError; |
||
53 | |||
54 | /** |
||
55 | * Buffer constructor. |
||
56 | * |
||
57 | * @param resource $stream |
||
58 | * @param EventLoopInterface $loop |
||
59 | * @param int $softLimit |
||
60 | */ |
||
61 | 18 | public function __construct($stream, EventLoopInterface $loop, int $softLimit = 2048) |
|
62 | { |
||
63 | 18 | $this->stream = $stream; |
|
64 | 18 | $this->loop = $loop; |
|
65 | 18 | $this->setSoftLimit($softLimit); |
|
66 | |||
67 | 18 | $this->setListening(false); |
|
68 | 18 | $this->writable = true; |
|
69 | 18 | $this->data = ''; |
|
70 | |||
71 | 18 | $this->lastError = [ |
|
72 | 'number' => 0, |
||
73 | 'message' => '', |
||
74 | 'file' => '', |
||
75 | 'line' => 0, |
||
76 | ]; |
||
77 | 18 | } |
|
78 | |||
79 | /** |
||
80 | * @return int |
||
81 | */ |
||
82 | 9 | public function getSoftLimit() : int |
|
86 | |||
87 | /** |
||
88 | * @param int $softLimit |
||
89 | * |
||
90 | * @return $this |
||
91 | */ |
||
92 | 18 | public function setSoftLimit(int $softLimit) : self |
|
93 | { |
||
94 | 18 | $this->softLimit = $softLimit; |
|
95 | |||
96 | 18 | return $this; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return bool |
||
101 | */ |
||
102 | 11 | public function isListening() : bool |
|
106 | |||
107 | /** |
||
108 | * @param bool $listening |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | 18 | public function setListening(bool $listening) : self |
|
113 | { |
||
114 | 18 | $this->listening = $listening; |
|
115 | |||
116 | 18 | return $this; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | */ |
||
122 | 2 | public function isWritable() : bool |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 10 | public function write($data) |
|
148 | |||
149 | /** |
||
150 | * @inheritdoc |
||
151 | */ |
||
152 | 3 | public function end($data = null) |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | 5 | public function close() : self |
|
180 | |||
181 | 8 | public function handleWrite() |
|
230 | |||
231 | 2 | protected function errorHandler($errno, $errstr, $errfile, $errline) |
|
238 | } |
||
239 |