33#ifndef DETERMINISTICESPASYNCWEBSERVER_TRANSPORT_H
34#define DETERMINISTICESPASYNCWEBSERVER_TRANSPORT_H
37#include "freertos/FreeRTOS.h"
38#include "freertos/queue.h"
92#if DETWS_ENABLE_HTTP2 || DETWS_ENABLE_HTTP3
96 bool (*resp_sink)(uint8_t slot,
int code,
const char *content_type,
const char *body,
size_t len);
103#if DETWS_ENABLE_HTTP3
111#define DETWS_PROTO_SLOT_NONE 0xFFu
235bool det_conn_send(uint8_t slot,
const void *data, u16_t len);
305static inline size_t det_conn_available(uint8_t slot)
312static inline bool det_conn_read_byte(uint8_t slot, uint8_t *out)
319static inline void det_conn_peek(uint8_t slot,
size_t off, uint8_t *dst,
size_t n)
326static inline void det_conn_consume(uint8_t slot,
size_t n)
332static inline size_t det_conn_read(uint8_t slot, uint8_t *buf,
size_t cap)
347static inline bool det_conn_active(uint8_t slot)
354static inline DetIface det_conn_iface(uint8_t slot)
360static inline uint8_t det_conn_listener_id(uint8_t slot)
456#if DETWS_ENABLE_OBSERVABILITY
459enum class DetConnReason : uint8_t
462 DET_CONN_R_CLOSE_REMOTE,
463 DET_CONN_R_CLOSE_LOCAL,
468 DET_CONN_R_BACKPRESSURE,
469 DET_CONN_R_DEFER_DROP
473struct DetConnCounters
476 uint32_t closes_remote;
477 uint32_t closes_local;
478 uint32_t closes_error;
479 uint32_t closes_timeout;
480 uint32_t closes_abort;
481 uint32_t backpressure;
482 uint32_t defer_drops;
483 uint32_t closing_gauge;
494typedef void (*DetConnEventCb)(uint8_t slot,
ConnState old_state,
ConnState new_state, DetConnReason reason);
497void det_conn_on_event(DetConnEventCb cb);
500DetConnCounters det_conn_counters();
503void det_conn_counters_reset();
507void detws_obs_transition(uint8_t slot,
ConnState olds,
ConnState news, DetConnReason reason);
508void detws_obs_notice(uint8_t slot,
ConnState st, DetConnReason reason);
509#define DETWS_OBS_TRANSITION(slot, olds, news, reason) detws_obs_transition((slot), (olds), (news), (reason))
510#define DETWS_OBS_NOTICE(slot, st, reason) detws_obs_notice((slot), (st), (reason))
516#define DETWS_OBS_TRANSITION(slot, olds, news, reason) ((void)0)
517#define DETWS_OBS_NOTICE(slot, st, reason) ((void)0)
529err_t
lowlevel_recv_cb(
void *arg,
struct tcp_pcb *tpcb,
struct pbuf *p, err_t err);
User-facing configuration for DeterministicESPAsyncWebServer.
DetIface
Network interface a connection arrived on (for per-route filtering).
ConnProto
Application protocol spoken on a listener port or connection slot.
#define RX_BUF_SIZE
Ring-buffer capacity in bytes per connection slot.
Static-only facade managing the shared TCP connection pool.
static void check_timeouts(int worker_id=0)
Scan the pool and force-close connections idle for > conn_timeout_ms.
static void pool_init(const WebServerConfig *cfg=nullptr)
Initialize the connection pool and store the runtime config.
static uint32_t conn_timeout_ms
Runtime connection-idle timeout in milliseconds.
static void stop()
Abort all active connections and reset the pool to ConnState::CONN_FREE.
Layer 3 (Network) - a family-tagged IP address (IPv4 or IPv6) with RFC-faithful text parsing,...
Shared single-producer / single-consumer byte-ring primitive.
A v4 or v6 address in network (big-endian) byte order.
A single TCP connection context.
DetIface iface
Interface this connection arrived on; set at accept time.
struct tcp_pcb * pcb
lwIP PCB; null when slot is free.
uint32_t last_activity_ms
millis() timestamp of last TX/RX event.
uint8_t id
Fixed slot index (0 … MAX_CONNS-1).
uint8_t rx_buffer[RX_BUF_SIZE]
Ring buffer storage.
uint8_t owner
Worker that owns this slot (round-robin at accept). Always 0 at N=1.
uint8_t listener_id
Index into listener_pool[]; set at accept time.
DetAtomic< size_t > rx_tail
Consumer read index (worker context).
DetAtomic< size_t > rx_head
Producer write index (lwIP/tcpip context).
DetAtomic< ConnState > state
Lifecycle state; acquire/release for inter-task visibility.
uint8_t tls
Non-zero when this connection is TLS (set at accept time).
uint8_t proto_slot
Per-protocol session/pool index (0xFF = none): the SSH session, an MQTT/Modbus session,...
ConnProto proto
Application protocol for this connection.
Event record posted from lwIP callbacks to the session layer.
EvtType type
What happened.
size_t data_len
Bytes copied (EvtType::EVT_DATA only); 0 for other types.
uint8_t slot_id
Which connection slot is affected.
Runtime-tunable server parameters.
uint32_t det_conn_remote_ip(uint8_t slot)
Raw source IPv4 of the connection in slot, or 0 if the slot has no active pcb (or on host builds)....
bool det_conn_send(uint8_t slot, const void *data, u16_t len)
Send len bytes on connection slot (copies data; TLS-aware).
void det_conn_flush(uint8_t slot)
Flush queued bytes / finish the send on slot (TLS-aware).
void det_conn_begin_close(uint8_t slot_id)
Begin a graceful close that dwells in ConnState::CONN_CLOSING until the peer ACKs.
uint32_t detws_ap_ip
softAP IPv4 address (network byte order) for STA/AP interface tagging.
u16_t det_conn_sndbuf(uint8_t slot)
Bytes that can currently be queued for sending on slot.
void det_conn_close(uint8_t slot)
Close connection slot gracefully (tcp_close), aborting if the FIN cannot be queued....
void det_conn_ack_consumed(uint8_t slot)
Reopen the TCP receive window by however much slot has drained.
TcpConn conn_pool[CONN_POOL_SLOTS]
Static pool of connection contexts. Defined in tcp.cpp. Sized CONN_POOL_SLOTS: MAX_CONNS TCP slots pl...
bool listener_enqueue(uint8_t listener_id, const TcpEvt *evt)
Post evt to the queue owned by listener listener_id.
bool det_conn_send_flush(uint8_t slot, const void *data, u16_t len)
Send len bytes on slot and flush in a single tcpip_thread round-trip.
err_t lowlevel_recv_cb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
lwIP receive callback - wired to each new connection by listener_accept_cb.
void det_lwip_to_detip(const ip_addr_t *ra, DetIp *out)
A stable per-peer 32-bit identity key for slot (the v4 address, or an FNV-1a hash of a v6 address)....
bool det_conn_raw_send(struct tcp_pcb *pcb, const void *data, u16_t len)
Write raw bytes straight to pcb (no TLS), context-safe.
err_t lowlevel_sent_cb(void *arg, struct tcp_pcb *tpcb, u16_t len)
lwIP sent callback - refreshes the idle-timeout timestamp.
void lowlevel_err_cb(void *arg, err_t err)
lwIP error callback - fires when the stack detects a fatal error.
void det_conn_touch_active(uint8_t slot)
Refresh slot's idle-timeout timestamp while a response body is in flight.
bool det_conn_remote_addr(uint8_t slot, DetIp *out)
The connected peer's address as a family-tagged DetIp (IPv4 or IPv6).
void det_conn_detach(struct tcp_pcb *pcb)
Detach pcb from its slot's lwIP callbacks before the slot is freed.
void det_conn_abort_slot(uint8_t slot)
Hard-abort connection slot (RST) for a fatal condition. The transport owns the teardown order: free t...
EvtType
Type of connection event posted to a listener's FreeRTOS queue.
@ EVT_DATA
Data received; bytes are already in the ring buffer.
@ EVT_CONNECT
New connection accepted.
@ EVT_DISCONNECT
Remote peer closed the connection gracefully.
@ EVT_ERROR
lwIP reported an error (PCB may already be freed).
ConnState
Lifecycle state of a connection pool slot.
@ CONN_CLOSING
FIN sent; waiting for final ACK (reserved).
@ CONN_ACTIVE
Live connection; PCB is valid.
@ CONN_FREE
Slot is available; no PCB is attached.
void det_conn_abort(struct tcp_pcb *pcb)
Hard-abort pcb (RST) for a fatal condition; no graceful FIN.
uint8_t det_conn_active_count()
Number of server connection slots currently in the CONN_ACTIVE state.