|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\IPCManager; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
|
5
|
|
|
use PHPDaemon\Core\Timer; |
|
6
|
|
|
use PHPDaemon\Network\Connection; |
|
7
|
|
|
|
|
8
|
|
|
class WorkerConnection extends Connection |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var null */ |
|
11
|
|
|
protected $timeout = null; |
|
12
|
|
|
/** @var int */ |
|
13
|
|
|
protected $lowMark = 4; // initial value of the minimal amout of bytes in buffer |
|
14
|
|
|
/** @var int */ |
|
15
|
|
|
protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer |
|
16
|
|
|
/** |
|
17
|
|
|
* @TODO DESCR |
|
18
|
|
|
*/ |
|
19
|
|
|
const STATE_CONTENT = 1; |
|
20
|
|
|
/** @var */ |
|
21
|
|
|
protected $packetLength; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @TODO DESCR |
|
25
|
|
|
*/ |
|
26
|
|
|
public function onReady() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->sendPacket([ |
|
29
|
|
|
'op' => 'start', |
|
30
|
|
|
'pid' => Daemon::$process->getPid(), |
|
31
|
|
|
'workerId' => Daemon::$process->getId() |
|
32
|
|
|
]); |
|
33
|
|
|
parent::onReady(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $p |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function onPacket($p) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($p['op'] === 'spawnInstance') { |
|
42
|
|
|
$fullname = $p['appfullname']; |
|
43
|
|
|
$fullname = str_replace('-', ':', $fullname); |
|
44
|
|
|
if (mb_orig_strpos($fullname, ':') === false) { |
|
45
|
|
|
$fullname .= ':'; |
|
46
|
|
|
} |
|
47
|
|
|
list($app, $name) = explode(':', $fullname, 2); |
|
48
|
|
|
Daemon::$appResolver->getInstance($app, $name, true, true); |
|
49
|
|
|
} elseif ($p['op'] === 'importFile') { |
|
50
|
|
|
if (!Daemon::$config->autoreimport->value) { |
|
51
|
|
|
Daemon::$process->gracefulRestart(); |
|
|
|
|
|
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
$path = $p['path']; |
|
55
|
|
|
Timer::add(function ($event) use ($path) { |
|
56
|
|
|
if (Daemon::supported(Daemon::SUPPORT_RUNKIT_IMPORT)) { |
|
57
|
|
|
//Daemon::log('--start runkit_import('.$path.')'); |
|
58
|
|
|
runkit_import($path, RUNKIT_IMPORT_FUNCTIONS | RUNKIT_IMPORT_CLASSES | RUNKIT_IMPORT_OVERRIDE); |
|
59
|
|
|
//Daemon::log('--end runkit_import('.$path.')'); |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->appInstance->log('Cannot import \'' . $path . '\': runkit_import is not callable.'); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$event->finish(); |
|
65
|
|
|
}, 5); |
|
66
|
|
|
} elseif ($p['op'] === 'call') { |
|
67
|
|
|
if (mb_orig_strpos($p['appfullname'], ':') === false) { |
|
68
|
|
|
$p['appfullname'] .= ':'; |
|
69
|
|
|
} |
|
70
|
|
|
list($app, $name) = explode(':', $p['appfullname'], 2); |
|
71
|
|
|
if ($app = Daemon::$appResolver->getInstance($app, $name)) { |
|
72
|
|
|
$app->RPCall($p['method'], $p['args']); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @TODO DESCR |
|
79
|
|
|
* @param $p |
|
80
|
|
|
*/ |
|
81
|
|
|
public function sendPacket($p) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($p === null) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
$data = \igbinary_serialize($p); |
|
87
|
|
|
$this->write(pack('N', mb_orig_strlen($data)) . $data); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @TODO DESCR |
|
92
|
|
|
* Called when new data received. |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function onRead() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
start: |
|
98
|
|
|
if ($this->state === self::STATE_ROOT) { |
|
99
|
|
|
if (false === ($r = $this->readExact(4))) { |
|
100
|
|
|
return; // not ready yet |
|
101
|
|
|
} |
|
102
|
|
|
$u = unpack('N', $r); |
|
103
|
|
|
$this->packetLength = $u[1]; |
|
104
|
|
|
$this->state = self::STATE_CONTENT; |
|
105
|
|
|
} |
|
106
|
|
|
if ($this->state === self::STATE_CONTENT) { |
|
107
|
|
|
if (false === ($packet = $this->readExact($this->packetLength))) { |
|
108
|
|
|
$this->setWatermark($this->packetLength, $this->packetLength); |
|
109
|
|
|
return; // not ready yet |
|
110
|
|
|
} |
|
111
|
|
|
$this->setWatermark(4, 0xFFFF); |
|
112
|
|
|
$this->state = self::STATE_ROOT; |
|
113
|
|
|
$this->onPacket(\igbinary_unserialize($packet)); |
|
114
|
|
|
} |
|
115
|
|
|
goto start; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: