ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
evt.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file evt.h
6 * @brief What the stack callbacks post to a listener's queue: the event type and the record.
7 *
8 * Separate from common.h because this is all the layers above the transport need. A listener sizes
9 * its queue storage on ::TcpEvt, the session layer switches on ::EvtType, and the presentation
10 * layer names EVT_CONNECT. None of them touches a connection slot's fields. common.h carries those
11 * fields and their atomic words, and the sketches do not need them.
12 *
13 * @author Douglas Quigg (dstroy0)
14 * @date 2026
15 */
16
17#ifndef PROTOCORE_TCP_EVT_H
18#define PROTOCORE_TCP_EVT_H
19
20#include "protocore_config.h" // the entry point: the widths, EMBED_ENUM_BYTE, static_assert
21
22/**
23 * @brief Lifecycle state of a connection pool slot.
24 *
25 * NOT the RFC 9293 sec 3.3.2 connection state machine. That machine (LISTEN, SYN-RECEIVED,
26 * ESTABLISHED, FIN-WAIT-1/2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) belongs to the stack under
27 * this layer; these three name only whether the pool slot is available. A test mapping CONN_* to
28 * RFC state names 1:1 is testing the wrong thing.
29 *
30 * Transitions, as the code performs them:
31 * - `CONN_FREE -> CONN_ACTIVE` accept callback fires.
32 * - `CONN_ACTIVE -> CONN_CLOSING` the dwell that precedes a close begins.
33 * - `CONN_CLOSING -> CONN_FREE` the peer ACKed the outbound data (snd_queuelen == 0), the dwell
34 * timed out, or data arrived that could no longer be delivered.
35 * - `CONN_ACTIVE -> CONN_FREE` local close, remote FIN, stack error, or the idle sweep.
36 *
37 * Every terminal edge detaches the control block and frees the slot before handing it to the stack,
38 * so the slot's lifetime ends before the connection's does; the FIN and its retransmission are the
39 * stack's from that point (RFC 9293 sec 3.6).
40 *
41 * Here rather than in common.h because the signaling layer reads a slot's state and reaches this
42 * layer through protocore.h. The enum is one packed byte.
43 */
45 CONN_FREE, ///< Slot is available; no control block is attached.
46 CONN_ACTIVE, ///< Live connection; the control block is valid.
47 CONN_CLOSING ///< Transmit-drain dwell: holding the slot until the peer ACKs what was sent. No
48 ///< FIN has been emitted yet - it goes out as the slot is released.
49);
50static_assert(sizeof(ConnState) == 1,
51 "ConnState must stay one byte (EMBED_ENUM_BYTE); TcpConn and conn_pool[] size themselves on it");
52
53/**
54 * @brief Type of connection event posted to a listener's event queue.
55 *
56 * EVT_DISCONNECT and EVT_ERROR are the two a close is reported through, and they are distinct
57 * because RFC 9293 sec 3.6 MUST-12 requires the layer above to be told whether a connection closed
58 * normally or was aborted.
59 */
61 EVT_CONNECT, ///< New connection accepted.
62 EVT_DATA, ///< Data received; bytes are already in the ring buffer.
63 EVT_DISCONNECT, ///< Remote peer closed the connection gracefully.
64 EVT_ERROR ///< The stack reported an error (the control block may already be freed).
65);
66static_assert(sizeof(EvtType) == 1,
67 "EvtType must stay one byte (EMBED_ENUM_BYTE); every listener's queue storage sizes itself on TcpEvt");
68
69/**
70 * @brief Event record posted from the stack callbacks to the session layer.
71 *
72 * Copied into the queue by value, so nothing in it points at a slot whose lifetime ends before the
73 * record is drained.
74 */
75typedef struct TcpEvt
76{
77 EvtType type; ///< What happened.
78 uint8_t slot_id; ///< Which connection slot is affected.
79 size_t data_len; ///< Bytes copied (EVT_DATA only); 0 for other types.
81
82// ---------------------------------------------------------------------------
83// Observability (PROTOCORE_ENABLE_OBSERVABILITY) - connection event hook + counters
84// ---------------------------------------------------------------------------
85#if PROTOCORE_ENABLE_OBSERVABILITY
86
87/** @brief Why a connection event fired (the reason for a transition or notice). */
88EMBED_ENUM_BYTE(protocore_conn_reason,
89 PROTOCORE_CONN_R_ACCEPT, ///< New connection accepted (CONN_FREE -> CONN_ACTIVE).
90 PROTOCORE_CONN_R_CLOSE_REMOTE, ///< Peer closed gracefully (FIN received).
91 PROTOCORE_CONN_R_CLOSE_LOCAL, ///< Application initiated the close.
92 PROTOCORE_CONN_R_ERROR, ///< The stack reported a fatal error on the connection.
93 PROTOCORE_CONN_R_TIMEOUT, ///< Idle-timeout sweep reaped the slot.
94 PROTOCORE_CONN_R_ABORT, ///< Forced abort (server stop / pool reset / data after close).
95 PROTOCORE_CONN_R_DRAINED, ///< CONN_CLOSING slot finished draining -> closed.
96 PROTOCORE_CONN_R_BACKPRESSURE, ///< RX segment refused (ring full); no state change.
97 PROTOCORE_CONN_R_DEFER_DROP ///< Event queue full; an event was dropped (no state change).
98);
99static_assert(sizeof(protocore_conn_reason) == 1, "protocore_conn_reason must stay one byte (EMBED_ENUM_BYTE)");
100
101/** @brief Snapshot of the transport's lifetime counters (plus a live gauge). */
102typedef struct protocore_conn_counters
103{
104 uint32_t accepts; ///< Connections accepted.
105 uint32_t closes_remote; ///< Closed by peer FIN.
106 uint32_t closes_local; ///< Closed by the application.
107 uint32_t closes_error; ///< Closed by a stack error.
108 uint32_t closes_timeout; ///< Reaped by the idle-timeout sweep.
109 uint32_t closes_abort; ///< Force-aborted (stop / reset / data after close).
110 uint32_t backpressure; ///< RX segments refused for lack of ring space.
111 uint32_t defer_drops; ///< Deferred events dropped because the queue was full.
112 uint32_t closing_gauge; ///< Slots currently in CONN_CLOSING (live, not cumulative).
113} protocore_conn_counters;
114
115/**
116 * @brief Callback fired on every connection state transition.
117 *
118 * Runs in whichever task drove the transition (the stack's callback context for
119 * accept / recv / error, a worker for close / timeout), so keep it short and non-blocking and do
120 * not call back into the server from it. @p old_state == @p new_state for the
121 * non-transition notices (backpressure, defer-drop).
122 */
123typedef void (*protocore_conn_event_cb)(uint8_t slot, ConnState old_state, ConnState new_state,
124 protocore_conn_reason reason);
125
126// Internal notify points (protocol/protocol.c), reached via the macros below so the protocol
127// engine and the server's accept path both record through one path.
128void protocore_obs_transition(uint8_t slot, ConnState olds, ConnState news, protocore_conn_reason reason);
129void protocore_obs_notice(uint8_t slot, ConnState st, protocore_conn_reason reason);
130#define PROTOCORE_OBS_TRANSITION(slot, olds, news, reason) protocore_obs_transition((slot), (olds), (news), (reason))
131#define PROTOCORE_OBS_NOTICE(slot, st, reason) protocore_obs_notice((slot), (st), (reason))
132
133#else // !PROTOCORE_ENABLE_OBSERVABILITY
134
135// Compile to nothing; the arguments (incl. protocore_conn_reason names, only declared
136// when the feature is on) are dropped unparsed by the preprocessor.
137#define PROTOCORE_OBS_TRANSITION(slot, olds, news, reason) ((void)0)
138#define PROTOCORE_OBS_NOTICE(slot, st, reason) ((void)0)
139
140#endif // PROTOCORE_ENABLE_OBSERVABILITY
141
142#endif // PROTOCORE_TCP_EVT_H
EMBED_ENUM_BYTE(ConnState, CONN_FREE, CONN_ACTIVE, CONN_CLOSING)
Lifecycle state of a connection pool slot.
Event record posted from the stack callbacks to the session layer.
Definition evt.h:76
EvtType type
What happened.
Definition evt.h:77
size_t data_len
Bytes copied (EVT_DATA only); 0 for other types.
Definition evt.h:79
uint8_t slot_id
Which connection slot is affected.
Definition evt.h:78