Centralized list implementation.
This commit is contained in:
@@ -6,7 +6,7 @@ LIBS ?= -ljansson -lprotobuf-c
|
|||||||
|
|
||||||
OBJ_NETWORK = incoming.o outgoing.o receive.o send.o
|
OBJ_NETWORK = incoming.o outgoing.o receive.o send.o
|
||||||
OBJ_PROTOCOL = airspy_adsb.o beast.o json.o proto.o raw.o stats.o
|
OBJ_PROTOCOL = airspy_adsb.o beast.o json.o proto.o raw.o stats.o
|
||||||
OBJ_UTIL = buf.o hex.o opts.o packet.o peer.o rand.o resolve.o server.o socket.o uuid.o wakeup.o
|
OBJ_UTIL = buf.o hex.o list.o opts.o packet.o peer.o rand.o resolve.o server.o socket.o uuid.o wakeup.o
|
||||||
OBJ_PROTO = adsb.pb-c.o
|
OBJ_PROTO = adsb.pb-c.o
|
||||||
|
|
||||||
all: adsbus
|
all: adsbus
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
#include "peer.h"
|
#include "peer.h"
|
||||||
#include "resolve.h"
|
#include "resolve.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
@@ -27,10 +28,10 @@ struct incoming {
|
|||||||
incoming_connection_handler handler;
|
incoming_connection_handler handler;
|
||||||
void *passthrough;
|
void *passthrough;
|
||||||
uint32_t *count;
|
uint32_t *count;
|
||||||
struct incoming *next;
|
struct list_head incoming_list;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct incoming *incoming_head = NULL;
|
static struct list_head incoming_head = LIST_HEAD_INIT(incoming_head);
|
||||||
|
|
||||||
static void incoming_resolve_wrapper(struct peer *);
|
static void incoming_resolve_wrapper(struct peer *);
|
||||||
|
|
||||||
@@ -137,11 +138,9 @@ static void incoming_resolve_wrapper(struct peer *peer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void incoming_cleanup() {
|
void incoming_cleanup() {
|
||||||
struct incoming *iter = incoming_head;
|
struct incoming *iter, *next;
|
||||||
while (iter) {
|
list_for_each_entry_safe(iter, next, &incoming_head, incoming_list) {
|
||||||
struct incoming *next = iter->next;
|
|
||||||
incoming_del(iter);
|
incoming_del(iter);
|
||||||
iter = next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,8 +157,7 @@ void incoming_new(char *node, char *service, incoming_connection_handler handler
|
|||||||
incoming->passthrough = passthrough;
|
incoming->passthrough = passthrough;
|
||||||
incoming->count = count;
|
incoming->count = count;
|
||||||
|
|
||||||
incoming->next = incoming_head;
|
list_add(&incoming->incoming_list, &incoming_head);
|
||||||
incoming_head = incoming;
|
|
||||||
|
|
||||||
incoming_resolve(incoming);
|
incoming_resolve(incoming);
|
||||||
}
|
}
|
||||||
|
|||||||
24
adsbus/list.c
Normal file
24
adsbus/list.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
|
void list_head_init(struct list_head *head) {
|
||||||
|
head->next = head->prev = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool list_is_empty(const struct list_head *head) {
|
||||||
|
return head->next == head;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_add(struct list_head *new, struct list_head *head) {
|
||||||
|
new->next = head;
|
||||||
|
new->prev = head->prev;
|
||||||
|
new->prev->next = new;
|
||||||
|
head->prev = new;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_del(struct list_head *entry) {
|
||||||
|
entry->next->prev = entry->prev;
|
||||||
|
entry->prev->next = entry->next;
|
||||||
|
entry->prev = entry->next = NULL;
|
||||||
|
}
|
||||||
39
adsbus/list.h
Normal file
39
adsbus/list.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
||||||
|
#pragma GCC diagnostic ignored "-Wgnu-statement-expression"
|
||||||
|
#pragma GCC diagnostic ignored "-Wlanguage-extension-token"
|
||||||
|
|
||||||
|
#define offset_of(type, member) ((size_t) &((type *) NULL)->member)
|
||||||
|
|
||||||
|
#define container_of(ptr, type, member) ({ \
|
||||||
|
typeof( ((type *) NULL)->member ) *__mptr = (ptr); \
|
||||||
|
(type *)( (char *)__mptr - offset_of(type, member) );})
|
||||||
|
|
||||||
|
struct list_head {
|
||||||
|
struct list_head *next;
|
||||||
|
struct list_head *prev;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LIST_HEAD_INIT(name) { &(name), &(name) }
|
||||||
|
|
||||||
|
#define list_entry(ptr, type, member) \
|
||||||
|
container_of(ptr, type, member)
|
||||||
|
|
||||||
|
#define list_for_each_entry(pos, head, member) \
|
||||||
|
for (pos = list_entry((head)->next, typeof(*pos), member); \
|
||||||
|
&pos->member != (head); \
|
||||||
|
pos = list_entry(pos->member.next, typeof(*pos), member))
|
||||||
|
|
||||||
|
#define list_for_each_entry_safe(pos, n, head, member) \
|
||||||
|
for (pos = list_entry((head)->next, typeof(*pos), member), \
|
||||||
|
n = list_entry(pos->member.next, typeof(*pos), member); \
|
||||||
|
&pos->member != (head); \
|
||||||
|
pos = n, n = list_entry(n->member.next, typeof(*n), member))
|
||||||
|
|
||||||
|
void list_head_init(struct list_head *);
|
||||||
|
bool list_is_empty(const struct list_head *);
|
||||||
|
void list_add(struct list_head *, struct list_head *);
|
||||||
|
void list_del(struct list_head *);
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
#include "peer.h"
|
#include "peer.h"
|
||||||
#include "resolve.h"
|
#include "resolve.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
@@ -28,10 +29,10 @@ struct outgoing {
|
|||||||
outgoing_connection_handler handler;
|
outgoing_connection_handler handler;
|
||||||
void *passthrough;
|
void *passthrough;
|
||||||
uint32_t *count;
|
uint32_t *count;
|
||||||
struct outgoing *next;
|
struct list_head outgoing_list;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct outgoing *outgoing_head = NULL;
|
static struct list_head outgoing_head = LIST_HEAD_INIT(outgoing_head);
|
||||||
|
|
||||||
static void outgoing_connect_result(struct outgoing *, int);
|
static void outgoing_connect_result(struct outgoing *, int);
|
||||||
static void outgoing_resolve(struct outgoing *);
|
static void outgoing_resolve(struct outgoing *);
|
||||||
@@ -147,11 +148,9 @@ static void outgoing_del(struct outgoing *outgoing) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void outgoing_cleanup() {
|
void outgoing_cleanup() {
|
||||||
struct outgoing *iter = outgoing_head;
|
struct outgoing *iter, *next;
|
||||||
while (iter) {
|
list_for_each_entry_safe(iter, next, &outgoing_head, outgoing_list) {
|
||||||
struct outgoing *next = iter->next;
|
|
||||||
outgoing_del(iter);
|
outgoing_del(iter);
|
||||||
iter = next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,8 +166,7 @@ void outgoing_new(char *node, char *service, outgoing_connection_handler handler
|
|||||||
outgoing->passthrough = passthrough;
|
outgoing->passthrough = passthrough;
|
||||||
outgoing->count = count;
|
outgoing->count = count;
|
||||||
|
|
||||||
outgoing->next = outgoing_head;
|
list_add(&outgoing->outgoing_list, &outgoing_head);
|
||||||
outgoing_head = outgoing;
|
|
||||||
|
|
||||||
outgoing_resolve(outgoing);
|
outgoing_resolve(outgoing);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include "beast.h"
|
#include "beast.h"
|
||||||
#include "buf.h"
|
#include "buf.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
#include "list.h"
|
||||||
#include "peer.h"
|
#include "peer.h"
|
||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
#include "raw.h"
|
#include "raw.h"
|
||||||
@@ -26,15 +27,14 @@ struct send {
|
|||||||
struct peer *on_close;
|
struct peer *on_close;
|
||||||
uint8_t id[UUID_LEN];
|
uint8_t id[UUID_LEN];
|
||||||
struct serializer *serializer;
|
struct serializer *serializer;
|
||||||
struct send *prev;
|
struct list_head send_list;
|
||||||
struct send *next;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef void (*serialize)(struct packet *, struct buf *);
|
typedef void (*serialize)(struct packet *, struct buf *);
|
||||||
static struct serializer {
|
static struct serializer {
|
||||||
char *name;
|
char *name;
|
||||||
serialize serialize;
|
serialize serialize;
|
||||||
struct send *send_head;
|
struct list_head send_head;
|
||||||
} serializers[] = {
|
} serializers[] = {
|
||||||
{
|
{
|
||||||
.name = "airspy_adsb",
|
.name = "airspy_adsb",
|
||||||
@@ -68,14 +68,7 @@ static void send_del(struct send *send) {
|
|||||||
peer_count_out--;
|
peer_count_out--;
|
||||||
peer_epoll_del((struct peer *) send);
|
peer_epoll_del((struct peer *) send);
|
||||||
assert(!close(send->peer.fd));
|
assert(!close(send->peer.fd));
|
||||||
if (send->prev) {
|
list_del(&send->send_list);
|
||||||
send->prev->next = send->next;
|
|
||||||
} else {
|
|
||||||
send->serializer->send_head = send->next;
|
|
||||||
}
|
|
||||||
if (send->next) {
|
|
||||||
send->next->prev = send->prev;
|
|
||||||
}
|
|
||||||
peer_call(send->on_close);
|
peer_call(send->on_close);
|
||||||
free(send);
|
free(send);
|
||||||
}
|
}
|
||||||
@@ -98,13 +91,17 @@ static bool send_hello(int fd, struct serializer *serializer) {
|
|||||||
|
|
||||||
void send_init() {
|
void send_init() {
|
||||||
assert(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
|
assert(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
|
||||||
|
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
|
||||||
|
list_head_init(&serializers[i].send_head);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_cleanup() {
|
void send_cleanup() {
|
||||||
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
|
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
|
||||||
struct serializer *serializer = &serializers[i];
|
struct serializer *serializer = &serializers[i];
|
||||||
while (serializer->send_head) {
|
struct send *iter, *next;
|
||||||
send_del(serializer->send_head);
|
list_for_each_entry_safe(iter, next, &serializer->send_head, send_list) {
|
||||||
|
send_del(iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,12 +129,7 @@ void send_new(int fd, struct serializer *serializer, struct peer *on_close) {
|
|||||||
send->on_close = on_close;
|
send->on_close = on_close;
|
||||||
uuid_gen(send->id);
|
uuid_gen(send->id);
|
||||||
send->serializer = serializer;
|
send->serializer = serializer;
|
||||||
send->prev = NULL;
|
list_add(&send->send_list, &serializer->send_head);
|
||||||
send->next = serializer->send_head;
|
|
||||||
if (send->next) {
|
|
||||||
send->next->prev = send;
|
|
||||||
}
|
|
||||||
serializer->send_head = send;
|
|
||||||
|
|
||||||
peer_epoll_add((struct peer *) send, 0);
|
peer_epoll_add((struct peer *) send, 0);
|
||||||
|
|
||||||
@@ -157,7 +149,7 @@ void send_new_wrapper(int fd, void *passthrough, struct peer *on_close) {
|
|||||||
void send_write(struct packet *packet) {
|
void send_write(struct packet *packet) {
|
||||||
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
|
for (size_t i = 0; i < NUM_SERIALIZERS; i++) {
|
||||||
struct serializer *serializer = &serializers[i];
|
struct serializer *serializer = &serializers[i];
|
||||||
if (serializer->send_head == NULL) {
|
if (list_is_empty(&serializer->send_head)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
struct buf buf = BUF_INIT;
|
struct buf buf = BUF_INIT;
|
||||||
@@ -165,14 +157,13 @@ void send_write(struct packet *packet) {
|
|||||||
if (buf.length == 0) {
|
if (buf.length == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
struct send *send = serializer->send_head;
|
struct send *iter, *next;
|
||||||
while (send) {
|
list_for_each_entry_safe(iter, next, &serializer->send_head, send_list) {
|
||||||
if (write(send->peer.fd, buf_at(&buf, 0), buf.length) != (ssize_t) buf.length) {
|
if (write(iter->peer.fd, buf_at(&buf, 0), buf.length) != (ssize_t) buf.length) {
|
||||||
// peer_loop() will see this shutdown and call send_del
|
// peer_loop() will see this shutdown and call send_del
|
||||||
int res = shutdown(send->peer.fd, SHUT_WR);
|
int res = shutdown(iter->peer.fd, SHUT_WR);
|
||||||
assert(res == 0 || (res == -1 && errno == ENOTSOCK));
|
assert(res == 0 || (res == -1 && errno == ENOTSOCK));
|
||||||
}
|
}
|
||||||
send = send->next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user