public function __construct($file, $scope = self::SCOPE_GLOBAL, $size = 65535) { if (!$file) { throw new \BadArgumentException("Expected filename to Queue constructor"); } $mf = $file . ($scope == self::SCOPE_PID ? '.' . getmypid() : ''); if (!file_exists($mf)) { touch($mf); } $this->key = fileinode($mf); if (!msg_queue_exists($this->key)) { debug("Creating IPC queue (0x%x) for %s", $this->key, $mf); $this->queue = msg_get_queue($this->key, 0600); } else { debug("Reopening IPC queue (0x%x) for %s", $this->key, $mf); $this->queue = msg_get_queue($this->key, 0600); } }
<?php $s_msg_qnum = "msg_qnum"; $filename = tempnam('/tmp', 'vmmsgqueue'); $token = ftok($filename, "a"); if (msg_queue_exists($token)) { echo "queue exists\n"; exit(1); } $queue = msg_get_queue($token); var_dump(msg_queue_exists($token)); $pid = pcntl_fork(); if ($pid == 0) { $q = msg_get_queue($token); msg_send($q, 2, "start"); msg_receive($q, 1, $type, 100, $msg); msg_send($q, 2, $msg); // echo exit(0); } msg_receive($queue, 2, $type, 100, $msg); var_dump($msg); msg_send($queue, 1, "ok"); msg_receive($queue, 2, $type, 100, $msg); var_dump($msg); $ret = @msg_send($queue, 0, 'msg', false, false, $s_error_code); var_dump($ret); var_dump(22 === $s_error_code); // 22 - invalid argument $ret = msg_receive($queue, 0, $type, 100, $msg, false, MSG_IPC_NOWAIT, $r_error_code); var_dump($ret);
/** * 判断一个队列是否存在 * @param $key * @return bool */ public function queue_exists($key) { return \msg_queue_exists($key); }
public function non_blocking_out() { if ($this->closed || !msg_queue_exists($this->key)) { return [self::CLOSED, null]; } $msgtype = null; $ipcmsg = null; $error = null; msg_receive($this->ipc, 1, $msgtype, 1 * PHP_INT_LENGTH + 1, $ipcmsg, false, MSG_IPC_NOWAIT, $error); if (MSG_ENOMSG === $error) { return [false, null]; } --$this->msg_count; $shm = new Message($ipcmsg); $ret = $shm->fetch(); return [true, $ret]; }
/** * Delete message from queue * * @param int $pid * @param callable $callback function handler * @return boolean */ public function delete($pid = null, callable $callback = null) { $this->queuePid = is_null($pid) === false ? $pid : $this->queuePid; if (msg_queue_exists($this->queuePid) === true) { msg_remove_queue(msg_get_queue($this->queuePid)); return true; } return false; }
/** * Unserializes a serialized semaphore. * * @param string $serialized The serialized semaphore. */ public function unserialize($serialized) { // Get the semaphore key and attempt to re-connect to the semaphore in memory. list($this->key, $this->maxLocks) = unserialize($serialized); if (msg_queue_exists($this->key)) { $this->queue = msg_get_queue($this->key); } }
if ($index < 3) { continue; } if (isset($deviceOptionList[$currentCommand])) { $syncList[$deviceOptionList[$currentCommand]] = $deviceCommandList[$deviceCommand][$index]; } } unset($index, $currentCommand); } $syncList = array_reverse($syncList); /* Establish connection to message queue and send syncList */ if (($msqKey = ftok("{$tempLocation}/" . $applicationServer . ".msq", 'B')) == -1) { echo 'Location does not exist, could not create token: terminating program.'; return -1; } if (!msg_queue_exists($msqKey)) { echo 'Message queue does not exist: terminating program.'; exit(-1); } $msqId = msg_get_queue($msqKey, 0666); foreach ($syncList as $currentCategory => $currentCommand) { $msqMessage = "{$currentCategory}={$currentCommand}"; if (!msg_send($msqId, 1, $msqMessage, false, true, $msqErr)) { echo "Failed to send message to queue: {$msqErr}\n"; } } unset($currentCategory, $currentCommand); if ($setVolume) { exec('sudo amixer -c Dummy sset Master ' . $deviceCommandList[$deviceCommand][2] . '% &'); } /* Sets a delay and gives the amplifier to process changes and this script to return
<?php $id = ftok(__FILE__, 'r'); msg_remove_queue(msg_get_queue($id, 0600)); var_dump(msg_queue_exists($id)); $res = msg_get_queue($id, 0600); var_dump($res); var_dump(msg_queue_exists($id)); var_dump(msg_remove_queue($res)); var_dump(msg_queue_exists($id)); echo "Done\n";
$json = json_decode(file_get_contents($file)); foreach ($json->queue as $queue) { ?> <tr> <td><?php echo $queue; ?> </td> <td> <?php if (msg_queue_exists($queue)) { echo "ON"; } else { echo "OFF"; } ?> </td> <td> <?php if (msg_queue_exists($queue)) { $openqueue = msg_get_queue($queue); echo msg_stat_queue($openqueue)['msg_qnum']; } else { echo "---"; } ?> </td> </tr> <?php }
<?php $tests = array(null, 'foo'); foreach ($tests as $q) { if ($q === null) { do { $id = ftok(__FILE__, chr(mt_rand(0, 255))); } while (msg_queue_exists($id)); $q = msg_get_queue($id) or die("Failed to create queue"); msg_remove_queue($q) or die("Failed to close queue"); } echo "Using '{$q}' as queue resource:\n"; $errno = 0; var_dump(msg_set_queue($q, array('msg_qbytes' => 1))); var_dump(msg_stat_queue($q)); var_dump(msg_receive($q, 0, $null, 1, $msg, true, 0, $errno)); var_dump($errno != 0); // again, but triggering an E_WARNING var_dump(msg_receive($q, 0, $null, 0, $msg)); var_dump(msg_send($q, 1, 'foo', true, true, $errno)); var_dump($errno != 0); } echo "Done\n";
protected function initQueue() { $queueExists = msg_queue_exists($this->queueKey); if (!$queueExists && !$this->createQueue) { throw new \Exception('Specified Queue does not exist, but options require it does: ' . $this->queueKey); } else { if ($queueExists && $this->createQueue && $this->requireQueueCreate) { throw new \Exception('Specified Queue already exists, but options require it does not: ' . $this->queueKey); } } $this->queue = msg_get_queue($this->queueKey, $this->perms); if (!is_resource($this->queue)) { throw new \Exception('Unable to attach to specified queue: ' . $this->queueKey); } }
/** * @brief * * @param * @return */ static function queueExists($queue) { if (!file_exists($queue)) { return false; } $this->ipckey = ftok($queue, 'q'); if (function_exists("msg_queue_exists")) { return msg_queue_exists($key); } else { $aQueues = array(); exec("ipcs -q | grep \"^[0-9]\" | cut -d \" \" -f 1", $aQueues); return in_array($key, $aQueues); } }