|
12 | 12 | use Protobuf\AbstractMessage; |
13 | 13 | use Pulsar\Exception\OptionsException; |
14 | 14 | use Pulsar\Exception\RuntimeException; |
15 | | -use Pulsar\Proto\BaseCommand; |
16 | | -use Pulsar\Proto\BaseCommand\Type; |
17 | | -use Pulsar\Proto\CommandSend; |
18 | 15 | use Pulsar\Proto\CommandSendReceipt; |
19 | 16 | use Pulsar\Proto\KeyValue; |
20 | 17 | use Pulsar\Proto\MessageMetadata; |
21 | 18 | use Pulsar\Proto\SingleMessageMetadata; |
| 19 | +use Pulsar\Traits\CommandSendBuilder; |
22 | 20 | use Pulsar\Traits\ProducerKeepAlive; |
23 | 21 | use Pulsar\Util\Buffer; |
24 | 22 | use Pulsar\Util\Helper; |
|
31 | 29 | class Producer extends Client |
32 | 30 | { |
33 | 31 | use ProducerKeepAlive; |
| 32 | + use CommandSendBuilder; |
34 | 33 |
|
35 | 34 | /** |
36 | 35 | * @var ProducerOptions |
@@ -83,13 +82,14 @@ public function connect() |
83 | 82 | * @param string $payload |
84 | 83 | * @param array $options |
85 | 84 | * @return string |
86 | | - * @throws RuntimeException|OptionsException |
| 85 | + * @throws RuntimeException |
| 86 | + * @throws \Exception |
87 | 87 | */ |
88 | 88 | public function send(string $payload, array $options = []): string |
89 | 89 | { |
90 | 90 | $producer = $this->getPartitionProducer(); |
91 | 91 | $messageOptions = new MessageOptions($options); |
92 | | - $buffer = $this->buildBuffer( |
| 92 | + $buffer = $this->buildSendBuffer( |
93 | 93 | $producer, |
94 | 94 | $payload, |
95 | 95 | $messageOptions, |
@@ -117,14 +117,15 @@ public function send(string $payload, array $options = []): string |
117 | 117 | * @param array $options |
118 | 118 | * @return void |
119 | 119 | * @throws RuntimeException|OptionsException |
| 120 | + * @throws \Exception |
120 | 121 | */ |
121 | 122 | public function sendAsync(string $payload, callable $callable, array $options = []) |
122 | 123 | { |
123 | 124 | $messageOptions = new MessageOptions($options); |
124 | 125 | $sequenceID = $messageOptions->getSequenceID(); |
125 | 126 |
|
126 | 127 | $producer = $this->getPartitionProducer(); |
127 | | - $buffer = $this->buildBuffer($producer, $payload, $messageOptions, $sequenceID); |
| 128 | + $buffer = $this->buildSendBuffer($producer, $payload, $messageOptions, $sequenceID); |
128 | 129 | $producer->sendAsync($buffer); |
129 | 130 | $this->callbacks[ $sequenceID ] = [$producer->getID(), $callable]; |
130 | 131 | } |
@@ -162,104 +163,6 @@ public function wait() |
162 | 163 | } while (count($this->callbacks)); |
163 | 164 | } |
164 | 165 |
|
165 | | - /** |
166 | | - * @param PartitionProducer $producer |
167 | | - * @param string $payload |
168 | | - * @param MessageOptions $messageOptions |
169 | | - * @param int $sequenceID |
170 | | - * @return Buffer |
171 | | - * @throws RuntimeException|OptionsException |
172 | | - */ |
173 | | - protected function buildBuffer(PartitionProducer $producer, string $payload, MessageOptions $messageOptions, int $sequenceID): Buffer |
174 | | - { |
175 | | - // [totalSize] [commandSize] [command] [magicNumber] [checksum] [metadataSize] [metadata] [payload] |
176 | | - |
177 | | - $buffer = new Buffer(); |
178 | | - |
179 | | - // BaseCommand |
180 | | - $baseCommand = new BaseCommand(); |
181 | | - $baseCommand->setType(Type::SEND()); |
182 | | - |
183 | | - // CommandSend |
184 | | - $commandSend = new CommandSend(); |
185 | | - $commandSend->setProducerId($producer->getID()); |
186 | | - $commandSend->setSequenceId($sequenceID); |
187 | | - $commandSend->setNumMessages(1); |
188 | | - $commandSend->setTxnidLeastBits(null); |
189 | | - $commandSend->setTxnidMostBits(null); |
190 | | - |
191 | | - $baseCommand->setSend($commandSend); |
192 | | - |
193 | | - // serialize BaseCommand |
194 | | - $baseCommandBytes = $baseCommand->toStream()->getContents(); |
195 | | - |
196 | | - // [commandSize] |
197 | | - $buffer->writeUint32(strlen($baseCommandBytes)); |
198 | | - |
199 | | - // [command] |
200 | | - $buffer->write($baseCommandBytes); |
201 | | - |
202 | | - // [magicNumber] |
203 | | - $buffer->writeUint16(0x0e01); |
204 | | - |
205 | | - // only support zlib、none |
206 | | - $compressionProvider = $this->options->getCompression(); |
207 | | - |
208 | | - // metadata |
209 | | - $msgMetadata = new MessageMetadata(); |
210 | | - $msgMetadata->setProducerName($producer->getName()); |
211 | | - $msgMetadata->setSequenceId(0); |
212 | | - $msgMetadata->setPublishTime(time() * 1000); |
213 | | - $msgMetadata->setNumMessagesInBatch(1); |
214 | | - $msgMetadata->setCompression($compressionProvider->getType()); |
215 | | - $msgMetadata->setPartitionKey($messageOptions->getKey()); |
216 | | - $msgMetadata->setDeliverAtTime($messageOptions->getDeliverAtTime()); |
217 | | - |
218 | | - // singleMessageMetadata |
219 | | - $singleMsgMetadata = new SingleMessageMetadata(); |
220 | | - $singleMsgMetadata->setPayloadSize(strlen($payload)); |
221 | | - $singleMsgMetadata->setEventTime(time() * 1000); |
222 | | - $singleMsgMetadata->setPartitionKey($messageOptions->getKey()); |
223 | | - $this->appendProperties($singleMsgMetadata, $messageOptions); |
224 | | - $singleMsgMetadataBytes = $singleMsgMetadata->toStream()->getContents(); |
225 | | - |
226 | | - // [metadataSize] [metadata] [payload] |
227 | | - $packet = ''; |
228 | | - $packet .= Helper::writeUint32(strlen($singleMsgMetadataBytes)); // [metadataSize] |
229 | | - $packet .= $singleMsgMetadataBytes; // [metadata] |
230 | | - $packet .= $payload; // [payload] |
231 | | - |
232 | | - $msgMetadata->setUncompressedSize(strlen($packet)); |
233 | | - $msgMetadataBytes = $msgMetadata->toStream()->getContents(); |
234 | | - |
235 | | - $msgMetadataSize = strlen($msgMetadataBytes); |
236 | | - |
237 | | - |
238 | | - // make checksum bytes |
239 | | - $compressionPacket = $compressionProvider->encode($packet); |
240 | | - $checksumBuffer = new Buffer(); |
241 | | - $checksumBuffer->writeUint32($msgMetadataSize); // [metadataSize] |
242 | | - $checksumBuffer->write($msgMetadataBytes); // [metadata] |
243 | | - $checksumBuffer->write($compressionPacket); // [payload] |
244 | | - |
245 | | - // [checksum] === [metadataSize] [metadata] [payload] |
246 | | - $buffer->writeUint32($this->getChecksum($checksumBuffer)); |
247 | | - |
248 | | - // [metadataSize] |
249 | | - $buffer->writeUint32($msgMetadataSize); |
250 | | - |
251 | | - // [metadata] |
252 | | - $buffer->write($msgMetadataBytes); |
253 | | - |
254 | | - // [payload] |
255 | | - $buffer->write($compressionPacket); |
256 | | - |
257 | | - // [totalSize] |
258 | | - $buffer->put(Helper::writeUint32($buffer->length()), 0); |
259 | | - |
260 | | - return $buffer; |
261 | | - } |
262 | | - |
263 | 166 |
|
264 | 167 | /** |
265 | 168 | * @return void |
|
0 commit comments