forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.8 KiB
75 lines
1.8 KiB
<?php |
|
|
|
/* |
|
Linha de comando |
|
ipcs -> mostra blocos de memoria alocados |
|
ipcrm -m "shmid" -> remove o bloco de memória referenciado por shmid |
|
*/ |
|
|
|
class ShareResourse { |
|
|
|
public $nPermissions; |
|
public $nKey; |
|
public $nBytesMemorySize; |
|
public $nShmId; |
|
public $nVarKey; |
|
public $mVar; |
|
private $file; |
|
|
|
public function __construct($file = '') { |
|
$this->file = !$file ? '/tmp/sharadememory.txt' : $file; |
|
$this->nKey = $this->GetKey(); |
|
$this->nBytesMemorySize = 50000; |
|
$this->nPermissions = 0666; |
|
$this->attachToSegment(); |
|
} |
|
|
|
private function GetKey() { |
|
if (!file_exists($this->file)) { |
|
touch($this->file); |
|
} |
|
return ftok($this->file, 'R'); |
|
} |
|
|
|
public function attachToSegment() { |
|
$this->nShmId = shm_attach($this->nKey, $this->nBytesMemorySize, $this->nPermissions); |
|
} |
|
|
|
public function detachFromSegment() { |
|
shm_detach($this->nShmId); |
|
} |
|
|
|
public function removeSegment() { |
|
shm_remove($this->nShmId); |
|
} |
|
|
|
public function getVarFromSegment() { |
|
return shm_get_var($this->nShmId, $this->nVarKey); |
|
} |
|
|
|
/** |
|
* Puts a PHP variable into shared memory (or updates an existing one for the given variable key). |
|
* |
|
* @return boolean returns TRUE on success/FALSE on error |
|
*/ |
|
public function putVarToSegment($var) { |
|
/* |
|
* Transforma boolean em inteiro. |
|
*/ |
|
if (is_bool($var)) { |
|
if ($var) |
|
$var = 1; |
|
else |
|
$var = 0; |
|
} |
|
return shm_put_var($this->nShmId, $this->nVarKey, $var); |
|
} |
|
|
|
public function removeVarFromSgement() { |
|
shm_remove_var($this->nShmId, $this->nVarKey); |
|
} |
|
|
|
} |
|
|
|
// end class |
|
?>
|