2021-02-01 03:58:50 +01:00
|
|
|
#include "config.h"
|
2017-03-19 21:24:14 +01:00
|
|
|
#include <assert.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/msg_queue.h>
|
2018-09-27 02:19:24 +02:00
|
|
|
#include <common/utils.h>
|
2017-03-19 21:24:14 +01:00
|
|
|
#include <wire/wire.h>
|
2017-03-10 11:44:40 +01:00
|
|
|
|
2018-10-25 01:46:08 +02:00
|
|
|
struct msg_queue {
|
2022-01-11 02:16:18 +01:00
|
|
|
bool fd_passing;
|
2018-10-25 01:46:08 +02:00
|
|
|
const u8 **q;
|
|
|
|
};
|
|
|
|
|
2022-01-11 02:16:18 +01:00
|
|
|
struct msg_queue *msg_queue_new(const tal_t *ctx, bool fd_passing)
|
2017-03-10 11:44:40 +01:00
|
|
|
{
|
2018-10-25 01:46:08 +02:00
|
|
|
struct msg_queue *q = tal(ctx, struct msg_queue);
|
2022-01-11 02:16:18 +01:00
|
|
|
q->fd_passing = fd_passing;
|
2018-10-25 01:46:08 +02:00
|
|
|
q->q = tal_arr(q, const u8 *, 0);
|
|
|
|
return q;
|
2017-03-10 11:44:40 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 01:46:08 +02:00
|
|
|
static void do_enqueue(struct msg_queue *q, const u8 *add TAKES)
|
2017-03-10 11:44:40 +01:00
|
|
|
{
|
2020-02-27 03:17:01 +01:00
|
|
|
tal_arr_expand(&q->q, tal_dup_talarr(q, u8, add));
|
2017-03-10 11:44:40 +01:00
|
|
|
|
|
|
|
/* In case someone is waiting */
|
|
|
|
io_wake(q);
|
|
|
|
}
|
|
|
|
|
2019-05-02 02:55:17 +02:00
|
|
|
size_t msg_queue_length(const struct msg_queue *q)
|
|
|
|
{
|
|
|
|
return tal_count(q->q);
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:24:14 +01:00
|
|
|
void msg_enqueue(struct msg_queue *q, const u8 *add)
|
|
|
|
{
|
2022-01-11 02:16:18 +01:00
|
|
|
if (q->fd_passing)
|
|
|
|
assert(fromwire_peektype(add) != MSG_PASS_FD);
|
2017-03-19 21:24:14 +01:00
|
|
|
do_enqueue(q, add);
|
|
|
|
}
|
|
|
|
|
|
|
|
void msg_enqueue_fd(struct msg_queue *q, int fd)
|
|
|
|
{
|
2018-10-25 01:46:08 +02:00
|
|
|
u8 *fdmsg = tal_arr(q, u8, 0);
|
2022-01-11 02:16:18 +01:00
|
|
|
assert(q->fd_passing);
|
2017-03-19 21:24:14 +01:00
|
|
|
towire_u16(&fdmsg, MSG_PASS_FD);
|
|
|
|
towire_u32(&fdmsg, fd);
|
|
|
|
do_enqueue(q, take(fdmsg));
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:44:40 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-03-19 21:24:14 +01:00
|
|
|
|
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
|
|
|
{
|
|
|
|
const u8 *p = msg + sizeof(u16);
|
|
|
|
size_t len = tal_count(msg) - sizeof(u16);
|
|
|
|
|
2022-01-11 02:16:18 +01:00
|
|
|
assert(q->fd_passing);
|
2017-03-19 21:24:14 +01:00
|
|
|
if (fromwire_peektype(msg) != MSG_PASS_FD)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return fromwire_u32(&p, &len);
|
|
|
|
}
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
void msg_wake(const struct msg_queue *q)
|
|
|
|
{
|
|
|
|
io_wake(q);
|
|
|
|
}
|