示例#1
0
 public function testMinSignedInt8()
 {
     $minSignedInt8 = -0x80;
     $packedMinSignedInt8 = Packer::packSignedInt8($minSignedInt8);
     $unpackedMinSignedInt8 = Packer::unpackSignedInt8($packedMinSignedInt8);
     $this->assertSame($minSignedInt8, $unpackedMinSignedInt8);
 }
示例#2
0
 public function send($requiredAcks, $timeout, $topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add RequiredAcks
     $data .= Packer::packSignedInt16($requiredAcks);
     // Add Timeout
     $data .= Packer::packSignedInt32($timeout);
     // Add Topic count
     $data .= Packer::packSignedInt32(count($topics));
     // Add Topics
     foreach ($topics as $topic => $partitions) {
         // Add Topic
         $data .= Packer::packStringSignedInt16($topic);
         // Add Partition count
         $data .= Packer::packSignedInt32(count($partitions));
         // Add Partitions
         foreach ($partitions as $partition => $messages) {
             $data .= Packer::packSignedInt32($partition);
             $packedMessageSet = '';
             foreach ($messages as $message) {
                 if (!is_array($message)) {
                     $offset = 0;
                     // Producer does not know message offset, so we can fill with any value (we choose 0)
                     $magicByte = 0;
                     // Used to ensure backwards compatibility. Currently set to 0;
                     $attributes = 0;
                     $key = null;
                 } else {
                     $offset = array_key_exists('Offset', $message) ? $message['Offset'] : 0;
                     $magicByte = array_key_exists('MagicByte', $message) ? $message['MagicByte'] : 0;
                     $attributes = array_key_exists('Attributes', $message) ? $message['Attributes'] : 0;
                     $attributes = 0;
                     // TODO: handle compression
                     $key = array_key_exists('Key', $message) ? $message['Key'] : null;
                 }
                 $packedMessage = Packer::packSignedInt8($magicByte);
                 $packedMessage .= Packer::packSignedInt8($attributes);
                 $packedMessage .= Packer::packStringSignedInt32($key);
                 $packedMessage .= Packer::packStringSignedInt32($message);
                 $packedMessage = Packer::packSignedInt32(crc32($packedMessage)) . $packedMessage;
                 $packedMessageSet .= Packer::packSignedInt64($offset) . Packer::packStringSignedInt32($packedMessage);
             }
             // Add MessageSet
             $data .= Packer::packStringSignedInt32($packedMessageSet);
         }
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }