add lock mechanism
This commit is contained in:
parent
1ecea91dac
commit
fc56154edb
|
@ -25,6 +25,11 @@ class Server implements MessageComponentInterface
|
|||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $locks = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
|
@ -109,4 +114,21 @@ class Server implements MessageComponentInterface
|
|||
{
|
||||
$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) {
|
||||
$tmpFilename = sprintf('%s/remote_i3wm_ws_screenshot.lock', sys_get_temp_dir());
|
||||
$qualities = ['hq' => 95, 'lq' => 5];
|
||||
|
||||
if (file_exists($tmpFilename) && time() - filemtime($tmpFilename) < 10) {
|
||||
$server->addMessageHandler('screenshot', function (ConnectionInterface $from, array $data) use ($shell, $messageOutput, $server) {
|
||||
if ($server->hasLock('screenshot')) {
|
||||
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']));
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue