| |
| #pragma once |
| #include <stdint.h> |
| #include <stddef.h> |
| #include <stdbool.h> |
| #include <pthread.h> |
|
|
| |
| #define HK_QUEUE_MAX 256 |
| #define HK_MAX_SUBS_PER_CONN 64 |
| #define HK_MAX_TOPIC_LEN 128 |
| #define HK_MAX_CONNS 256 |
| #define HK_MAX_ID_LEN 64 |
| #define HK_MAX_BODY 8192 |
| #define HK_SOCK_PATH "/tmp/hk.sock" |
|
|
| |
| typedef struct { |
| char type[32]; |
| char from[64]; |
| char to[64]; |
| char topic[128]; |
| uint64_t corr; |
| char body[HK_MAX_BODY]; |
| } hk_message_t; |
|
|
| |
| typedef struct { |
| char topic[HK_MAX_TOPIC_LEN]; |
| bool active; |
| } hk_subscription_t; |
|
|
| |
| typedef struct { |
| hk_message_t msg; |
| bool used; |
| } hk_queue_entry_t; |
|
|
| |
| typedef struct hk_conn hk_conn_t; |
| struct hk_conn { |
| char id[HK_MAX_ID_LEN]; |
| int fd; |
| hk_subscription_t subs[HK_MAX_SUBS_PER_CONN]; |
| uint32_t sub_count; |
| pthread_mutex_t q_lock; |
| pthread_cond_t q_cond; |
| hk_queue_entry_t queue[HK_QUEUE_MAX]; |
| uint32_t q_head; |
| uint32_t q_tail; |
| uint32_t q_count; |
| pthread_t thread; |
| bool active; |
| struct hk_bus *bus; |
| |
| uint64_t msgs_sent; |
| uint64_t msgs_recv; |
| uint64_t drops; |
| }; |
|
|
| |
| typedef struct hk_bus { |
| pthread_mutex_t lock; |
| hk_conn_t *conns[HK_MAX_CONNS]; |
| uint32_t conn_count; |
| int server_fd; |
| pthread_t accept_thread; |
| bool running; |
| |
| uint64_t total_published; |
| uint64_t total_routed; |
| uint64_t total_dropped; |
| } hk_bus_t; |
|
|
| |
| int hk_msg_encode (const hk_message_t *m, char *out, size_t out_sz); |
| int hk_msg_decode (const char *data, size_t len, hk_message_t *out); |
|
|
| int hk_bus_init (hk_bus_t *bus, const char *sock_path); |
| void hk_bus_destroy (hk_bus_t *bus); |
|
|
| int hk_bus_connect (hk_bus_t *bus, const char *id, hk_conn_t **out); |
| void hk_bus_disconnect(hk_bus_t *bus, hk_conn_t *conn); |
|
|
| int hk_bus_publish (hk_bus_t *bus, const hk_message_t *msg); |
| int hk_bus_subscribe (hk_conn_t *conn, const char *topic); |
| int hk_bus_unsubscribe(hk_conn_t *conn, const char *topic); |
|
|
| |
| |
| int hk_bus_route (hk_bus_t *bus, const hk_message_t *msg); |
|
|
| |
| |
| int hk_bus_recv (hk_conn_t *conn, hk_message_t *out, uint32_t timeout_ms); |
|
|
| |
| typedef struct { |
| uint64_t total_published; |
| uint64_t total_routed; |
| uint64_t total_dropped; |
| uint32_t active_connections; |
| } hk_bus_stats_t; |
|
|
| void hk_bus_get_stats(const hk_bus_t *bus, hk_bus_stats_t *out); |
|
|