Use static greeting buffers.

This commit is contained in:
Ian Gulliver
2016-02-27 16:23:26 -08:00
parent 8d9b40e954
commit 071613d061
9 changed files with 49 additions and 48 deletions

View File

@@ -115,6 +115,7 @@ int main(int argc, char *argv[]) {
beast_init();
json_init();
proto_init();
stats_init();
if (!parse_opts(argc, argv)) {

View File

@@ -151,10 +151,6 @@ bool airspy_adsb_parse(struct buf *buf, struct packet *packet, void *state_in) {
}
void airspy_adsb_serialize(struct packet *packet, struct buf *buf) {
if (!packet) {
return;
}
switch (packet->type) {
case PACKET_TYPE_NONE:
break;

View File

@@ -180,10 +180,6 @@ bool beast_parse(struct buf *buf, struct packet *packet, void *state_in) {
}
void beast_serialize(struct packet *packet, struct buf *buf) {
if (!packet) {
return;
}
switch (packet->type) {
case PACKET_TYPE_NONE:
break;

View File

@@ -25,6 +25,7 @@ struct json_parser_state {
};
static json_t *json_prev = NULL;
static struct buf json_hello_buf = BUF_INIT;
static void json_serialize_to_buf(json_t *obj, struct buf *buf) {
assert(json_dump_callback(obj, json_buf_append_callback, buf, 0) == 0);
@@ -32,19 +33,6 @@ static void json_serialize_to_buf(json_t *obj, struct buf *buf) {
buf_chr(buf, buf->length++) = '\n';
}
static void json_hello(struct buf *buf) {
json_t *hello = json_pack(
"{s:s, s:s, s:s, s:s, s:I, s:I, s:I}",
"type", "header",
"magic", JSON_MAGIC,
"server_version", server_version,
"server_id", server_id,
"mlat_timestamp_mhz", (json_int_t) PACKET_MLAT_MHZ,
"mlat_timestamp_max", (json_int_t) PACKET_MLAT_MAX,
"rssi_max", (json_int_t) PACKET_RSSI_MAX);
json_serialize_to_buf(hello, buf);
}
static void json_add_common(struct packet *packet, json_t *obj) {
json_object_set_new(obj, "type", json_string(packet_type_names[packet->type]));
json_object_set_new(obj, "source_id", json_string((const char *) packet->source_id));
@@ -192,6 +180,17 @@ void json_init() {
size_t seed;
rand_fill(&seed, sizeof(seed));
json_object_seed(seed);
json_t *hello = json_pack(
"{s:s, s:s, s:s, s:s, s:I, s:I, s:I}",
"type", "header",
"magic", JSON_MAGIC,
"server_version", server_version,
"server_id", server_id,
"mlat_timestamp_mhz", (json_int_t) PACKET_MLAT_MHZ,
"mlat_timestamp_max", (json_int_t) PACKET_MLAT_MAX,
"rssi_max", (json_int_t) PACKET_RSSI_MAX);
json_serialize_to_buf(hello, &json_hello_buf);
}
void json_cleanup() {
@@ -248,11 +247,6 @@ bool json_parse(struct buf *buf, struct packet *packet, void *state_in) {
}
void json_serialize(struct packet *packet, struct buf *buf) {
if (!packet) {
json_hello(buf);
return;
}
switch (packet->type) {
case PACKET_TYPE_NONE:
break;
@@ -267,6 +261,10 @@ void json_serialize(struct packet *packet, struct buf *buf) {
}
}
void json_hello(struct buf **buf) {
*buf = &json_hello_buf;
}
int json_buf_append_callback(const char *buffer, size_t size, void *data) {
struct buf *buf = data;
if (buf->length + size + 1 > BUF_LEN_MAX) {

View File

@@ -9,5 +9,6 @@ void json_init(void);
void json_cleanup(void);
bool __attribute__ ((warn_unused_result)) json_parse(struct buf *, struct packet *, void *);
void json_serialize(struct packet *, struct buf *);
void json_hello(struct buf **);
int json_buf_append_callback(const char *, size_t, void *);

View File

@@ -29,6 +29,7 @@ struct proto_parser_state {
};
static Adsb *proto_prev = NULL;
static struct buf proto_hello_buf = BUF_INIT;
static void proto_obj_to_buf(Adsb *wrapper, struct buf *buf) {
assert(!buf->length);
@@ -126,6 +127,21 @@ static bool proto_parse_packet(AdsbPacket *in, struct packet *packet, struct pro
return true;
}
void proto_init() {
AdsbHeader header = ADSB_HEADER__INIT;
header.magic = PROTO_MAGIC;
header.server_version = server_version;
header.server_id = (char *) server_id;
header.mlat_timestamp_mhz = PACKET_MLAT_MHZ;
header.mlat_timestamp_max = PACKET_MLAT_MAX;
header.rssi_max = PACKET_RSSI_MAX;
Adsb wrapper = ADSB__INIT;
wrapper.header = &header;
proto_obj_to_buf(&wrapper, &proto_hello_buf);
}
void proto_cleanup() {
if (proto_prev) {
adsb__free_unpacked(proto_prev, NULL);
@@ -184,22 +200,6 @@ bool proto_parse(struct buf *buf, struct packet *packet, void *state_in) {
}
void proto_serialize(struct packet *packet, struct buf *buf) {
if (!packet) {
AdsbHeader header = ADSB_HEADER__INIT;
header.magic = PROTO_MAGIC;
header.server_version = server_version;
header.server_id = (char *) server_id;
header.mlat_timestamp_mhz = PACKET_MLAT_MHZ;
header.mlat_timestamp_max = PACKET_MLAT_MAX;
header.rssi_max = PACKET_RSSI_MAX;
Adsb wrapper = ADSB__INIT;
wrapper.header = &header;
proto_obj_to_buf(&wrapper, buf);
return;
}
switch (packet->type) {
case PACKET_TYPE_NONE:
break;
@@ -213,3 +213,7 @@ void proto_serialize(struct packet *packet, struct buf *buf) {
break;
}
}
void proto_hello(struct buf **buf) {
*buf = &proto_hello_buf;
}

View File

@@ -5,6 +5,8 @@
struct buf;
struct packet;
void proto_init(void);
void proto_cleanup(void);
bool __attribute__ ((warn_unused_result)) proto_parse(struct buf *, struct packet *, void *);
void proto_serialize(struct packet *, struct buf *);
void proto_hello(struct buf **);

View File

@@ -87,10 +87,6 @@ bool raw_parse(struct buf *buf, struct packet *packet, void __attribute__((unuse
}
void raw_serialize(struct packet *packet, struct buf *buf) {
if (!packet) {
return;
}
switch (packet->type) {
case PACKET_TYPE_NONE:
break;

View File

@@ -32,34 +32,42 @@ struct send {
};
typedef void (*serialize)(struct packet *, struct buf *);
typedef void (*hello)(struct buf **);
static struct serializer {
char *name;
serialize serialize;
hello hello;
struct list_head send_head;
} serializers[] = {
{
.name = "airspy_adsb",
.serialize = airspy_adsb_serialize,
.hello = NULL,
},
{
.name = "beast",
.serialize = beast_serialize,
.hello = NULL,
},
{
.name = "json",
.serialize = json_serialize,
.hello = json_hello,
},
{
.name = "proto",
.serialize = proto_serialize,
.hello = proto_hello,
},
{
.name = "raw",
.serialize = raw_serialize,
.hello = NULL,
},
{
.name = "stats",
.serialize = stats_serialize,
.hello = NULL,
},
};
#define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers))
@@ -130,8 +138,7 @@ void send_new_wrapper(int fd, void *passthrough, struct peer *on_close) {
void send_hello(struct buf **buf_pp, void *passthrough) {
struct serializer *serializer = (struct serializer *) passthrough;
// TODO: change API to avoid special-case NULL packet*, and to allow static greetings.
serializer->serialize(NULL, *buf_pp);
serializer->hello(buf_pp);
}
void send_write(struct packet *packet) {