Add framing to proto, since it isn't built in to protobuf.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -11,6 +12,10 @@
|
|||||||
|
|
||||||
#define PROTO_MAGIC "aDsB"
|
#define PROTO_MAGIC "aDsB"
|
||||||
|
|
||||||
|
struct proto_header {
|
||||||
|
uint32_t length;
|
||||||
|
};
|
||||||
|
|
||||||
struct proto_parser_state {
|
struct proto_parser_state {
|
||||||
struct packet_mlat_state mlat_state;
|
struct packet_mlat_state mlat_state;
|
||||||
uint16_t mlat_timestamp_mhz;
|
uint16_t mlat_timestamp_mhz;
|
||||||
@@ -23,9 +28,13 @@ static Adsb *proto_prev = NULL;
|
|||||||
|
|
||||||
static void proto_obj_to_buf(Adsb *wrapper, struct buf *buf) {
|
static void proto_obj_to_buf(Adsb *wrapper, struct buf *buf) {
|
||||||
assert(!buf->length);
|
assert(!buf->length);
|
||||||
assert(adsb__get_packed_size(wrapper) <= BUF_LEN_MAX);
|
struct proto_header *header = (struct proto_header *) buf_at(buf, 0);
|
||||||
buf->length = adsb__pack(wrapper, (uint8_t *) buf_at(buf, 0));
|
assert(sizeof(*header) <= BUF_LEN_MAX);
|
||||||
assert(buf->length);
|
uint32_t msg_len = adsb__get_packed_size(wrapper);
|
||||||
|
buf->length = sizeof(*header) + msg_len;
|
||||||
|
assert(buf->length <= BUF_LEN_MAX);
|
||||||
|
assert(adsb__pack(wrapper, (uint8_t *) buf_at(buf, sizeof(*header))) == msg_len);
|
||||||
|
header->length = htonl(msg_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void proto_serialize_packet(struct packet *packet, AdsbPacket *out, size_t len) {
|
static void proto_serialize_packet(struct packet *packet, AdsbPacket *out, size_t len) {
|
||||||
@@ -113,7 +122,16 @@ bool proto_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
|||||||
proto_prev = NULL;
|
proto_prev = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Adsb *wrapper = adsb__unpack(NULL, buf->length, (uint8_t *) buf_at(buf, 0));
|
struct proto_header *header = (struct proto_header *) buf_at(buf, 0);
|
||||||
|
if (buf->length < sizeof(*header)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint32_t msg_len = ntohl(header->length);
|
||||||
|
if (buf->length < sizeof(*header) + msg_len) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Adsb *wrapper = adsb__unpack(NULL, msg_len, (uint8_t *) buf_at(buf, sizeof(*header)));
|
||||||
if (!wrapper) {
|
if (!wrapper) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -143,7 +161,7 @@ bool proto_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proto_prev = wrapper;
|
proto_prev = wrapper;
|
||||||
buf_consume(buf, adsb__get_packed_size(wrapper));
|
buf_consume(buf, sizeof(*header) + msg_len);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ struct serializer *send_get_serializer(char *name) {
|
|||||||
void send_add(int fd, struct serializer *serializer) {
|
void send_add(int fd, struct serializer *serializer) {
|
||||||
if (!send_hello(fd, serializer)) {
|
if (!send_hello(fd, serializer)) {
|
||||||
fprintf(stderr, "S xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx: Failed to write hello\n");
|
fprintf(stderr, "S xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx: Failed to write hello\n");
|
||||||
|
assert(!close(fd));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user