Ejemplo n.º 1
0
 /**
  * Callback after content body has been completely received.
  */
 protected function onBodyComplete()
 {
     if ($this->returnFrame) {
         $content = $this->bodyBuffer->consume($this->bodyBuffer->getLength());
         $message = new Message(null, null, false, $this->returnFrame->exchange, $this->returnFrame->routingKey, $this->headerFrame->toArray(), $content);
         foreach ($this->returnCallbacks as $callback) {
             $callback($message, $this->returnFrame);
         }
         $this->returnFrame = null;
         $this->headerFrame = null;
     } elseif ($this->deliverFrame) {
         $content = $this->bodyBuffer->consume($this->bodyBuffer->getLength());
         if (isset($this->deliverCallbacks[$this->deliverFrame->consumerTag])) {
             $message = new Message($this->deliverFrame->consumerTag, $this->deliverFrame->deliveryTag, $this->deliverFrame->redelivered, $this->deliverFrame->exchange, $this->deliverFrame->routingKey, $this->headerFrame->toArray(), $content);
             $callback = $this->deliverCallbacks[$this->deliverFrame->consumerTag];
             $callback($message, $this, $this->client);
         }
         $this->deliverFrame = null;
         $this->headerFrame = null;
     } elseif ($this->getOkFrame) {
         $content = $this->bodyBuffer->consume($this->bodyBuffer->getLength());
         // deferred has to be first nullified and then resolved, otherwise results in race condition
         $deferred = $this->getDeferred;
         $this->getDeferred = null;
         $deferred->resolve(new Message(null, $this->getOkFrame->deliveryTag, $this->getOkFrame->redelivered, $this->getOkFrame->exchange, $this->getOkFrame->routingKey, $this->headerFrame->toArray(), $content));
         $this->getOkFrame = null;
         $this->headerFrame = null;
     } else {
         throw new \LogicException("Either return or deliver frame has to be handled here.");
     }
 }
Ejemplo n.º 2
0
 /**
  * Writes data from {@link writeBuffer} to stream.
  */
 protected function write()
 {
     if (($written = @fwrite($this->getStream(), $this->writeBuffer->read($this->writeBuffer->getLength()))) === false) {
         throw new ClientException("Could not write data to socket.");
     }
     if ($written === 0) {
         throw new ClientException("Broken pipe or closed connection.");
     }
     fflush($this->getStream());
     // flush internal PHP buffers
     $this->writeBuffer->discard($written);
     $this->lastWrite = microtime(true);
 }
Ejemplo n.º 3
0
 /**
  * Callback after content body has been completely received.
  */
 protected function onBodyComplete()
 {
     if ($this->returnFrame) {
         // TODO
     } elseif ($this->deliverFrame) {
         $content = $this->bodyBuffer->consume($this->bodyBuffer->getLength());
         if (isset($this->deliverCallbacks[$this->deliverFrame->consumerTag])) {
             $msg = new Message($this->deliverFrame->consumerTag, $this->deliverFrame->deliveryTag, $this->deliverFrame->redelivered, $this->deliverFrame->exchange, $this->deliverFrame->routingKey, $this->headerFrame->toArray(), $content);
             $callback = $this->deliverCallbacks[$this->deliverFrame->consumerTag];
             $callback($msg, $this, $this->client);
         }
         $this->deliverFrame = null;
         $this->headerFrame = null;
     } else {
         throw new \LogicException("Either return or deliver frame has to be handled here.");
     }
 }
Ejemplo n.º 4
0
 public function publish($channel, $body, array $headers, $exchange = '', $routingKey = '', $mandatory = false, $immediate = false)
 {
     $buffer = $this->getWriteBuffer();
     $ck = serialize([$channel, $headers, $exchange, $routingKey, $mandatory, $immediate]);
     $c = isset($this->cache[$ck]) ? $this->cache[$ck] : null;
     $flags = 0;
     $off0 = 0;
     $len0 = 0;
     $off1 = 0;
     $len1 = 0;
     $contentTypeLength = null;
     $contentType = null;
     $contentEncodingLength = null;
     $contentEncoding = null;
     $headersBuffer = null;
     $deliveryMode = null;
     $priority = null;
     $correlationIdLength = null;
     $correlationId = null;
     $replyToLength = null;
     $replyTo = null;
     $expirationLength = null;
     $expiration = null;
     $messageIdLength = null;
     $messageId = null;
     $timestamp = null;
     $typeLength = null;
     $type = null;
     $userIdLength = null;
     $userId = null;
     $appIdLength = null;
     $appId = null;
     $clusterIdLength = null;
     $clusterId = null;
     if ($c) {
         $buffer->append($c[0]);
     } else {
         $off0 = $buffer->getLength();
         $buffer->appendUint8(1);
         $buffer->appendUint16($channel);
         $buffer->appendUint32(9 + strlen($exchange) + strlen($routingKey));
         $buffer->appendUint16(60);
         $buffer->appendUint16(40);
         $buffer->appendInt16(0);
         $buffer->appendUint8(strlen($exchange));
         $buffer->append($exchange);
         $buffer->appendUint8(strlen($routingKey));
         $buffer->append($routingKey);
         $this->getWriter()->appendBits([$mandatory, $immediate], $buffer);
         $buffer->appendUint8(206);
         $s = 14;
         if (isset($headers['content-type'])) {
             $flags |= 32768;
             $contentType = $headers['content-type'];
             $s += 1;
             $s += $contentTypeLength = strlen($contentType);
             unset($headers['content-type']);
         }
         if (isset($headers['content-encoding'])) {
             $flags |= 16384;
             $contentEncoding = $headers['content-encoding'];
             $s += 1;
             $s += $contentEncodingLength = strlen($contentEncoding);
             unset($headers['content-encoding']);
         }
         if (isset($headers['delivery-mode'])) {
             $flags |= 4096;
             $deliveryMode = $headers['delivery-mode'];
             $s += 1;
             unset($headers['delivery-mode']);
         }
         if (isset($headers['priority'])) {
             $flags |= 2048;
             $priority = $headers['priority'];
             $s += 1;
             unset($headers['priority']);
         }
         if (isset($headers['correlation-id'])) {
             $flags |= 1024;
             $correlationId = $headers['correlation-id'];
             $s += 1;
             $s += $correlationIdLength = strlen($correlationId);
             unset($headers['correlation-id']);
         }
         if (isset($headers['reply-to'])) {
             $flags |= 512;
             $replyTo = $headers['reply-to'];
             $s += 1;
             $s += $replyToLength = strlen($replyTo);
             unset($headers['reply-to']);
         }
         if (isset($headers['expiration'])) {
             $flags |= 256;
             $expiration = $headers['expiration'];
             $s += 1;
             $s += $expirationLength = strlen($expiration);
             unset($headers['expiration']);
         }
         if (isset($headers['message-id'])) {
             $flags |= 128;
             $messageId = $headers['message-id'];
             $s += 1;
             $s += $messageIdLength = strlen($messageId);
             unset($headers['message-id']);
         }
         if (isset($headers['timestamp'])) {
             $flags |= 64;
             $timestamp = $headers['timestamp'];
             $s += 8;
             unset($headers['timestamp']);
         }
         if (isset($headers['type'])) {
             $flags |= 32;
             $type = $headers['type'];
             $s += 1;
             $s += $typeLength = strlen($type);
             unset($headers['type']);
         }
         if (isset($headers['user-id'])) {
             $flags |= 16;
             $userId = $headers['user-id'];
             $s += 1;
             $s += $userIdLength = strlen($userId);
             unset($headers['user-id']);
         }
         if (isset($headers['app-id'])) {
             $flags |= 8;
             $appId = $headers['app-id'];
             $s += 1;
             $s += $appIdLength = strlen($appId);
             unset($headers['app-id']);
         }
         if (isset($headers['cluster-id'])) {
             $flags |= 4;
             $clusterId = $headers['cluster-id'];
             $s += 1;
             $s += $clusterIdLength = strlen($clusterId);
             unset($headers['cluster-id']);
         }
         if (!empty($headers)) {
             $flags |= 8192;
             $this->getWriter()->appendTable($headers, $headersBuffer = new Buffer());
             $s += $headersBuffer->getLength();
         }
         $buffer->appendUint8(2);
         $buffer->appendUint16($channel);
         $buffer->appendUint32($s);
         $buffer->appendUint16(60);
         $buffer->appendUint16(0);
         $len0 = $buffer->getLength() - $off0;
     }
     $buffer->appendUint64(strlen($body));
     if ($c) {
         $buffer->append($c[1]);
     } else {
         $off1 = $buffer->getLength();
         $buffer->appendUint16($flags);
         if ($flags & 32768) {
             $buffer->appendUint8($contentTypeLength);
             $buffer->append($contentType);
         }
         if ($flags & 16384) {
             $buffer->appendUint8($contentEncodingLength);
             $buffer->append($contentEncoding);
         }
         if ($flags & 8192) {
             $buffer->append($headersBuffer);
         }
         if ($flags & 4096) {
             $buffer->appendUint8($deliveryMode);
         }
         if ($flags & 2048) {
             $buffer->appendUint8($priority);
         }
         if ($flags & 1024) {
             $buffer->appendUint8($correlationIdLength);
             $buffer->append($correlationId);
         }
         if ($flags & 512) {
             $buffer->appendUint8($replyToLength);
             $buffer->append($replyTo);
         }
         if ($flags & 256) {
             $buffer->appendUint8($expirationLength);
             $buffer->append($expiration);
         }
         if ($flags & 128) {
             $buffer->appendUint8($messageIdLength);
             $buffer->append($messageId);
         }
         if ($flags & 64) {
             $this->getWriter()->appendTimestamp($timestamp, $buffer);
         }
         if ($flags & 32) {
             $buffer->appendUint8($typeLength);
             $buffer->append($type);
         }
         if ($flags & 16) {
             $buffer->appendUint8($userIdLength);
             $buffer->append($userId);
         }
         if ($flags & 8) {
             $buffer->appendUint8($appIdLength);
             $buffer->append($appId);
         }
         if ($flags & 4) {
             $buffer->appendUint8($clusterIdLength);
             $buffer->append($clusterId);
         }
         $buffer->appendUint8(206);
         $len1 = $buffer->getLength() - $off1;
     }
     if (!$c) {
         $this->cache[$ck] = [$buffer->read($len0, $off0), $buffer->read($len1, $off1)];
         if (count($this->cache) > 100) {
             reset($this->cache);
             unset($this->cache[key($this->cache)]);
         }
     }
     for ($payloadMax = $this->getFrameMax() - 8, $i = 0, $l = strlen($body); $i < $l; $i += $payloadMax) {
         $payloadSize = $l - $i;
         if ($payloadSize > $payloadMax) {
             $payloadSize = $payloadMax;
         }
         $buffer->appendUint8(3);
         $buffer->appendUint16($channel);
         $buffer->appendUint32($payloadSize);
         $buffer->append(substr($body, $i, $payloadSize));
         $buffer->appendUint8(206);
     }
     return $this->flushWriteBuffer();
 }
Ejemplo n.º 5
0
 /**
  * Appends AMQP method frame to buffer.
  *
  * @param MethodFrame $frame
  * @param Buffer $buffer
  */
 public function appendMethodFrame(MethodFrame $frame, Buffer $buffer)
 {
     $buffer->appendUint16($frame->classId);
     $buffer->appendUint16($frame->methodId);
     if ($frame instanceof MethodConnectionStartFrame) {
         $buffer->appendUint8($frame->versionMajor);
         $buffer->appendUint8($frame->versionMinor);
         $this->appendTable($frame->serverProperties, $buffer);
         $buffer->appendUint32(strlen($frame->mechanisms));
         $buffer->append($frame->mechanisms);
         $buffer->appendUint32(strlen($frame->locales));
         $buffer->append($frame->locales);
     } elseif ($frame instanceof MethodConnectionStartOkFrame) {
         $this->appendTable($frame->clientProperties, $buffer);
         $buffer->appendUint8(strlen($frame->mechanism));
         $buffer->append($frame->mechanism);
         $buffer->appendUint32(strlen($frame->response));
         $buffer->append($frame->response);
         $buffer->appendUint8(strlen($frame->locale));
         $buffer->append($frame->locale);
     } elseif ($frame instanceof MethodConnectionSecureFrame) {
         $buffer->appendUint32(strlen($frame->challenge));
         $buffer->append($frame->challenge);
     } elseif ($frame instanceof MethodConnectionSecureOkFrame) {
         $buffer->appendUint32(strlen($frame->response));
         $buffer->append($frame->response);
     } elseif ($frame instanceof MethodConnectionTuneFrame) {
         $buffer->appendInt16($frame->channelMax);
         $buffer->appendInt32($frame->frameMax);
         $buffer->appendInt16($frame->heartbeat);
     } elseif ($frame instanceof MethodConnectionTuneOkFrame) {
         $buffer->appendInt16($frame->channelMax);
         $buffer->appendInt32($frame->frameMax);
         $buffer->appendInt16($frame->heartbeat);
     } elseif ($frame instanceof MethodConnectionOpenFrame) {
         $buffer->appendUint8(strlen($frame->virtualHost));
         $buffer->append($frame->virtualHost);
         $buffer->appendUint8(strlen($frame->capabilities));
         $buffer->append($frame->capabilities);
         $this->appendBits([$frame->insist], $buffer);
     } elseif ($frame instanceof MethodConnectionOpenOkFrame) {
         $buffer->appendUint8(strlen($frame->knownHosts));
         $buffer->append($frame->knownHosts);
     } elseif ($frame instanceof MethodConnectionCloseFrame) {
         $buffer->appendInt16($frame->replyCode);
         $buffer->appendUint8(strlen($frame->replyText));
         $buffer->append($frame->replyText);
         $buffer->appendInt16($frame->closeClassId);
         $buffer->appendInt16($frame->closeMethodId);
     } elseif ($frame instanceof MethodConnectionCloseOkFrame) {
     } elseif ($frame instanceof MethodConnectionBlockedFrame) {
         $buffer->appendUint8(strlen($frame->reason));
         $buffer->append($frame->reason);
     } elseif ($frame instanceof MethodConnectionUnblockedFrame) {
     } elseif ($frame instanceof MethodChannelOpenFrame) {
         $buffer->appendUint8(strlen($frame->outOfBand));
         $buffer->append($frame->outOfBand);
     } elseif ($frame instanceof MethodChannelOpenOkFrame) {
         $buffer->appendUint32(strlen($frame->channelId));
         $buffer->append($frame->channelId);
     } elseif ($frame instanceof MethodChannelFlowFrame) {
         $this->appendBits([$frame->active], $buffer);
     } elseif ($frame instanceof MethodChannelFlowOkFrame) {
         $this->appendBits([$frame->active], $buffer);
     } elseif ($frame instanceof MethodChannelCloseFrame) {
         $buffer->appendInt16($frame->replyCode);
         $buffer->appendUint8(strlen($frame->replyText));
         $buffer->append($frame->replyText);
         $buffer->appendInt16($frame->closeClassId);
         $buffer->appendInt16($frame->closeMethodId);
     } elseif ($frame instanceof MethodChannelCloseOkFrame) {
     } elseif ($frame instanceof MethodAccessRequestFrame) {
         $buffer->appendUint8(strlen($frame->realm));
         $buffer->append($frame->realm);
         $this->appendBits([$frame->exclusive, $frame->passive, $frame->active, $frame->write, $frame->read], $buffer);
     } elseif ($frame instanceof MethodAccessRequestOkFrame) {
         $buffer->appendInt16($frame->reserved1);
     } elseif ($frame instanceof MethodExchangeDeclareFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->exchangeType));
         $buffer->append($frame->exchangeType);
         $this->appendBits([$frame->passive, $frame->durable, $frame->autoDelete, $frame->internal, $frame->nowait], $buffer);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodExchangeDeclareOkFrame) {
     } elseif ($frame instanceof MethodExchangeDeleteFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $this->appendBits([$frame->ifUnused, $frame->nowait], $buffer);
     } elseif ($frame instanceof MethodExchangeDeleteOkFrame) {
     } elseif ($frame instanceof MethodExchangeBindFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->destination));
         $buffer->append($frame->destination);
         $buffer->appendUint8(strlen($frame->source));
         $buffer->append($frame->source);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
         $this->appendBits([$frame->nowait], $buffer);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodExchangeBindOkFrame) {
     } elseif ($frame instanceof MethodExchangeUnbindFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->destination));
         $buffer->append($frame->destination);
         $buffer->appendUint8(strlen($frame->source));
         $buffer->append($frame->source);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
         $this->appendBits([$frame->nowait], $buffer);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodExchangeUnbindOkFrame) {
     } elseif ($frame instanceof MethodQueueDeclareFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $this->appendBits([$frame->passive, $frame->durable, $frame->exclusive, $frame->autoDelete, $frame->nowait], $buffer);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodQueueDeclareOkFrame) {
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $buffer->appendInt32($frame->messageCount);
         $buffer->appendInt32($frame->consumerCount);
     } elseif ($frame instanceof MethodQueueBindFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
         $this->appendBits([$frame->nowait], $buffer);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodQueueBindOkFrame) {
     } elseif ($frame instanceof MethodQueuePurgeFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $this->appendBits([$frame->nowait], $buffer);
     } elseif ($frame instanceof MethodQueuePurgeOkFrame) {
         $buffer->appendInt32($frame->messageCount);
     } elseif ($frame instanceof MethodQueueDeleteFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $this->appendBits([$frame->ifUnused, $frame->ifEmpty, $frame->nowait], $buffer);
     } elseif ($frame instanceof MethodQueueDeleteOkFrame) {
         $buffer->appendInt32($frame->messageCount);
     } elseif ($frame instanceof MethodQueueUnbindFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodQueueUnbindOkFrame) {
     } elseif ($frame instanceof MethodBasicQosFrame) {
         $buffer->appendInt32($frame->prefetchSize);
         $buffer->appendInt16($frame->prefetchCount);
         $this->appendBits([$frame->global], $buffer);
     } elseif ($frame instanceof MethodBasicQosOkFrame) {
     } elseif ($frame instanceof MethodBasicConsumeFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $buffer->appendUint8(strlen($frame->consumerTag));
         $buffer->append($frame->consumerTag);
         $this->appendBits([$frame->noLocal, $frame->noAck, $frame->exclusive, $frame->nowait], $buffer);
         $this->appendTable($frame->arguments, $buffer);
     } elseif ($frame instanceof MethodBasicConsumeOkFrame) {
         $buffer->appendUint8(strlen($frame->consumerTag));
         $buffer->append($frame->consumerTag);
     } elseif ($frame instanceof MethodBasicCancelFrame) {
         $buffer->appendUint8(strlen($frame->consumerTag));
         $buffer->append($frame->consumerTag);
         $this->appendBits([$frame->nowait], $buffer);
     } elseif ($frame instanceof MethodBasicCancelOkFrame) {
         $buffer->appendUint8(strlen($frame->consumerTag));
         $buffer->append($frame->consumerTag);
     } elseif ($frame instanceof MethodBasicPublishFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
         $this->appendBits([$frame->mandatory, $frame->immediate], $buffer);
     } elseif ($frame instanceof MethodBasicReturnFrame) {
         $buffer->appendInt16($frame->replyCode);
         $buffer->appendUint8(strlen($frame->replyText));
         $buffer->append($frame->replyText);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
     } elseif ($frame instanceof MethodBasicDeliverFrame) {
         $buffer->appendUint8(strlen($frame->consumerTag));
         $buffer->append($frame->consumerTag);
         $buffer->appendInt64($frame->deliveryTag);
         $this->appendBits([$frame->redelivered], $buffer);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
     } elseif ($frame instanceof MethodBasicGetFrame) {
         $buffer->appendInt16($frame->reserved1);
         $buffer->appendUint8(strlen($frame->queue));
         $buffer->append($frame->queue);
         $this->appendBits([$frame->noAck], $buffer);
     } elseif ($frame instanceof MethodBasicGetOkFrame) {
         $buffer->appendInt64($frame->deliveryTag);
         $this->appendBits([$frame->redelivered], $buffer);
         $buffer->appendUint8(strlen($frame->exchange));
         $buffer->append($frame->exchange);
         $buffer->appendUint8(strlen($frame->routingKey));
         $buffer->append($frame->routingKey);
         $buffer->appendInt32($frame->messageCount);
     } elseif ($frame instanceof MethodBasicGetEmptyFrame) {
         $buffer->appendUint8(strlen($frame->clusterId));
         $buffer->append($frame->clusterId);
     } elseif ($frame instanceof MethodBasicAckFrame) {
         $buffer->appendInt64($frame->deliveryTag);
         $this->appendBits([$frame->multiple], $buffer);
     } elseif ($frame instanceof MethodBasicRejectFrame) {
         $buffer->appendInt64($frame->deliveryTag);
         $this->appendBits([$frame->requeue], $buffer);
     } elseif ($frame instanceof MethodBasicRecoverAsyncFrame) {
         $this->appendBits([$frame->requeue], $buffer);
     } elseif ($frame instanceof MethodBasicRecoverFrame) {
         $this->appendBits([$frame->requeue], $buffer);
     } elseif ($frame instanceof MethodBasicRecoverOkFrame) {
     } elseif ($frame instanceof MethodBasicNackFrame) {
         $buffer->appendInt64($frame->deliveryTag);
         $this->appendBits([$frame->multiple, $frame->requeue], $buffer);
     } elseif ($frame instanceof MethodTxSelectFrame) {
     } elseif ($frame instanceof MethodTxSelectOkFrame) {
     } elseif ($frame instanceof MethodTxCommitFrame) {
     } elseif ($frame instanceof MethodTxCommitOkFrame) {
     } elseif ($frame instanceof MethodTxRollbackFrame) {
     } elseif ($frame instanceof MethodTxRollbackOkFrame) {
     } elseif ($frame instanceof MethodConfirmSelectFrame) {
         $this->appendBits([$frame->nowait], $buffer);
     } elseif ($frame instanceof MethodConfirmSelectOkFrame) {
     } else {
         throw new ProtocolException('Unhandled method frame ' . get_class($frame) . '.');
     }
 }
Ejemplo n.º 6
0
 /**
  * Consumes AMQP method frame.
  *
  * @param Buffer $buffer
  * @return MethodFrame
  */
 public function consumeMethodFrame(Buffer $buffer)
 {
     $classId = $buffer->consumeUint16();
     $methodId = $buffer->consumeUint16();
     if ($classId === Constants::CLASS_CONNECTION) {
         if ($methodId === Constants::METHOD_CONNECTION_START) {
             $frame = new MethodConnectionStartFrame();
             $frame->versionMajor = $buffer->consumeUint8();
             $frame->versionMinor = $buffer->consumeUint8();
             $frame->serverProperties = $this->consumeTable($buffer);
             $frame->mechanisms = $buffer->consume($buffer->consumeUint32());
             $frame->locales = $buffer->consume($buffer->consumeUint32());
         } elseif ($methodId === Constants::METHOD_CONNECTION_START_OK) {
             $frame = new MethodConnectionStartOkFrame();
             $frame->clientProperties = $this->consumeTable($buffer);
             $frame->mechanism = $buffer->consume($buffer->consumeUint8());
             $frame->response = $buffer->consume($buffer->consumeUint32());
             $frame->locale = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_CONNECTION_SECURE) {
             $frame = new MethodConnectionSecureFrame();
             $frame->challenge = $buffer->consume($buffer->consumeUint32());
         } elseif ($methodId === Constants::METHOD_CONNECTION_SECURE_OK) {
             $frame = new MethodConnectionSecureOkFrame();
             $frame->response = $buffer->consume($buffer->consumeUint32());
         } elseif ($methodId === Constants::METHOD_CONNECTION_TUNE) {
             $frame = new MethodConnectionTuneFrame();
             $frame->channelMax = $buffer->consumeInt16();
             $frame->frameMax = $buffer->consumeInt32();
             $frame->heartbeat = $buffer->consumeInt16();
         } elseif ($methodId === Constants::METHOD_CONNECTION_TUNE_OK) {
             $frame = new MethodConnectionTuneOkFrame();
             $frame->channelMax = $buffer->consumeInt16();
             $frame->frameMax = $buffer->consumeInt32();
             $frame->heartbeat = $buffer->consumeInt16();
         } elseif ($methodId === Constants::METHOD_CONNECTION_OPEN) {
             $frame = new MethodConnectionOpenFrame();
             $frame->virtualHost = $buffer->consume($buffer->consumeUint8());
             $frame->capabilities = $buffer->consume($buffer->consumeUint8());
             list($frame->insist) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_CONNECTION_OPEN_OK) {
             $frame = new MethodConnectionOpenOkFrame();
             $frame->knownHosts = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_CONNECTION_CLOSE) {
             $frame = new MethodConnectionCloseFrame();
             $frame->replyCode = $buffer->consumeInt16();
             $frame->replyText = $buffer->consume($buffer->consumeUint8());
             $frame->closeClassId = $buffer->consumeInt16();
             $frame->closeMethodId = $buffer->consumeInt16();
         } elseif ($methodId === Constants::METHOD_CONNECTION_CLOSE_OK) {
             $frame = new MethodConnectionCloseOkFrame();
         } elseif ($methodId === Constants::METHOD_CONNECTION_BLOCKED) {
             $frame = new MethodConnectionBlockedFrame();
             $frame->reason = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_CONNECTION_UNBLOCKED) {
             $frame = new MethodConnectionUnblockedFrame();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_CHANNEL) {
         if ($methodId === Constants::METHOD_CHANNEL_OPEN) {
             $frame = new MethodChannelOpenFrame();
             $frame->outOfBand = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_CHANNEL_OPEN_OK) {
             $frame = new MethodChannelOpenOkFrame();
             $frame->channelId = $buffer->consume($buffer->consumeUint32());
         } elseif ($methodId === Constants::METHOD_CHANNEL_FLOW) {
             $frame = new MethodChannelFlowFrame();
             list($frame->active) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_CHANNEL_FLOW_OK) {
             $frame = new MethodChannelFlowOkFrame();
             list($frame->active) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_CHANNEL_CLOSE) {
             $frame = new MethodChannelCloseFrame();
             $frame->replyCode = $buffer->consumeInt16();
             $frame->replyText = $buffer->consume($buffer->consumeUint8());
             $frame->closeClassId = $buffer->consumeInt16();
             $frame->closeMethodId = $buffer->consumeInt16();
         } elseif ($methodId === Constants::METHOD_CHANNEL_CLOSE_OK) {
             $frame = new MethodChannelCloseOkFrame();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_ACCESS) {
         if ($methodId === Constants::METHOD_ACCESS_REQUEST) {
             $frame = new MethodAccessRequestFrame();
             $frame->realm = $buffer->consume($buffer->consumeUint8());
             list($frame->exclusive, $frame->passive, $frame->active, $frame->write, $frame->read) = $this->consumeBits($buffer, 5);
         } elseif ($methodId === Constants::METHOD_ACCESS_REQUEST_OK) {
             $frame = new MethodAccessRequestOkFrame();
             $frame->reserved1 = $buffer->consumeInt16();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_EXCHANGE) {
         if ($methodId === Constants::METHOD_EXCHANGE_DECLARE) {
             $frame = new MethodExchangeDeclareFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->exchangeType = $buffer->consume($buffer->consumeUint8());
             list($frame->passive, $frame->durable, $frame->autoDelete, $frame->internal, $frame->nowait) = $this->consumeBits($buffer, 5);
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_EXCHANGE_DECLARE_OK) {
             $frame = new MethodExchangeDeclareOkFrame();
         } elseif ($methodId === Constants::METHOD_EXCHANGE_DELETE) {
             $frame = new MethodExchangeDeleteFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             list($frame->ifUnused, $frame->nowait) = $this->consumeBits($buffer, 2);
         } elseif ($methodId === Constants::METHOD_EXCHANGE_DELETE_OK) {
             $frame = new MethodExchangeDeleteOkFrame();
         } elseif ($methodId === Constants::METHOD_EXCHANGE_BIND) {
             $frame = new MethodExchangeBindFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->destination = $buffer->consume($buffer->consumeUint8());
             $frame->source = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
             list($frame->nowait) = $this->consumeBits($buffer, 1);
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_EXCHANGE_BIND_OK) {
             $frame = new MethodExchangeBindOkFrame();
         } elseif ($methodId === Constants::METHOD_EXCHANGE_UNBIND) {
             $frame = new MethodExchangeUnbindFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->destination = $buffer->consume($buffer->consumeUint8());
             $frame->source = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
             list($frame->nowait) = $this->consumeBits($buffer, 1);
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_EXCHANGE_UNBIND_OK) {
             $frame = new MethodExchangeUnbindOkFrame();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_QUEUE) {
         if ($methodId === Constants::METHOD_QUEUE_DECLARE) {
             $frame = new MethodQueueDeclareFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             list($frame->passive, $frame->durable, $frame->exclusive, $frame->autoDelete, $frame->nowait) = $this->consumeBits($buffer, 5);
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_QUEUE_DECLARE_OK) {
             $frame = new MethodQueueDeclareOkFrame();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             $frame->messageCount = $buffer->consumeInt32();
             $frame->consumerCount = $buffer->consumeInt32();
         } elseif ($methodId === Constants::METHOD_QUEUE_BIND) {
             $frame = new MethodQueueBindFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
             list($frame->nowait) = $this->consumeBits($buffer, 1);
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_QUEUE_BIND_OK) {
             $frame = new MethodQueueBindOkFrame();
         } elseif ($methodId === Constants::METHOD_QUEUE_PURGE) {
             $frame = new MethodQueuePurgeFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             list($frame->nowait) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_QUEUE_PURGE_OK) {
             $frame = new MethodQueuePurgeOkFrame();
             $frame->messageCount = $buffer->consumeInt32();
         } elseif ($methodId === Constants::METHOD_QUEUE_DELETE) {
             $frame = new MethodQueueDeleteFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             list($frame->ifUnused, $frame->ifEmpty, $frame->nowait) = $this->consumeBits($buffer, 3);
         } elseif ($methodId === Constants::METHOD_QUEUE_DELETE_OK) {
             $frame = new MethodQueueDeleteOkFrame();
             $frame->messageCount = $buffer->consumeInt32();
         } elseif ($methodId === Constants::METHOD_QUEUE_UNBIND) {
             $frame = new MethodQueueUnbindFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_QUEUE_UNBIND_OK) {
             $frame = new MethodQueueUnbindOkFrame();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_BASIC) {
         if ($methodId === Constants::METHOD_BASIC_QOS) {
             $frame = new MethodBasicQosFrame();
             $frame->prefetchSize = $buffer->consumeInt32();
             $frame->prefetchCount = $buffer->consumeInt16();
             list($frame->global) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_QOS_OK) {
             $frame = new MethodBasicQosOkFrame();
         } elseif ($methodId === Constants::METHOD_BASIC_CONSUME) {
             $frame = new MethodBasicConsumeFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             $frame->consumerTag = $buffer->consume($buffer->consumeUint8());
             list($frame->noLocal, $frame->noAck, $frame->exclusive, $frame->nowait) = $this->consumeBits($buffer, 4);
             $frame->arguments = $this->consumeTable($buffer);
         } elseif ($methodId === Constants::METHOD_BASIC_CONSUME_OK) {
             $frame = new MethodBasicConsumeOkFrame();
             $frame->consumerTag = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_BASIC_CANCEL) {
             $frame = new MethodBasicCancelFrame();
             $frame->consumerTag = $buffer->consume($buffer->consumeUint8());
             list($frame->nowait) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_CANCEL_OK) {
             $frame = new MethodBasicCancelOkFrame();
             $frame->consumerTag = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_BASIC_PUBLISH) {
             $frame = new MethodBasicPublishFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
             list($frame->mandatory, $frame->immediate) = $this->consumeBits($buffer, 2);
         } elseif ($methodId === Constants::METHOD_BASIC_RETURN) {
             $frame = new MethodBasicReturnFrame();
             $frame->replyCode = $buffer->consumeInt16();
             $frame->replyText = $buffer->consume($buffer->consumeUint8());
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_BASIC_DELIVER) {
             $frame = new MethodBasicDeliverFrame();
             $frame->consumerTag = $buffer->consume($buffer->consumeUint8());
             $frame->deliveryTag = $buffer->consumeInt64();
             list($frame->redelivered) = $this->consumeBits($buffer, 1);
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_BASIC_GET) {
             $frame = new MethodBasicGetFrame();
             $frame->reserved1 = $buffer->consumeInt16();
             $frame->queue = $buffer->consume($buffer->consumeUint8());
             list($frame->noAck) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_GET_OK) {
             $frame = new MethodBasicGetOkFrame();
             $frame->deliveryTag = $buffer->consumeInt64();
             list($frame->redelivered) = $this->consumeBits($buffer, 1);
             $frame->exchange = $buffer->consume($buffer->consumeUint8());
             $frame->routingKey = $buffer->consume($buffer->consumeUint8());
             $frame->messageCount = $buffer->consumeInt32();
         } elseif ($methodId === Constants::METHOD_BASIC_GET_EMPTY) {
             $frame = new MethodBasicGetEmptyFrame();
             $frame->clusterId = $buffer->consume($buffer->consumeUint8());
         } elseif ($methodId === Constants::METHOD_BASIC_ACK) {
             $frame = new MethodBasicAckFrame();
             $frame->deliveryTag = $buffer->consumeInt64();
             list($frame->multiple) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_REJECT) {
             $frame = new MethodBasicRejectFrame();
             $frame->deliveryTag = $buffer->consumeInt64();
             list($frame->requeue) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_RECOVER_ASYNC) {
             $frame = new MethodBasicRecoverAsyncFrame();
             list($frame->requeue) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_RECOVER) {
             $frame = new MethodBasicRecoverFrame();
             list($frame->requeue) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_BASIC_RECOVER_OK) {
             $frame = new MethodBasicRecoverOkFrame();
         } elseif ($methodId === Constants::METHOD_BASIC_NACK) {
             $frame = new MethodBasicNackFrame();
             $frame->deliveryTag = $buffer->consumeInt64();
             list($frame->multiple, $frame->requeue) = $this->consumeBits($buffer, 2);
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_TX) {
         if ($methodId === Constants::METHOD_TX_SELECT) {
             $frame = new MethodTxSelectFrame();
         } elseif ($methodId === Constants::METHOD_TX_SELECT_OK) {
             $frame = new MethodTxSelectOkFrame();
         } elseif ($methodId === Constants::METHOD_TX_COMMIT) {
             $frame = new MethodTxCommitFrame();
         } elseif ($methodId === Constants::METHOD_TX_COMMIT_OK) {
             $frame = new MethodTxCommitOkFrame();
         } elseif ($methodId === Constants::METHOD_TX_ROLLBACK) {
             $frame = new MethodTxRollbackFrame();
         } elseif ($methodId === Constants::METHOD_TX_ROLLBACK_OK) {
             $frame = new MethodTxRollbackOkFrame();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } elseif ($classId === Constants::CLASS_CONFIRM) {
         if ($methodId === Constants::METHOD_CONFIRM_SELECT) {
             $frame = new MethodConfirmSelectFrame();
             list($frame->nowait) = $this->consumeBits($buffer, 1);
         } elseif ($methodId === Constants::METHOD_CONFIRM_SELECT_OK) {
             $frame = new MethodConfirmSelectOkFrame();
         } else {
             throw new InvalidMethodException($classId, $methodId);
         }
     } else {
         throw new InvalidClassException($classId);
     }
     $frame->classId = $classId;
     $frame->methodId = $methodId;
     return $frame;
 }
Ejemplo n.º 7
0
 /**
  * Appends AMQP table/array field value to buffer.
  *
  * @param mixed $value
  * @param Buffer $buffer
  */
 public function appendFieldValue($value, Buffer $buffer)
 {
     if (is_string($value)) {
         $buffer->appendUint8(Constants::FIELD_LONG_STRING);
         $buffer->appendUint32(strlen($value));
         $buffer->append($value);
     } elseif (is_int($value)) {
         $buffer->appendUint8(Constants::FIELD_LONG_INT);
         $buffer->appendInt32($value);
     } elseif (is_bool($value)) {
         $buffer->appendUint8(Constants::FIELD_BOOLEAN);
         $buffer->appendUint8(intval($value));
     } elseif (is_float($value)) {
         $buffer->appendUint8(Constants::FIELD_DOUBLE);
         $buffer->appendDouble($value);
     } elseif (is_array($value)) {
         if (array_keys($value) === range(0, count($value) - 1)) {
             // sequential array
             $buffer->appendUint8(Constants::FIELD_ARRAY);
             $this->appendArray($value, $buffer);
         } else {
             $buffer->appendUint8(Constants::FIELD_TABLE);
             $this->appendTable($value, $buffer);
         }
     } elseif (is_null($value)) {
         $buffer->appendUint8(Constants::FIELD_NULL);
     } elseif ($value instanceof \DateTime) {
         $buffer->appendUint8(Constants::FIELD_TIMESTAMP);
         $this->appendTimestamp($value, $buffer);
     } else {
         throw new ProtocolException("Unhandled value type '" . gettype($value) . "' " . (is_object($value) ? "(class " . get_class($value) . ")" : "") . ".");
     }
 }
Ejemplo n.º 8
0
 /**
  * Consumes AMQP table/array field value.
  *
  * @param Buffer $buffer
  * @return mixed
  */
 public function consumeFieldValue(Buffer $buffer)
 {
     $fieldType = $buffer->consumeUint8();
     switch ($fieldType) {
         case Constants::FIELD_BOOLEAN:
             return $buffer->consumeUint8() > 0;
         case Constants::FIELD_SHORT_SHORT_INT:
             return $buffer->consumeInt8();
         case Constants::FIELD_SHORT_SHORT_UINT:
             return $buffer->consumeUint8();
         case Constants::FIELD_SHORT_INT:
             return $buffer->consumeInt16();
         case Constants::FIELD_SHORT_UINT:
             return $buffer->consumeUint16();
         case Constants::FIELD_LONG_INT:
             return $buffer->consumeInt32();
         case Constants::FIELD_LONG_UINT:
             return $buffer->consumeUint32();
         case Constants::FIELD_LONG_LONG_INT:
             return $buffer->consumeInt64();
         case Constants::FIELD_LONG_LONG_UINT:
             return $buffer->consumeUint64();
         case Constants::FIELD_FLOAT:
             return $buffer->consumeFloat();
         case Constants::FIELD_DOUBLE:
             return $buffer->consumeDouble();
         case Constants::FIELD_DECIMAL_VALUE:
             return $this->consumeDecimalValue($buffer);
         case Constants::FIELD_SHORT_STRING:
             return $buffer->consume($buffer->consumeUint8());
         case Constants::FIELD_LONG_STRING:
             return $buffer->consume($buffer->consumeUint32());
         case Constants::FIELD_ARRAY:
             return $this->consumeArray($buffer);
         case Constants::FIELD_TIMESTAMP:
             return $this->consumeTimestamp($buffer);
         case Constants::FIELD_TABLE:
             return $this->consumeTable($buffer);
         case Constants::FIELD_NULL:
             return null;
         default:
             throw new ProtocolException(sprintf("Unhandled field type 0x%02x", $fieldType) . (ctype_print(chr($fieldType)) ? " ('" . chr($fieldType) . "')" : "") . ".");
     }
 }