note 116422 modified in class.splqueue by salathe
| From: | salathe@php.net | Date: | Fri, 02 Jan 2015 11:35:16 +0000 |
| Subject: | note 116422 modified in class.splqueue by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201339@lists.php.net to get a copy of this message | ||
You can use shift/unshift and push/pop to dequeue/undequeue and queue/unqueue respectively. Really
handy for those applications that use sockets where you might not know you can't send data
until you attempt to.
for example, this is a function for an application that will un-dequeue the remainder of the data if
socket_write indicated it did not write the entire contents of the provided data.
<?php
function processSendQueue($socket, $sendQueue) {
while (!$sendQueue->isEmpty()) {
//shift() is the same as dequeue()
$senditem = $sendQueue->shift();
//returns the number of bytes written.
$rtn = socket_write($socket, $senditem);
if ($rtn === false) {
$sendQueue->unshift($senditem);
throw new exception("send error: " . socket_last_error($socket));
return;
}
if ($rtn < strlen($senditem) {
$sendQueue->unshift(substr($senditem, $rtn);
break;
}
}
}
?>
--was--
To expand on the comment about push and pop:
You can use shift/unshift and push/pop to dequeue/undequeue and queue/unqueue respectively. Really
handy for those applications that use sockets where you might not know you can't send data
until you attempt to.
for example, this is a function for an application that will un-dequeue the remainder of the data if
socket_write indicated it did not write the entire contents of the provided data.
<?php
function processSendQueue($socket, $sendQueue) {
while (!$sendQueue->isEmpty()) {
//shift() is the same as dequeue()
$senditem = $sendQueue->shift();
//returns the number of bytes written.
$rtn = socket_write($socket, $senditem);
if ($rtn === false) {
$sendQueue->unshift($senditem);
throw new exception("send error: " . socket_last_error($socket));
return;
}
if ($rtn < strlen($senditem) {
$sendQueue->unshift(substr($senditem, $rtn);
break;
}
}
}
?>
http://php.net/manual/en/class.splqueue.php