Use static greeting buffers.
This commit is contained in:
@@ -115,6 +115,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
beast_init();
|
beast_init();
|
||||||
json_init();
|
json_init();
|
||||||
|
proto_init();
|
||||||
stats_init();
|
stats_init();
|
||||||
|
|
||||||
if (!parse_opts(argc, argv)) {
|
if (!parse_opts(argc, argv)) {
|
||||||
|
|||||||
@@ -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) {
|
void airspy_adsb_serialize(struct packet *packet, struct buf *buf) {
|
||||||
if (!packet) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (packet->type) {
|
switch (packet->type) {
|
||||||
case PACKET_TYPE_NONE:
|
case PACKET_TYPE_NONE:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -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) {
|
void beast_serialize(struct packet *packet, struct buf *buf) {
|
||||||
if (!packet) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (packet->type) {
|
switch (packet->type) {
|
||||||
case PACKET_TYPE_NONE:
|
case PACKET_TYPE_NONE:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ struct json_parser_state {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static json_t *json_prev = NULL;
|
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) {
|
static void json_serialize_to_buf(json_t *obj, struct buf *buf) {
|
||||||
assert(json_dump_callback(obj, json_buf_append_callback, buf, 0) == 0);
|
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';
|
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) {
|
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, "type", json_string(packet_type_names[packet->type]));
|
||||||
json_object_set_new(obj, "source_id", json_string((const char *) packet->source_id));
|
json_object_set_new(obj, "source_id", json_string((const char *) packet->source_id));
|
||||||
@@ -192,6 +180,17 @@ void json_init() {
|
|||||||
size_t seed;
|
size_t seed;
|
||||||
rand_fill(&seed, sizeof(seed));
|
rand_fill(&seed, sizeof(seed));
|
||||||
json_object_seed(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() {
|
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) {
|
void json_serialize(struct packet *packet, struct buf *buf) {
|
||||||
if (!packet) {
|
|
||||||
json_hello(buf);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (packet->type) {
|
switch (packet->type) {
|
||||||
case PACKET_TYPE_NONE:
|
case PACKET_TYPE_NONE:
|
||||||
break;
|
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) {
|
int json_buf_append_callback(const char *buffer, size_t size, void *data) {
|
||||||
struct buf *buf = data;
|
struct buf *buf = data;
|
||||||
if (buf->length + size + 1 > BUF_LEN_MAX) {
|
if (buf->length + size + 1 > BUF_LEN_MAX) {
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ void json_init(void);
|
|||||||
void json_cleanup(void);
|
void json_cleanup(void);
|
||||||
bool __attribute__ ((warn_unused_result)) json_parse(struct buf *, struct packet *, void *);
|
bool __attribute__ ((warn_unused_result)) json_parse(struct buf *, struct packet *, void *);
|
||||||
void json_serialize(struct packet *, struct buf *);
|
void json_serialize(struct packet *, struct buf *);
|
||||||
|
void json_hello(struct buf **);
|
||||||
|
|
||||||
int json_buf_append_callback(const char *, size_t, void *);
|
int json_buf_append_callback(const char *, size_t, void *);
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ struct proto_parser_state {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static Adsb *proto_prev = NULL;
|
static Adsb *proto_prev = NULL;
|
||||||
|
static struct buf proto_hello_buf = BUF_INIT;
|
||||||
|
|
||||||
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);
|
||||||
@@ -126,6 +127,21 @@ static bool proto_parse_packet(AdsbPacket *in, struct packet *packet, struct pro
|
|||||||
return true;
|
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() {
|
void proto_cleanup() {
|
||||||
if (proto_prev) {
|
if (proto_prev) {
|
||||||
adsb__free_unpacked(proto_prev, NULL);
|
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) {
|
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) {
|
switch (packet->type) {
|
||||||
case PACKET_TYPE_NONE:
|
case PACKET_TYPE_NONE:
|
||||||
break;
|
break;
|
||||||
@@ -213,3 +213,7 @@ void proto_serialize(struct packet *packet, struct buf *buf) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void proto_hello(struct buf **buf) {
|
||||||
|
*buf = &proto_hello_buf;
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
struct buf;
|
struct buf;
|
||||||
struct packet;
|
struct packet;
|
||||||
|
|
||||||
|
void proto_init(void);
|
||||||
void proto_cleanup(void);
|
void proto_cleanup(void);
|
||||||
bool __attribute__ ((warn_unused_result)) proto_parse(struct buf *, struct packet *, void *);
|
bool __attribute__ ((warn_unused_result)) proto_parse(struct buf *, struct packet *, void *);
|
||||||
void proto_serialize(struct packet *, struct buf *);
|
void proto_serialize(struct packet *, struct buf *);
|
||||||
|
void proto_hello(struct buf **);
|
||||||
|
|||||||
@@ -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) {
|
void raw_serialize(struct packet *packet, struct buf *buf) {
|
||||||
if (!packet) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (packet->type) {
|
switch (packet->type) {
|
||||||
case PACKET_TYPE_NONE:
|
case PACKET_TYPE_NONE:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -32,34 +32,42 @@ struct send {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef void (*serialize)(struct packet *, struct buf *);
|
typedef void (*serialize)(struct packet *, struct buf *);
|
||||||
|
typedef void (*hello)(struct buf **);
|
||||||
static struct serializer {
|
static struct serializer {
|
||||||
char *name;
|
char *name;
|
||||||
serialize serialize;
|
serialize serialize;
|
||||||
|
hello hello;
|
||||||
struct list_head send_head;
|
struct list_head send_head;
|
||||||
} serializers[] = {
|
} serializers[] = {
|
||||||
{
|
{
|
||||||
.name = "airspy_adsb",
|
.name = "airspy_adsb",
|
||||||
.serialize = airspy_adsb_serialize,
|
.serialize = airspy_adsb_serialize,
|
||||||
|
.hello = NULL,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "beast",
|
.name = "beast",
|
||||||
.serialize = beast_serialize,
|
.serialize = beast_serialize,
|
||||||
|
.hello = NULL,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "json",
|
.name = "json",
|
||||||
.serialize = json_serialize,
|
.serialize = json_serialize,
|
||||||
|
.hello = json_hello,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "proto",
|
.name = "proto",
|
||||||
.serialize = proto_serialize,
|
.serialize = proto_serialize,
|
||||||
|
.hello = proto_hello,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "raw",
|
.name = "raw",
|
||||||
.serialize = raw_serialize,
|
.serialize = raw_serialize,
|
||||||
|
.hello = NULL,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "stats",
|
.name = "stats",
|
||||||
.serialize = stats_serialize,
|
.serialize = stats_serialize,
|
||||||
|
.hello = NULL,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
#define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers))
|
#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) {
|
void send_hello(struct buf **buf_pp, void *passthrough) {
|
||||||
struct serializer *serializer = (struct serializer *) passthrough;
|
struct serializer *serializer = (struct serializer *) passthrough;
|
||||||
// TODO: change API to avoid special-case NULL packet*, and to allow static greetings.
|
serializer->hello(buf_pp);
|
||||||
serializer->serialize(NULL, *buf_pp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_write(struct packet *packet) {
|
void send_write(struct packet *packet) {
|
||||||
|
|||||||
Reference in New Issue
Block a user