#ifndef MAZE32_GATEWAY_H #define MAZE32_GATEWAY_H #include #include #include "esp_err.h" #include "esp_http_server.h" #include "esp_timer.h" #include "maze32_game.h" #include "maze32_lobby.h" #include "maze32_maze.h" #ifdef __cplusplus extern "C" { #endif #define MAZE32_GATEWAY_CONNECTION_LIMIT MAZE32_SESSION_LIMIT #define MAZE32_GATEWAY_QUEUE_LIMIT 12U #define MAZE32_GATEWAY_OUTGOING_LIMIT 768U #define MAZE32_GATEWAY_RATE_LIMIT_PER_SECOND 20U #define MAZE32_GATEWAY_RATE_LIMIT_CLOSE_THRESHOLD 40U #define MAZE32_GATEWAY_PLAYER_LATENCY_BUDGET_MS 500U typedef struct { int active; int socket_fd; uint32_t last_client_sequence; uint64_t rate_window_started_ms; unsigned int messages_in_window; int closing; } maze32_gateway_connection_t; typedef struct { int socket_fd; size_t length; int drop_if_busy; char payload[MAZE32_GATEWAY_OUTGOING_LIMIT]; } maze32_gateway_outgoing_t; typedef struct { uint32_t connection_high_water; uint32_t queue_high_water; uint32_t messages_received; uint32_t messages_sent; uint32_t messages_dropped; uint32_t send_failures; uint32_t malformed_messages; uint32_t oversized_messages; uint32_t rate_limited_messages; uint32_t accepted_game_commands; uint32_t matches_started; uint32_t matches_completed; uint32_t matches_reset; uint64_t maximum_handler_us; uint64_t maximum_send_us; size_t minimum_internal_free_bytes; size_t minimum_internal_largest_block_bytes; } maze32_gateway_metrics_t; typedef struct { httpd_handle_t server; maze32_lobby_t lobby; maze32_gateway_connection_t connections[MAZE32_GATEWAY_CONNECTION_LIMIT]; maze32_gateway_outgoing_t outgoing[MAZE32_GATEWAY_QUEUE_LIMIT]; size_t outgoing_head; size_t outgoing_count; uint32_t server_sequence; unsigned int suggestion_index; esp_timer_handle_t countdown_timer; esp_timer_handle_t reconnect_timer; esp_timer_handle_t finish_timer; maze32_gateway_metrics_t metrics; maze32_maze_t maze; maze32_maze_workspace_t maze_workspace; maze32_game_t game; maze32_view_t view; } maze32_gateway_t; esp_err_t maze32_gateway_init(maze32_gateway_t* gateway); void maze32_gateway_set_server(maze32_gateway_t* gateway, httpd_handle_t server); esp_err_t maze32_gateway_register(httpd_handle_t server, maze32_gateway_t* gateway); void maze32_gateway_close_socket(httpd_handle_t server, int socket_fd); int maze32_gateway_can_reconfigure(const maze32_gateway_t* gateway); size_t maze32_gateway_format_diagnostics(maze32_gateway_t* gateway, char* output, size_t output_size); #ifdef __cplusplus } #endif #endif