AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
protobuf_framing.c
Go to the documentation of this file.
1 
4 #include <amqp.h> // for amqp_envelope_t
5 #include <stdint.h> // for uint8_t, uint32_t
6 #include <string.h> // for bzero, memcpy
7 
8 #include "protobuf_framing.h"
9 
13 uint32_t convert_framing_size(uint32_t len, uint8_t* bytes)
14 {
15  uint32_t size = 0;
16  uint32_t value = len;
17 
18  while (value > 0x7F)
19  {
20  bytes[size++] = (((uint8_t)(value)) & 0x7F) | 0x80;
21  value >>= 7;
22  }
23  bytes[size++] = (uint8_t) value;
24 
25  return size;
26 }
27 
31 int write_protobuf(socket_t sock, amqp_envelope_t* envelope)
32 {
33  size_t amqp_size = envelope->message.body.len;
34  uint32_t size = amqp_size + 4;
35  uint8_t framing[4] = {0};
36  uint8_t buf[size];
37  memset(buf, 0, size);
38 
39  uint8_t size_framing = convert_framing_size(amqp_size, framing);
40  memcpy(buf, framing, size_framing);
41  memcpy(buf + size_framing, envelope->message.body.bytes, amqp_size);
42  return send(sock, buf, size, 0);
43 }
int write_protobuf(socket_t sock, amqp_envelope_t *envelope)
Write a protobuf, with a varint32 framing, and padding at the end from an envelope.
int socket_t
Alias to differenciate between regular ints and socket fds.
Definition: socket.h:13
uint32_t convert_framing_size(uint32_t len, uint8_t *bytes)
Convert an unsigned int to varint32 so that google’s C++ protobuf libs can read it.
Utility to convert framing to varint32 for google’s parser.