add lock mechanism
This commit is contained in:
parent
1ecea91dac
commit
fc56154edb
|
@ -25,6 +25,11 @@ class Server implements MessageComponentInterface
|
||||||
*/
|
*/
|
||||||
protected $output;
|
protected $output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $locks = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
|
@ -109,4 +114,21 @@ class Server implements MessageComponentInterface
|
||||||
{
|
{
|
||||||
$conn->close();
|
$conn->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addLock($key)
|
||||||
|
{
|
||||||
|
$this->locks[$key] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasLock($key)
|
||||||
|
{
|
||||||
|
return isset($this->locks[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeLock($key)
|
||||||
|
{
|
||||||
|
if ($this->hasLock($key)) {
|
||||||
|
unset($this->locks[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,16 +167,14 @@ $server->addMessageHandler('text', function (ConnectionInterface $from, array $d
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput) {
|
$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput, $server) {
|
||||||
$tmpFilename = sprintf('%s/remote_i3wm_ws_screenshot.lock', sys_get_temp_dir());
|
if ($server->hasLock('screenshot')) {
|
||||||
$qualities = ['hq' => 95, 'lq' => 5];
|
|
||||||
|
|
||||||
if (file_exists($tmpFilename) && time() - filemtime($tmpFilename) < 10) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
touch($tmpFilename);
|
$server->addLock('screenshot');
|
||||||
|
|
||||||
|
$qualities = ['hq' => 95, 'lq' => 5];
|
||||||
$content = $shell->exec(sprintf('import -window root -quality %d -display :0 jpeg:-', $qualities[$data['quality'] ?? 'hq']));
|
$content = $shell->exec(sprintf('import -window root -quality %d -display :0 jpeg:-', $qualities[$data['quality'] ?? 'hq']));
|
||||||
|
|
||||||
if (!empty($content)) {
|
if (!empty($content)) {
|
||||||
|
@ -188,7 +186,7 @@ $server->addMessageHandler('screenshot', function (ConnectionInterface $from, ar
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
unlink($tmpFilename);
|
$server->removeLock('screenshot');
|
||||||
});
|
});
|
||||||
|
|
||||||
$server->addMessageHandler('messages', function (ConnectionInterface $from, array $data) use ($server, $serverOutput) {
|
$server->addMessageHandler('messages', function (ConnectionInterface $from, array $data) use ($server, $serverOutput) {
|
||||||
|
|
Loading…
Reference in a new issue