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

@@ -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) {