diff --git a/lightningd/Makefile b/lightningd/Makefile index e243ee643..bf75acb0c 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -46,7 +46,8 @@ LIGHTNINGD_LIB_SRC := \ lightningd/funding_tx.c \ lightningd/htlc_tx.c \ lightningd/key_derive.c \ - lightningd/peer_failed.c \ + lightningd/msg_queue.c \ + lightningd/peer_failed.c \ lightningd/utxo.c LIGHTNINGD_LIB_OBJS := $(LIGHTNINGD_LIB_SRC:.c=.o) diff --git a/lightningd/msg_queue.c b/lightningd/msg_queue.c new file mode 100644 index 000000000..5c0678c43 --- /dev/null +++ b/lightningd/msg_queue.c @@ -0,0 +1,30 @@ +#include + +void msg_queue_init(struct msg_queue *q, const tal_t *ctx) +{ + q->q = tal_arr(ctx, const u8 *, 0); +} + +void msg_enqueue(struct msg_queue *q, const u8 *add) +{ + size_t n = tal_count(q->q); + tal_resize(&q->q, n+1); + q->q[n] = add; + + /* In case someone is waiting */ + io_wake(q); +} + +const u8 *msg_dequeue(struct msg_queue *q) +{ + size_t n = tal_count(q->q); + const u8 *msg; + + if (!n) + return NULL; + + msg = q->q[0]; + memmove(q->q, q->q + 1, sizeof(*q->q) * (n-1)); + tal_resize(&q->q, n-1); + return msg; +} diff --git a/lightningd/msg_queue.h b/lightningd/msg_queue.h new file mode 100644 index 000000000..fc69d9b6e --- /dev/null +++ b/lightningd/msg_queue.h @@ -0,0 +1,21 @@ +/* Helper for simple message queues. */ +#ifndef LIGHTNING_LIGHTNINGD_MSG_QUEUE_H +#define LIGHTNING_LIGHTNINGD_MSG_QUEUE_H +#include "config.h" +#include +#include + +struct msg_queue { + const u8 **q; +}; + +void msg_queue_init(struct msg_queue *q, const tal_t *ctx); + +void msg_enqueue(struct msg_queue *q, const u8 *add); + +const u8 *msg_dequeue(struct msg_queue *q); + +#define msg_queue_wait(conn, q, next, arg) \ + io_out_wait((conn), (q), (next), (arg)) + +#endif /* LIGHTNING_LIGHTNINGD_MSG_QUEUE_H */