/* * etcpmon_client.h - ETCP Monitor Client Network Layer * * Handles TCP connection to utun control server */ #ifndef ETCPMON_CLIENT_H #define ETCPMON_CLIENT_H #include "etcpmon_protocol.h" #include #include #include #ifdef __cplusplus extern "C" { #endif /* Graph history size (samples) */ #define GRAPH_HISTORY_SIZE 860 /* Graph metrics types */ typedef enum { GRAPH_METRIC_RTT_LAST = 0, GRAPH_METRIC_RTT_AVG10, GRAPH_METRIC_RTT_AVG100, GRAPH_METRIC_JITTER, GRAPH_METRIC_RETRANS, GRAPH_METRIC_ACKS, GRAPH_METRIC_INFLIGHT, GRAPH_METRIC_BYTES_SENT, GRAPH_METRIC_COUNT } graph_metric_type_t; /* Metrics history buffer */ struct metrics_history { float values[GRAPH_METRIC_COUNT][GRAPH_HISTORY_SIZE]; float min_val[GRAPH_METRIC_COUNT]; float max_val[GRAPH_METRIC_COUNT]; DWORD last_minmax_update; int head; int count; uint32_t last_retrans; uint32_t last_acks; uint64_t last_bytes_sent; }; /* Client state */ struct etcpmon_client { SOCKET sock; int connected; int fully_connected; /* Receive buffer */ uint8_t recv_buffer[ETCPMON_MAX_MSG_SIZE]; uint16_t recv_len; /* Server address */ char server_addr[64]; uint16_t server_port; /* Selected connection */ uint64_t selected_peer_id; /* Connection info cache */ struct etcpmon_conn_info* conn_list; uint8_t conn_count; /* Latest metrics */ struct etcpmon_rsp_metrics* last_metrics; struct etcpmon_link_metrics* last_links; uint8_t last_links_count; /* Log file */ FILE* log_file; /* Metrics history for graph */ struct metrics_history history; /* Pending requests queue with callbacks */ struct etcpmon_pending_request* pending_head; struct etcpmon_pending_request* pending_tail; uint8_t next_seq_id; /* Last received response seq_id (for matching) */ uint8_t last_response_seq_id; /* Callbacks */ void (*on_connected)(void* user_data); void (*on_disconnected)(void* user_data); void (*on_conn_list)(struct etcpmon_conn_info* list, uint8_t count, void* user_data); void (*on_metrics)(struct etcpmon_rsp_metrics* metrics, struct etcpmon_link_metrics* links, uint8_t links_count, void* user_data); void (*on_error)(const char* msg, void* user_data); void* user_data; uint64_t last_metrics_request_ms; }; /* Pending request with callback */ struct etcpmon_pending_request { uint8_t seq_id; /* Sequence ID */ void (*callback)(void* arg, uint8_t msg_type, const uint8_t* payload, uint16_t payload_size); void* arg; /* User argument for callback */ struct etcpmon_pending_request* next; }; /* Initialize client structure */ void etcpmon_client_init(struct etcpmon_client* client); /* Cleanup client resources */ void etcpmon_client_cleanup(struct etcpmon_client* client); /* Connect to server * Returns 0 on success, -1 on error */ int etcpmon_client_connect(struct etcpmon_client* client, const char* addr, uint16_t port); /* Disconnect from server */ void etcpmon_client_disconnect(struct etcpmon_client* client); /* Check if connected */ int etcpmon_client_is_connected(const struct etcpmon_client* client); /* Request connection list * Returns 0 on success, -1 on error */ int etcpmon_client_request_list(struct etcpmon_client* client); /* Select connection to monitor * Returns 0 on success, -1 on error */ int etcpmon_client_select_connection(struct etcpmon_client* client, uint64_t peer_node_id); /* Request metrics for selected connection * Returns 0 on success, -1 on error */ int etcpmon_client_request_metrics(struct etcpmon_client* client); /* Send request with callback * callback will be called when response is received * arg will be passed to callback * Returns 0 on success (queued), -1 on error */ int etcpmon_client_send_request(struct etcpmon_client* client, uint8_t type, const void* payload, uint16_t payload_size, void (*callback)(void* arg, uint8_t msg_type, const uint8_t* payload, uint16_t payload_size), void* arg); /* Process incoming data (non-blocking) * Should be called periodically (e.g., from window message loop) * Returns 1 if data was processed, 0 if no data, -1 on error */ int etcpmon_client_process(struct etcpmon_client* client); /* Set callbacks */ void etcpmon_client_set_callbacks(struct etcpmon_client* client, void (*on_connected)(void*), void (*on_disconnected)(void*), void (*on_conn_list)(struct etcpmon_conn_info*, uint8_t, void*), void (*on_metrics)(struct etcpmon_rsp_metrics*, struct etcpmon_link_metrics*, uint8_t, void*), void (*on_error)(const char*, void*), void* user_data); /* Get connection name by peer_id */ const char* etcpmon_client_get_conn_name(struct etcpmon_client* client, uint64_t peer_id); /* Get metrics history */ struct metrics_history* etcpmon_client_get_history(struct etcpmon_client* client); /* Add metrics to history */ void etcpmon_client_add_to_history(struct etcpmon_client* client, struct etcpmon_rsp_metrics* metrics); uint64_t get_timestamp_ms(void); // Add this declaration #ifdef __cplusplus } #endif #endif /* ETCPMON_CLIENT_H */