33#ifndef PROTOCORE_TCP_H
34#define PROTOCORE_TCP_H
36#include "freertos/FreeRTOS.h"
37#include "freertos/queue.h"
95#if PC_ENABLE_HTTP2 || PC_ENABLE_HTTP3
99 bool (*pc_resp_sink)(uint8_t slot,
int code,
const char *content_type,
const char *body,
size_t len);
103 uint8_t pc_h2_checked;
104 uint32_t pc_h2_stream;
108 uint32_t pc_h3_conn_id;
109 uint64_t pc_h3_stream;
114#define PC_PROTO_SLOT_NONE 0xFFu
247bool pc_conn_send(uint8_t slot,
const void *data, u16_t len);
317static inline size_t pc_conn_available(uint8_t slot)
324static inline bool pc_conn_read_byte(uint8_t slot, uint8_t *out)
331static inline void pc_conn_peek(uint8_t slot,
size_t off, uint8_t *dst,
size_t n)
338static inline void pc_conn_consume(uint8_t slot,
size_t n)
344static inline size_t pc_conn_read(uint8_t slot, uint8_t *buf,
size_t cap)
359static inline bool pc_conn_active(uint8_t slot)
366static inline pc_iface pc_conn_iface(uint8_t slot)
372static inline uint8_t pc_conn_listener_id(uint8_t slot)
468#if PC_ENABLE_OBSERVABILITY
471enum class pc_conn_reason : uint8_t
474 PC_CONN_R_CLOSE_REMOTE,
475 PC_CONN_R_CLOSE_LOCAL,
480 PC_CONN_R_BACKPRESSURE,
485struct pc_conn_counters
488 uint32_t closes_remote;
489 uint32_t closes_local;
490 uint32_t closes_error;
491 uint32_t closes_timeout;
492 uint32_t closes_abort;
493 uint32_t backpressure;
494 uint32_t defer_drops;
495 uint32_t closing_gauge;
506typedef void (*pc_conn_event_cb)(uint8_t slot,
ConnState old_state,
ConnState new_state, pc_conn_reason reason);
509void pc_conn_on_event(pc_conn_event_cb cb);
512pc_conn_counters pc_conn_counters_get();
515void pc_conn_counters_reset();
519void pc_obs_transition(uint8_t slot,
ConnState olds,
ConnState news, pc_conn_reason reason);
520void pc_obs_notice(uint8_t slot,
ConnState st, pc_conn_reason reason);
521#define PC_OBS_TRANSITION(slot, olds, news, reason) pc_obs_transition((slot), (olds), (news), (reason))
522#define PC_OBS_NOTICE(slot, st, reason) pc_obs_notice((slot), (st), (reason))
528#define PC_OBS_TRANSITION(slot, olds, news, reason) ((void)0)
529#define PC_OBS_NOTICE(slot, st, reason) ((void)0)
541err_t
lowlevel_recv_cb(
void *arg,
struct tcp_pcb *tpcb,
struct pbuf *p, err_t err);
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,...
User-facing configuration for ProtoCore.
ConnProto
Application protocol spoken on a listener port or connection slot.
pc_iface
Network interface a connection arrived on (for per-route filtering).
Shared single-producer / single-consumer byte-ring primitive.
A single TCP connection context.
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.
pc_atomic< size_t > rx_tail
Consumer read index (worker context).
pc_iface iface
Interface this connection arrived on; set at accept time.
uint8_t listener_id
Index into listener_pool[]; set at accept time.
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,...
pc_atomic< ConnState > state
Lifecycle state; acquire/release for inter-task visibility.
ConnProto proto
Application protocol for this connection.
pc_atomic< size_t > rx_head
Producer write index (lwIP/tcpip context).
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.
A v4 or v6 address in network (big-endian) byte order.
void pc_conn_set_state(uint8_t slot, ConnState st)
The single conn_pool[slot].state write path. Writes the state (release) and keeps the free-slot bitma...
int32_t pc_conn_alloc_free()
First CONN_FREE slot via a ctz on the bitmask (replaces the MAX_CONNS scan); -1 if the pool is full....
void pc_conn_detach(struct tcp_pcb *pcb)
Detach pcb from its slot's lwIP callbacks before the slot is freed.
void pc_conn_abort_slot(uint8_t slot)
Hard-abort connection slot (RST) for a fatal condition. The transport owns the teardown order: free t...
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.
void pc_lwip_to_ip(const ip_addr_t *ra, pc_ip *out)
A stable per-peer 32-bit identity key for slot (the v4 address, or an FNV-1a hash of a v6 address)....
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.
bool pc_conn_raw_send(struct tcp_pcb *pcb, const void *data, u16_t len)
Write raw bytes straight to pcb (no TLS), context-safe.
void pc_conn_flush(uint8_t slot)
Flush queued bytes / finish the send on slot (TLS-aware).
uint32_t pc_ap_ip
softAP IPv4 address (network byte order) for STA/AP interface tagging.
u16_t pc_conn_sndbuf(uint8_t slot)
Bytes that can currently be queued for sending on slot.
uint32_t pc_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)....
void pc_conn_ack_consumed(uint8_t slot)
Reopen the TCP receive window by however much slot has drained.
void pc_conn_close(uint8_t slot)
Close connection slot gracefully (tcp_close), aborting if the FIN cannot be queued....
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.
bool pc_conn_send(uint8_t slot, const void *data, u16_t len)
Send len bytes on connection slot (copies data; TLS-aware).
void pc_conn_touch_active(uint8_t slot)
Refresh slot's idle-timeout timestamp while a response body is in flight.
uint8_t pc_conn_active_count()
Number of server connection slots currently in the CONN_ACTIVE state.
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).
void pc_conn_begin_close(uint8_t slot_id)
Begin a graceful close that dwells in ConnState::CONN_CLOSING until the peer ACKs.
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.
bool pc_conn_remote_addr(uint8_t slot, pc_ip *out)
The connected peer's address as a family-tagged pc_ip (IPv4 or IPv6).
bool pc_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.
void pc_conn_abort(struct tcp_pcb *pcb)
Hard-abort pcb (RST) for a fatal condition; no graceful FIN.