1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jenner <[email protected]> |
4
|
|
|
* @blog http://www.huyanping.cn |
5
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
6
|
|
|
* @datetime: 2015/11/24 18:38 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Jenner\SimpleFork\Queue; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class PipeQueue implements QueueInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Pipe |
16
|
|
|
*/ |
17
|
|
|
protected $pipe; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $block; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $filename fifo filename |
26
|
|
|
* @param int $mode |
27
|
|
|
* @param bool $block if blocking |
|
|
|
|
28
|
|
|
*/ |
29
|
3 |
|
public function __construct($filename = '/tmp/simple-fork.pipe', $mode = 0666) |
30
|
|
|
{ |
31
|
3 |
|
$this->pipe = new Pipe($filename, $mode); |
32
|
3 |
|
$this->block = false; |
33
|
3 |
|
$this->pipe->setBlock($this->block); |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* put value into the queue of channel |
38
|
|
|
* |
39
|
|
|
* @param $value |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
3 |
|
public function put($value) |
43
|
|
|
{ |
44
|
3 |
|
$len = strlen($value); |
45
|
3 |
|
if ($len > 2147483647) { |
46
|
|
|
throw new \RuntimeException('value is too long'); |
47
|
|
|
} |
48
|
3 |
|
$raw = pack('N', $len) . $value; |
49
|
3 |
|
$write_len = $this->pipe->write($raw); |
50
|
|
|
|
51
|
3 |
|
return $write_len == strlen($raw); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* get value from the queue of channel |
56
|
|
|
* |
57
|
|
|
* @param bool $block if block when the queue is empty |
58
|
|
|
* @return bool|string |
59
|
|
|
*/ |
60
|
3 |
|
public function get($block = false) |
61
|
|
|
{ |
62
|
3 |
|
if ($this->block != $block) { |
63
|
|
|
$this->pipe->setBlock($block); |
64
|
|
|
$this->block = $block; |
65
|
|
|
} |
66
|
3 |
|
$len = $this->pipe->read(4); |
67
|
3 |
|
if ($len === false) { |
68
|
|
|
throw new \RuntimeException('read pipe failed'); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
if (strlen($len) === 0) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
3 |
|
$len = unpack('N', $len); |
75
|
3 |
|
if (empty($len) || !array_key_exists(1, $len) || empty($len[1])) { |
76
|
|
|
throw new \RuntimeException('data protocol error'); |
77
|
|
|
} |
78
|
3 |
|
$len = intval($len[1]); |
79
|
|
|
|
80
|
3 |
|
$value = ''; |
81
|
3 |
|
while (true) { |
82
|
3 |
|
$temp = $this->pipe->read($len); |
83
|
3 |
|
if (strlen($temp) == $len) { |
84
|
3 |
|
return $temp; |
85
|
|
|
} |
86
|
|
|
$value .= $temp; |
87
|
|
|
$len -= strlen($temp); |
88
|
|
|
if ($len == 0) { |
89
|
|
|
return $value; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* remove the queue resource |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function remove() |
100
|
|
|
{ |
101
|
|
|
$this->pipe->close(); |
102
|
|
|
$this->pipe->remove(); |
103
|
|
|
} |
104
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.