Пример #1
0
 /**
  * Preload the next message
  * 
  * @return void
  */
 private function preloadNextMessage()
 {
     try {
         $this->msg = new Message($this->getMessage());
         if ($this->msg->compression() != Encoder::COMPRESSION_NONE) {
             $this->internalIterator = $this->msg->payload();
             $this->internalIterator->rewind();
             $this->msg = null;
         } else {
             $this->internalIterator = null;
         }
         $this->valid = TRUE;
     } catch (Exception\OutOfRange $e) {
         $this->valid = FALSE;
     }
 }
 /**
  * Pack message
  *
  * Internal method for packing message into kafka wire format.
  *
  * @param Message $message
  * @param Mixed   $overrideCompression Null or \Kafka\Kafka::COMPRESSION_NONE
  * or
  *      \Kafka\Kafka::COMPRESSION_GZIP, etc.
  *
  * @throws \Kafka\Exception
  */
 protected function packMessage(Message $message, $overrideCompression = null)
 {
     $compression = $overrideCompression === null ? $message->compression() : $overrideCompression;
     switch ($compression) {
         case \Kafka\Kafka::COMPRESSION_NONE:
             $compressedPayload = $message->payload();
             break;
         case \Kafka\Kafka::COMPRESSION_GZIP:
             $compressedPayload = gzencode($message->payload());
             break;
         case \Kafka\Kafka::COMPRESSION_SNAPPY:
             throw new \Kafka\Kafka_Exception("Snappy compression not yet implemented in php\n                    client");
             break;
         default:
             throw new \Kafka\Kafka_Exception("Unknown kafka compression codec {$compression}");
             break;
     }
     // for reach message using MAGIC_1 format which includes compression
     // attribute byte
     $messageBoundsSize = 1 + 1 + 4 + strlen($compressedPayload);
     $data = pack('N', $messageBoundsSize);
     //int
     $data .= pack('C', \Kafka\Kafka::MAGIC_1);
     //byte
     $data .= pack('C', $compression);
     //byte
     $data .= pack('N', crc32($compressedPayload));
     //int
     $data .= $compressedPayload;
     //unbounded string
     return $data;
 }