Repository navigation
Expand file tree
/
Copy pathBufferedTcpSocket.php
More file actions
50 lines (50 loc) · 1.59 KB
/
Copy pathBufferedTcpSocket.php
File metadata and controls
50 lines (50 loc) · 1.59 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
<?php
class BufferedTcpSocket extends TcpSocket {
const LINEMODE = 1;
const CHUNKMODE = 2;
private $buffer;
private $delimiter = "\r\n";
public function init() {
parent::init();
}
public function processData() {
if ($this->mode == self::LINEMODE) {
$pos = strpos($this->buffer, $this->delimiter);
if ($pos !== false) {
$len = $pos + strlen($this->delimiter);
$retdata = substr($this->buffer, 0, $len);
$this->buffer = substr($this->buffer, $len);
$this->emit('ReadLine', $retdata);
} else {
return false;
}
} else {
if (strlen($this->buffer) >= $this->chunksize) {
$retdata = substr($this->buffer, 0, $this->chunksize);
$this->buffer = substr($this->buffer, $this->chunksize);
$this->emit('ReadChunk', $retdata);
} else {
return false;
}
}
}
public function onRead($data) {
$this->buffer .= $data;
while ($this->processData() !== false);
}
public function setMode($mode) {
if($mode == 'line' || $mode == self::LINEMODE) {
$this->mode = self::LINEMODE;
} else {
$this->mode = self::CHUNKMODE;
}
}
public function setChunkSize($size) {
$this->setMode(self::CHUNKMODE);
$this->chunksize = $size;
}
public function setLine($delimiter = "\r\n") {
$this->setMode(self::LINEMODE);
$this->delimiter = $delimiter;
}
}