This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventBuffer.php
420 lines (388 loc) · 8.76 KB
/
EventBuffer.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
namespace Aza\Components\LibEvent;
use Aza\Components\LibEvent\Exceptions\Exception;
/**
* LibEvent buffered event resourse wrapper
*
* @link http://www.wangafu.net/~nickm/libevent-book/
*
* @uses libevent
*
* @project Anizoptera CMF
* @package system.libevent
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
class EventBuffer extends EventBasic
{
/**
* Buffer read error
*/
const E_READ = 0x01; // EVBUFFER_READ
/**
* Buffer write error
*/
const E_WRITE = 0x02; // EVBUFFER_WRITE
/**
* Buffer EOF error
*/
const E_EOF = 0x10; // EVBUFFER_EOF
/**
* Buffer error
*/
const E_ERROR = 0x20; // EVBUFFER_ERROR
/**
* Buffer timeout error
*/
const E_TIMEOUT = 0x40; // EVBUFFER_TIMEOUT
/**
* Default <i>lowmark</i>
*
* @see setWatermark
*/
const DEF_LOWMARK = 1;
/**
* Default <i>highmark</i>
*
* @see setWatermark
*/
const DEF_HIGHMARK = 0xffffff;
/**
* Default priority
*
* @see setPriority
*/
const DEF_PRIORITY = 10;
/**
* Default read timeout
*
* @see setTimout
*/
const DEF_TIMEOUT_READ = 30;
/**
* Default write timeout
*
* @see setTimout
*/
const DEF_TIMEOUT_WRITE = 30;
/**
* @var resource
*/
public $stream;
/**
* Creates a new buffered event resource.
*
* @see event_buffer_new
*
* @throws Exception
*
* @param resource $stream <p>
* Valid PHP stream resource.
* Must be castable to file descriptor.
* </p>
* @param callback|null $readcb <p>
* Callback to invoke where there is data to read,
* or NULL if no callback is desired.
* <br><tt>function(resource $buf,
* array $args(EventBuffer $e, mixed $arg))</tt>
* </p>
* @param callback|null $writecb <p>
* Callback to invoke where the descriptor is ready
* for writing, or NULL if no callback is desired.
* <br><tt>function(resource $buf,
* array $args(EventBuffer $e, mixed $arg))</tt>
* </p>
* @param callback $errorcb <p>
* Callback to invoke where there is an error
* on the descriptor, cannot be NULL.
* <br><tt>function(resource $buf, int $what,
* array $args(EventBuffer $e, mixed $arg))</tt>
* </p>
* @param mixed $arg [optional] <p>
* An argument that will be passed
* to each of the callbacks.
* </p>
*/
public function __construct($stream, $readcb,
$writecb, $errorcb, $arg = null)
{
parent::__construct();
$this->stream = $stream;
if (!$this->resource = event_buffer_new(
$stream, $readcb, $writecb, $errorcb,
array($this, $arg)
)) {
throw new Exception(
"Can't create new buffered event resourse (event_buffer_new)"
);
}
}
/**
* Disables buffered event
*
* @see event_buffer_disable
*
* @throws Exception
*
* @param int $events Any combination of EV_READ and EV_WRITE.
*
* @return self
*/
public function disable($events)
{
$this->checkResourse();
if (!event_buffer_disable($this->resource, $events)) {
throw new Exception(
"Can't disable buffered event (event_buffer_disable)"
);
}
return $this;
}
/**
* Enables buffered event
*
* @see event_buffer_enable
*
* @throws Exception
*
* @param int $events Any combination of EV_READ and EV_WRITE.
*
* @return self
*/
public function enable($events)
{
$this->checkResourse();
if (!event_buffer_enable($this->resource, $events)) {
throw new Exception(
"Can't enable buffered event (event_buffer_enable)"
);
}
return $this;
}
/**
* Associate event with an event base
*
* @see event_buffer_base_set
*
* @throws Exception
*
* @param EventBase $event_base
*
* @return self
*/
public function setBase($event_base)
{
$this->checkResourse();
$event_base->checkResourse();
if (!event_buffer_base_set(
$this->resource, $event_base->resource
)) {
throw new Exception(
"Can't set buffered event base (event_buffer_base_set)"
);
}
return parent::setBase($event_base);
}
/**
* Destroys the buffered event and frees all the resources associated.
*
* @see event_buffer_free
*
* @throws Exception
*
* @return self
*/
public function free()
{
parent::free();
if ($this->resource) {
event_buffer_free($this->resource);
$this->resource = null;
}
return $this;
}
/**
* Reads data from the input buffer of the buffered event.
*
* @see event_buffer_read
*
* @param int $data_size Data size in bytes.
*
* @return string|bool Data from buffer or FALSE
*/
public function read($data_size)
{
$this->checkResourse();
return event_buffer_read(
$this->resource, $data_size
);
}
/**
* Writes data to the specified buffered event.
*
* @see event_buffer_write
*
* @throws Exception
*
* @param string $data <p>
* The data to be written.
* </p>
* @param int $data_size [optional] <p>
* Optional size parameter. Writes all the data by default
* </p>
*
* @return self
*/
public function write($data, $data_size = -1)
{
$this->checkResourse();
if (!event_buffer_write($this->resource, $data, $data_size)) {
throw new Exception(
"Can't write data to the buffered event (event_buffer_write)"
);
}
return $this;
}
/**
* Changes the stream on which the buffered event operates.
*
* @see event_buffer_fd_set
*
* @throws Exception
*
* @param resource $stream <p>
* Valid PHP stream, must be castable to file descriptor.
* </p>
*
* @return self
*/
public function setStream($stream)
{
$this->checkResourse();
if (!event_buffer_fd_set($this->resource, $stream)) {
throw new Exception("Can't set buffered event stream (event_buffer_fd_set)");
}
$this->stream = $stream;
return $this;
}
/**
* Sets or changes existing callbacks for the buffered event.
*
* @see event_buffer_set_callback
*
* @throws Exception
*
* @param callback|null $readcb <p>
* Callback to invoke where there is data to read,
* or NULL if no callback is desired.
* <br><tt>function(resource $buf,
* array $args(EventBuffer $e, mixed $arg))</tt>
* </p>
* @param callback|null $writecb <p>
* Callback to invoke where the descriptor is ready
* for writing, or NULL if no callback is desired.
* <br><tt>function(resource $buf,
* array $args(EventBuffer $e, mixed $arg))</tt>
* </p>
* @param callback $errorcb <p>
* Callback to invoke where there is an error on
* the descriptor, cannot be NULL.
* <br><tt>function(resource $buf, int $what,
* array $args(EventBuffer $e, mixed $arg))</tt>
* </p>
* @param mixed $arg [optional] <p>
* An argument that will be passed
* to each of the callbacks.
* </p>
*
* @return self
*/
public function setCallback($readcb, $writecb, $errorcb, $arg = null)
{
$this->checkResourse();
if (!event_buffer_set_callback(
$this->resource, $readcb, $writecb, $errorcb,
array($this, $arg)
)) {
throw new Exception(
"Can't set buffered event callbacks (event_buffer_set_callback)"
);
}
return $this;
}
/**
* Sets the read and write timeouts for the specified buffered event.
*
* @see event_buffer_timeout_set
*
* @throws Exception
*
* @param int $read_timeout Read timeout (in seconds).
* @param int $write_timeout Write timeout (in seconds).
*
* @return self
*/
public function setTimout($read_timeout = self::DEF_TIMEOUT_READ,
$write_timeout = self::DEF_TIMEOUT_WRITE)
{
$this->checkResourse();
event_buffer_timeout_set(
$this->resource, $read_timeout, $write_timeout
);
return $this;
}
/**
* Set the marks for read and write events.
*
* <p>Libevent does not invoke read callback unless
* there is at least <i>lowmark</i> bytes in the
* input buffer; if the read buffer is beyond the
* <i>highmark</i>, reading is stopped. On output,
* the write callback is invoked whenever the
* buffered data falls below the <i>lowmark</i>.</p>
*
* @see event_buffer_timeout_set
*
* @throws Exception
*
* @param int $events Any combination of EV_READ and EV_WRITE.
* @param int $lowmark Low watermark.
* @param int $highmark High watermark.
*
* @return self
*/
public function setWatermark($events, $lowmark = self::DEF_LOWMARK,
$highmark = self::DEF_HIGHMARK)
{
$this->checkResourse();
event_buffer_watermark_set(
$this->resource, $events, $lowmark, $highmark
);
return $this;
}
/**
* Assign a priority to a buffered event.
*
* @see event_buffer_priority_set
*
* @param int $value <p>
* Priority level. Cannot be less than zero and
* cannot exceed maximum priority level of the
* event base (see {@link event_base_priority_init}()).
* </p>
*
* @return self
*
* @throws Exception
*/
public function setPriority($value = self::DEF_PRIORITY)
{
$this->checkResourse();
if (!event_buffer_priority_set($this->resource, $value)) {
throw new Exception(
"Can't set buffered event priority to {$value} (event_buffer_priority_set)"
);
}
return $this;
}
}