1/* @ajax/mesh-top-client: one background thread per meshd endpoint (crates/mesh-top src/client.rs).
2 * A persistent control-port connection polls `snap` and runs the viewer's commands in between;
3 * the UI never blocks on the network, it only pops mt_updates from a shared queue. Real time
4 * (CLOCK_MONOTONIC), never mesh-top-clock's virtual clock. */
5#ifndef MESH_TOP_CLIENT_H
6#define MESH_TOP_CLIENT_H
78#include "mesh/top_app.h"
910#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
1314/* A binary reply larger than this is a protocol error, not data. */
15#define MT_MAX_REPLY ((size_t)16 << 20)
16#define MT_IO_TIMEOUT_MS 3000u
17#define MT_BACKOFF_MIN_MS 100u
18#define MT_BACKOFF_MAX_MS 2000u
1920/* How long a request's reply may take (ms): past meshd's own budget for it. */
21uint32_t mt_reply_timeout_ms(const char *line);
22/* Whether the request's reply is framed (u32 LE len + bytes) rather than a text line. */
23bool mt_binary_reply(const char *line);
2425/* The updates every endpoint sends to (one receiver). Free the endpoints before the queue. */
26typedef struct mt_updates mt_updates;
27mt_updates *mt_updates_new(void);
28void mt_updates_free(mt_updates *q); /* frees updates never popped */
29void mt_updates_push(mt_updates *q, mt_update u);
30/* Waits up to timeout_ms (0: don't wait) for an update; false when none came. */
31bool mt_updates_pop(mt_updates *q, uint32_t timeout_ms, mt_update *out);
32/* Frees an update that was not handed to mt_app_apply. */
33void mt_update_free(mt_update *u);
3435typedef struct mt_endpoint mt_endpoint;
36/* Starts the endpoint thread for machine idx. */
37mt_endpoint *mt_endpoint_spawn(size_t idx, const char *addr, mt_updates *updates, uint64_t interval_ms);
38/* Queues a command line (its reply comes back as an MT_UPDATE_REPLY). */
39void mt_endpoint_send(mt_endpoint *e, const char *line);
40/* Commands sent and not yet answered (the UI drops repeat keys past a few). */
41size_t mt_endpoint_pending(const mt_endpoint *e);
42/* The snap poll period; the Doom tab polls faster. */
43void mt_endpoint_set_interval(mt_endpoint *e, uint64_t ms);
44uint64_t mt_endpoint_interval(const mt_endpoint *e);
45const char *mt_endpoint_addr(const mt_endpoint *e);
46/* Stops the thread (interrupting any request in flight) and frees the endpoint. */
47void mt_endpoint_free(mt_endpoint *e);
4849#endif