1
|
|
|
<?php namespace Comodojo\Extender\Components; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
4
|
|
|
use \Comodojo\Foundation\Validation\DataFilter; |
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Comodojo Extender |
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
10
|
|
|
* @license MIT |
11
|
|
|
* |
12
|
|
|
* LICENSE: |
13
|
|
|
* |
14
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
19
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
20
|
|
|
* THE SOFTWARE. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
class Ipc { |
24
|
|
|
|
25
|
|
|
const READER = 0; |
26
|
|
|
|
27
|
|
|
const WRITER = 1; |
28
|
|
|
|
29
|
|
|
private $bytes; |
30
|
|
|
|
31
|
|
|
private $ipc = []; |
32
|
|
|
|
33
|
|
|
public function __construct(Configuration $configuration) { |
34
|
|
|
|
35
|
|
|
$this->bytes = DataFilter::filterInteger( |
36
|
|
|
$configuration->get('child-max-result-bytes'), |
37
|
|
|
1024, |
|
|
|
|
38
|
|
|
PHP_INT_MAX, |
|
|
|
|
39
|
|
|
16384 |
|
|
|
|
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function init($uid) { |
45
|
|
|
|
46
|
|
|
$this->ipc[$uid] = []; |
47
|
|
|
|
48
|
|
|
$socket = socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $this->ipc[$uid]); |
49
|
|
|
|
50
|
|
|
if ( $socket == false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error())); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $socket; |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function read($uid) { |
57
|
|
|
|
58
|
|
|
$reader = $this->ipc[$uid][self::READER]; |
59
|
|
|
|
60
|
|
|
$result = socket_read($reader, $this->bytes, PHP_BINARY_READ); |
61
|
|
|
|
62
|
|
|
if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($reader))); |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function write($uid, $data) { |
69
|
|
|
|
70
|
|
|
$writer = $this->ipc[$uid][self::WRITER]; |
71
|
|
|
|
72
|
|
|
$result = socket_write($writer, $data, strlen($data)); |
73
|
|
|
|
74
|
|
|
if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($writer))); |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function close($uid, $handler) { |
81
|
|
|
|
82
|
|
|
socket_close($this->ipc[$uid][$handler]); |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function hang($uid) { |
87
|
|
|
|
88
|
|
|
socket_close($this->ipc[$uid][self::READER]); |
89
|
|
|
socket_close($this->ipc[$uid][self::WRITER]); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function free() { |
94
|
|
|
|
95
|
|
|
$this->ipc = []; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|