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 16:29 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Jenner\SimpleFork\Queue; |
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class Pipe |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var resource |
16
|
|
|
*/ |
17
|
|
|
protected $read; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var resource |
21
|
|
|
*/ |
22
|
|
|
protected $write; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $filename; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $block; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $filename fifo filename |
36
|
|
|
* @param int $mode |
37
|
|
|
* @param bool $block if blocking |
38
|
|
|
*/ |
39
|
12 |
|
public function __construct($filename = '/tmp/simple-fork.pipe', $mode = 0666, $block = false) |
40
|
|
|
{ |
41
|
12 |
|
if (!file_exists($filename) && !posix_mkfifo($filename, $mode)) { |
42
|
|
|
throw new \RuntimeException("create pipe failed"); |
43
|
|
|
} |
44
|
12 |
|
if (filetype($filename) != "fifo") { |
45
|
|
|
throw new \RuntimeException("file exists and it is not a fifo file"); |
46
|
|
|
} |
47
|
|
|
|
48
|
12 |
|
$this->filename = $filename; |
49
|
12 |
|
$this->block = $block; |
50
|
12 |
|
} |
51
|
|
|
|
52
|
6 |
|
public function setBlock($block = true) |
53
|
|
|
{ |
54
|
6 |
|
if (is_resource($this->read)) { |
55
|
|
|
$set = stream_set_blocking($this->read, $block); |
56
|
|
|
if (!$set) { |
57
|
|
|
throw new \RuntimeException("stream_set_blocking failed"); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
if (is_resource($this->write)) { |
62
|
|
|
$set = stream_set_blocking($this->write, $block); |
63
|
|
|
if (!$set) { |
64
|
|
|
throw new \RuntimeException("stream_set_blocking failed"); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$this->block = $block; |
69
|
6 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $size |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
9 |
View Code Duplication |
public function read($size = 1024) |
|
|
|
|
76
|
|
|
{ |
77
|
9 |
|
if (!is_resource($this->read)) { |
78
|
9 |
|
$this->read = fopen($this->filename, 'r+'); |
79
|
9 |
|
if (!is_resource($this->read)) { |
80
|
|
|
throw new \RuntimeException("open file failed"); |
81
|
|
|
} |
82
|
9 |
|
if (!$this->block) { |
83
|
6 |
|
$set = stream_set_blocking($this->read, false); |
84
|
6 |
|
if (!$set) { |
85
|
|
|
throw new \RuntimeException("stream_set_blocking failed"); |
86
|
|
|
} |
87
|
6 |
|
} |
88
|
9 |
|
} |
89
|
|
|
|
90
|
9 |
|
return fread($this->read, $size); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $message |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
6 |
View Code Duplication |
public function write($message) |
|
|
|
|
98
|
|
|
{ |
99
|
6 |
|
if (!is_resource($this->write)) { |
100
|
6 |
|
$this->write = fopen($this->filename, 'w+'); |
101
|
6 |
|
if (!is_resource($this->write)) { |
102
|
|
|
throw new \RuntimeException("open file failed"); |
103
|
|
|
} |
104
|
6 |
|
if (!$this->block) { |
105
|
6 |
|
$set = stream_set_blocking($this->write, false); |
106
|
6 |
|
if (!$set) { |
107
|
|
|
throw new \RuntimeException("stream_set_blocking failed"); |
108
|
|
|
} |
109
|
6 |
|
} |
110
|
6 |
|
} |
111
|
|
|
|
112
|
6 |
|
return fwrite($this->write, $message); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
*/ |
118
|
12 |
|
public function close() |
119
|
|
|
{ |
120
|
12 |
|
if (is_resource($this->read)) { |
121
|
9 |
|
fclose($this->read); |
122
|
9 |
|
} |
123
|
12 |
|
if (is_resource($this->write)) { |
124
|
6 |
|
fclose($this->write); |
125
|
6 |
|
} |
126
|
12 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
*/ |
131
|
12 |
|
public function __destruct() |
132
|
|
|
{ |
133
|
12 |
|
$this->close(); |
134
|
12 |
|
} |
135
|
|
|
|
136
|
|
|
public function remove() |
137
|
|
|
{ |
138
|
|
|
return unlink($this->filename); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|