2017-03-10 11:44:40 +01:00
|
|
|
/* Helper for simple message queues. */
|
2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_MSG_QUEUE_H
|
|
|
|
#define LIGHTNING_COMMON_MSG_QUEUE_H
|
2017-03-10 11:44:40 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/short_types/short_types.h>
|
|
|
|
|
2017-03-19 21:24:14 +01:00
|
|
|
/* Reserved type used to indicate we're actually passing an fd. */
|
|
|
|
#define MSG_PASS_FD 0xFFFF
|
|
|
|
|
2022-01-11 02:16:18 +01:00
|
|
|
/* Allocate a new msg queue; if we control all msgs we send/receive,
|
|
|
|
* we can pass fds. Otherwise, set @fd_passing to false. */
|
|
|
|
struct msg_queue *msg_queue_new(const tal_t *ctx, bool fd_passing);
|
2017-03-10 11:44:40 +01:00
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
/* If add is taken(), freed after sending. msg_wake() implied. */
|
2018-10-25 01:46:08 +02:00
|
|
|
void msg_enqueue(struct msg_queue *q, const u8 *add TAKES);
|
2017-03-10 11:44:40 +01:00
|
|
|
|
2019-05-02 02:55:17 +02:00
|
|
|
/* Get current queue length */
|
|
|
|
size_t msg_queue_length(const struct msg_queue *q);
|
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
/* Fd is closed after sending. msg_wake() implied. */
|
2017-03-19 21:24:14 +01:00
|
|
|
void msg_enqueue_fd(struct msg_queue *q, int fd);
|
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
/* Explicitly wake up a msg_queue_wait */
|
|
|
|
void msg_wake(const struct msg_queue *q);
|
|
|
|
|
2017-03-19 21:24:14 +01:00
|
|
|
/* Returns NULL if nothing to do. */
|
2017-03-10 11:44:40 +01:00
|
|
|
const u8 *msg_dequeue(struct msg_queue *q);
|
|
|
|
|
2017-03-19 21:24:14 +01:00
|
|
|
/* Returns -1 if not an fd: close after sending. */
|
2022-01-11 02:16:18 +01:00
|
|
|
int msg_extract_fd(const struct msg_queue *q, const u8 *msg);
|
2017-03-19 21:24:14 +01:00
|
|
|
|
2017-03-10 11:44:40 +01:00
|
|
|
#define msg_queue_wait(conn, q, next, arg) \
|
|
|
|
io_out_wait((conn), (q), (next), (arg))
|
|
|
|
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_MSG_QUEUE_H */
|