Add discovery module: UDP multicast announcements and peer tracking
Sends 6-byte framed announcements to 224.0.0.251:5353 on startup and every interval_ms (default 5s). Receive thread maintains a peer table (max 64 entries); fires on_peer_found for new peers, on_peer_lost when a peer misses timeout_intervals (default 3) consecutive intervals. Own announcements are filtered by name+site_id. SO_REUSEADDR+REUSEPORT allows multiple processes on the same host for testing. discovery_cli: announce <name> <tcp_port> [flags] — prints found/lost events. Also notes future config module in planning.md. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
50
include/discovery.h
Normal file
50
include/discovery.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "error.h"
|
||||
|
||||
#define DISCOVERY_MULTICAST_GROUP "224.0.0.251"
|
||||
#define DISCOVERY_PORT 5353
|
||||
#define DISCOVERY_PROTOCOL_VERSION 1
|
||||
#define DISCOVERY_MAX_NAME_LEN 255
|
||||
#define DISCOVERY_MAX_PEERS 64
|
||||
|
||||
/* function_flags bits — matches protocol doc */
|
||||
#define DISCOVERY_FLAG_SOURCE (1u << 0)
|
||||
#define DISCOVERY_FLAG_RELAY (1u << 1)
|
||||
#define DISCOVERY_FLAG_SINK (1u << 2)
|
||||
#define DISCOVERY_FLAG_CONTROLLER (1u << 3)
|
||||
|
||||
struct Discovery_Peer {
|
||||
uint32_t addr; /* source IPv4, network byte order */
|
||||
uint16_t site_id;
|
||||
uint16_t tcp_port;
|
||||
uint16_t function_flags;
|
||||
char name[DISCOVERY_MAX_NAME_LEN + 1];
|
||||
};
|
||||
|
||||
typedef void (*Discovery_Peer_Found_Cb)(const struct Discovery_Peer *peer, void *userdata);
|
||||
typedef void (*Discovery_Peer_Lost_Cb)(const struct Discovery_Peer *peer, void *userdata);
|
||||
|
||||
struct Discovery_Config {
|
||||
/* our own identity */
|
||||
uint16_t site_id;
|
||||
uint16_t tcp_port;
|
||||
uint16_t function_flags;
|
||||
const char *name;
|
||||
|
||||
/* timing */
|
||||
uint32_t interval_ms; /* announcement interval; 0 = use default (5000) */
|
||||
uint32_t timeout_intervals; /* missed intervals before peer_lost; 0 = use default (3) */
|
||||
|
||||
/* callbacks */
|
||||
Discovery_Peer_Found_Cb on_peer_found; /* required */
|
||||
Discovery_Peer_Lost_Cb on_peer_lost; /* optional */
|
||||
void *userdata;
|
||||
};
|
||||
|
||||
struct Discovery;
|
||||
|
||||
struct App_Error discovery_create(struct Discovery **out, struct Discovery_Config *config);
|
||||
struct App_Error discovery_start(struct Discovery *d);
|
||||
void discovery_destroy(struct Discovery *d);
|
||||
Reference in New Issue
Block a user