<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="es">
    <title>David Negro</title>
    <link rel="self" type="application/atom+xml" href="https://davidnegro.me/es/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://davidnegro.me"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-10-25T00:00:00+00:00</updated>
    <id>https://davidnegro.me/es/atom.xml</id>
    <entry xml:lang="es">
        <title>WebRTC from scratch: part 1</title>
        <published>2026-10-25T00:00:00+00:00</published>
        <updated>2026-10-25T00:00:00+00:00</updated>
        
        <author>
          <name>David Negro Martín</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://davidnegro.me/es/posts/webrtc-from-scratch-1/"/>
        <id>https://davidnegro.me/es/posts/webrtc-from-scratch-1/</id>
        
        <content type="html" xml:base="https://davidnegro.me/es/posts/webrtc-from-scratch-1/">&lt;p&gt;This is the second post of the &quot;WebRTC from scratch&quot; series.
&lt;a href=&quot;https://davidnegro.me/posts/webrtc-from-scratch-0/&quot;&gt;Part 0&lt;/a&gt; covered the motivation; this one
drafts the high-level design: the components, their interfaces and how they
fit together.&lt;/p&gt;
&lt;p&gt;The more AI is involved in writing the code, the more time I find it worth
spending on the design up front. A clear design keeps the generated code from
becoming an opaque black box, and makes it obvious when the AI is doing
something it shouldn&#39;t.&lt;/p&gt;
&lt;h1 id=&quot;organization-of-the-project&quot;&gt;Organization of the project&lt;/h1&gt;
&lt;p&gt;WebRTC is modular by nature, and it makes a lot more sense when you structure it
that way instead of hiding everything behind the usual PeerConnection API, so I
will keep that modularity. The implementation will have six components:
IceTransport, CryptoTransport, RtpTransport, DataChannelTransport,
AudioSender and AudioReceiver.&lt;/p&gt;
&lt;figure style=&quot;overflow-x: auto;&quot;&gt;
&lt;svg viewBox=&quot;0 0 740 626&quot; width=&quot;100%&quot; style=&quot;display: block; min-width: 560px;&quot; role=&quot;img&quot; aria-label=&quot;The six components stacked between the application and the I/O layer, with the events that connect them&quot;&gt;
&lt;style&gt;
.d-box { fill: var(--code-bg); stroke: var(--border); }
.d-name { fill: var(--text); font: 500 15px var(--font); text-anchor: middle; }
.d-sub { fill: var(--muted); font: 12px var(--font); text-anchor: middle; }
.d-arrow { stroke: var(--muted); stroke-width: 1.2; fill: none; marker-end: url(#d-head); }
.d-both { marker-start: url(#d-head); }
.d-call { stroke-dasharray: 4 3; }
.d-label { fill: var(--muted); font: 12px var(--mono); }
.d-left { text-anchor: end; }
.d-head { fill: var(--muted); }
&lt;/style&gt;
&lt;defs&gt;
&lt;marker id=&quot;d-head&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;9&quot; refY=&quot;5&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto-start-reverse&quot;&gt;&lt;path class=&quot;d-head&quot; d=&quot;M0,0 L10,5 L0,10 z&quot;/&gt;&lt;/marker&gt;
&lt;/defs&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;20&quot; y=&quot;10&quot; width=&quot;700&quot; height=&quot;40&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;370&quot; y=&quot;35&quot;&gt;Application&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;20&quot; y=&quot;120&quot; width=&quot;220&quot; height=&quot;40&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;130&quot; y=&quot;145&quot;&gt;AudioSender&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;260&quot; y=&quot;120&quot; width=&quot;220&quot; height=&quot;40&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;370&quot; y=&quot;145&quot;&gt;AudioReceiver&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;500&quot; y=&quot;120&quot; width=&quot;220&quot; height=&quot;40&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;610&quot; y=&quot;145&quot;&gt;DataChannelTransport&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;20&quot; y=&quot;230&quot; width=&quot;460&quot; height=&quot;44&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;250&quot; y=&quot;248&quot;&gt;RtpTransport&lt;/text&gt;
&lt;text class=&quot;d-sub&quot; x=&quot;250&quot; y=&quot;265&quot;&gt;packetization, RTCP&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;20&quot; y=&quot;344&quot; width=&quot;700&quot; height=&quot;44&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;370&quot; y=&quot;362&quot;&gt;CryptoTransport&lt;/text&gt;
&lt;text class=&quot;d-sub&quot; x=&quot;370&quot; y=&quot;379&quot;&gt;DTLS, SRTP&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;20&quot; y=&quot;458&quot; width=&quot;700&quot; height=&quot;44&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;370&quot; y=&quot;476&quot;&gt;IceTransport&lt;/text&gt;
&lt;text class=&quot;d-sub&quot; x=&quot;370&quot; y=&quot;493&quot;&gt;connectivity checks, keepalives&lt;/text&gt;
&lt;rect class=&quot;d-box&quot; x=&quot;20&quot; y=&quot;572&quot; width=&quot;700&quot; height=&quot;44&quot; rx=&quot;6&quot;/&gt;
&lt;text class=&quot;d-name&quot; x=&quot;370&quot; y=&quot;590&quot;&gt;I/O layer&lt;/text&gt;
&lt;text class=&quot;d-sub&quot; x=&quot;370&quot; y=&quot;607&quot;&gt;UDP socket, clock&lt;/text&gt;
&lt;line class=&quot;d-arrow d-call&quot; x1=&quot;130&quot; y1=&quot;50&quot; x2=&quot;130&quot; y2=&quot;120&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;136&quot; y=&quot;89&quot;&gt;10 ms of PCM&lt;/text&gt;
&lt;line class=&quot;d-arrow d-call&quot; x1=&quot;370&quot; y1=&quot;120&quot; x2=&quot;370&quot; y2=&quot;50&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;376&quot; y=&quot;89&quot;&gt;10 ms of PCM&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;610&quot; y1=&quot;50&quot; x2=&quot;610&quot; y2=&quot;120&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;616&quot; y=&quot;89&quot;&gt;messages&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;130&quot; y1=&quot;160&quot; x2=&quot;130&quot; y2=&quot;230&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;136&quot; y=&quot;199&quot;&gt;frames, control&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;370&quot; y1=&quot;160&quot; x2=&quot;370&quot; y2=&quot;230&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;376&quot; y=&quot;199&quot;&gt;frames, control&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;250&quot; y1=&quot;274&quot; x2=&quot;250&quot; y2=&quot;344&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;256&quot; y=&quot;313&quot;&gt;RTP, RTCP&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;610&quot; y1=&quot;160&quot; x2=&quot;610&quot; y2=&quot;344&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;616&quot; y=&quot;256&quot;&gt;SCTP&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;370&quot; y1=&quot;388&quot; x2=&quot;370&quot; y2=&quot;458&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;376&quot; y=&quot;427&quot;&gt;DTLS, SRTP&lt;/text&gt;
&lt;line class=&quot;d-arrow d-both&quot; x1=&quot;370&quot; y1=&quot;502&quot; x2=&quot;370&quot; y2=&quot;572&quot;/&gt;
&lt;text class=&quot;d-label&quot; x=&quot;376&quot; y=&quot;541&quot;&gt;UDP&lt;/text&gt;
&lt;/svg&gt;
&lt;/figure&gt;
&lt;p&gt;SDP is not in the picture: it is a special case. The two audio components
also deserve a discussion of their own, for the way audio enters and leaves
the pipeline; how they talk to the RTP transport is covered with that
component.&lt;/p&gt;
&lt;h1 id=&quot;handling-sdp-negotiation&quot;&gt;Handling SDP negotiation&lt;/h1&gt;
&lt;p&gt;SDP is the protocol the two peers use to negotiate the configuration of the
connection.&lt;/p&gt;
&lt;p&gt;Following the philosophy of part 0, I will implement only the parts I need. SDP
can describe arbitrarily complex sessions, with several audio and video streams
and a long list of settings, and it is designed for renegotiation: adding a
track or restarting ICE in the middle of a call means a new offer and answer,
with a state machine of its own, while the media keeps flowing. I need almost
none of that. The Realtime API allows a single negotiation in which the client
is the offerer; the offer carries one media line for audio and one for the
data channel, and I will support Opus as the only audio codec.&lt;/p&gt;
&lt;p&gt;That turns SDP into a simple configuration step that runs once, before the
components exist, and produces the configuration of the pipeline. No persistent
state, no state machine, and the rest of the library never sees an SDP string.
The API will look like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; What one of the two peers offers or accepts.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    bool&lt;/span&gt;&lt;span&gt; is_offer&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ice_config_t&lt;/span&gt;&lt;span&gt; ice&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;                  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ufrag, password, remote candidates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    dtls_config_t&lt;/span&gt;&lt;span&gt; dtls&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;                //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; fingerprint, role&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    rtp_config_t&lt;/span&gt;&lt;span&gt; rtp&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;                  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; SSRCs, payload type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    datachannel_config_t&lt;/span&gt;&lt;span&gt; datachannel&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; SCTP port&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Maybe more fields.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_peer_config_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The result of the negotiation. Both sides are kept, since every&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; component needs its own parameters and the remote ones.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    sdp_peer_config_t&lt;/span&gt;&lt;span&gt; local&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    sdp_peer_config_t&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_negotiated_config_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The error types are left unspecified for now.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; code&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_negotiation_error_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; code&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_parsing_error_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Fills a peer configuration with defaults: fresh ICE credentials, the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; fingerprint of the given certificate, random SSRCs. No local candidates:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; see IceTransport.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; sdp_init_default&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    arena_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; arena&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; is_offer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; certificate_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; certificate&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    sdp_peer_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Checks that the two configurations are compatible and fills result.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;sdp_negotiation_error_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; sdp_negotiate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    arena_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; arena&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_peer_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; local&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_peer_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_negotiated_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; sdp_to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_peer_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; config&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; string_builder_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; out&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;sdp_parsing_error_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; sdp_from_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    arena_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; arena&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; str&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; is_offer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_peer_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The usage is extremely simple. The HTTP exchange that carries the offer and
the answer belongs to the application, not to the library:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; do_sdp_negotiation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Error handling left out for clarity.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    arena_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; arena &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; /*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ... &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;*/&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    string_builder_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; request_body &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; /*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ... &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;*/&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    certificate_t&lt;/span&gt;&lt;span&gt; certificate&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The application creates or loads its certificate here.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    sdp_peer_config_t&lt;/span&gt;&lt;span&gt; local&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    sdp_peer_config_t&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    sdp_negotiated_config_t&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    sdp_init_default&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arena&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; true&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;certificate&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;local&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    sdp_to_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;local&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; request_body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; sdp_response &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; do_http_request&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;URL&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; request_body&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    sdp_from_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arena&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; sdp_response&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    sdp_negotiate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;arena&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;local&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Use result to create the components.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;webrtc-components&quot;&gt;WebRTC components&lt;/h1&gt;
&lt;p&gt;The six components run during the call, and they are built as described in
&lt;a href=&quot;https://davidnegro.me/posts/sans-io-in-c/&quot;&gt;Sans I/O in C&lt;/a&gt;. Each one declares the events it
consumes and emits, and exposes the three functions of that post next to
init and deinit. The ICE transport serves as the example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ---- Events ----&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Only the events the ICE transport consumes or emits are defined here.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Consumers. Each event kind is tagged with the components that handle it.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_IO&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;     0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0100&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; the I/O layer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_APP&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0200&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; the application&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_ICE&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0400&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; the ICE transport&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_CRYPTO&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0800&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; the cryptographic transport&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; SEND_UDP&lt;/span&gt;&lt;span&gt;          (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_IO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; RECEIVE_UDP&lt;/span&gt;&lt;span&gt;       (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_ICE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; SEND_ENCRYPTED&lt;/span&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_ICE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; RECEIVE_ENCRYPTED&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_CRYPTO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ICE_STATE&lt;/span&gt;&lt;span&gt;         (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_CRYPTO &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;|&lt;/span&gt;&lt;span&gt; TO_APP&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; several consumers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ---- Component ----&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Fields omitted. The struct is complete so that it can be embedded.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ice_transport_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ice_transport_init&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ice_transport_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; config&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ice_transport_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; transport&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ice_transport_deinit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;ice_transport_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; transport&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The three sans I/O functions.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Handles one event. May push new events into the buffer and change&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; the state of the transport.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ice_transport_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ice_transport_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; transport&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Handles the passage of time: timeouts, retransmissions, keepalives.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ice_transport_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ice_transport_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; transport&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint64_t&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The time at which process_time next has something to do, or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; UINT64_MAX. A hint: calling process_time earlier is fine.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ice_transport_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ice_transport_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; transport&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The config comes from the negotiated SDP plus the local settings: for the ICE
transport, the credentials of both sides, the remote candidates and the local
endpoints.&lt;/p&gt;
&lt;h2 id=&quot;icetransport&quot;&gt;IceTransport&lt;/h2&gt;
&lt;p&gt;The ICE transport receives UDP traffic from potentially several local
interfaces and establishes a route, a pair of local and remote endpoints,
through which everything else reaches the other side. ICE uses packets of its
own, STUN, and only asks that whatever runs on top of it can be told apart
from them.&lt;/p&gt;
&lt;p&gt;As decided in &lt;a href=&quot;https://davidnegro.me/posts/webrtc-from-scratch-0/&quot;&gt;part 0&lt;/a&gt;, this is a lite
client talking to a lite server: no STUN or TURN servers and no candidate
gathering. The server never tries to reach us, so the offer carries no local
candidates; the client sends a single connectivity check to the server&#39;s
candidate, which also tells the server where we are.&lt;/p&gt;
&lt;p&gt;For now, UDP only, and let&#39;s assume the network interfaces never change during
the call. The Realtime API negotiates with a single HTTP request/response, so
every remote candidate is known after the SDP step: the ICE transport can be
created with everything it needs and only has to attempt the connection.&lt;/p&gt;
&lt;h2 id=&quot;cryptotransport&quot;&gt;CryptoTransport&lt;/h2&gt;
&lt;p&gt;The protocols WebRTC runs on top of ICE are DTLS and SRTP/SRTCP, and they are
related: the SRTP/SRTCP keys are derived from the DTLS connection. As a
reminder:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DTLS is TLS for datagrams.&lt;/li&gt;
&lt;li&gt;SRTP/SRTCP are the secure versions of RTP/RTCP.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The two protocols have different packet formats. Reading and comparing a few
byte positions tells whether a packet is DTLS, SRTP or SRTCP, so they can
share one packet stream. This component runs the DTLS handshake, then
demultiplexes and decrypts the received traffic, and encrypts and sends the
RTP, RTCP and SCTP traffic. DTLS itself carries only SCTP; RTP and RTCP use
SRTP and SRTCP with the keys from the handshake.&lt;/p&gt;
&lt;h2 id=&quot;datachanneltransport&quot;&gt;DataChannelTransport&lt;/h2&gt;
&lt;p&gt;The data channel transport speaks SCTP plus a small protocol on top of it,
DCEP, that opens and labels the channels. This implementation needs a single
reliable data channel opened by the client, and that is all it will do.&lt;/p&gt;
&lt;h2 id=&quot;rtptransport&quot;&gt;RtpTransport&lt;/h2&gt;
&lt;p&gt;The idea of the RTP transport is to handle the low-level details of RTP and
RTCP and offer the audio sender and receiver a protocol-agnostic, &quot;logical&quot;
interface instead: media events that carry frames, and control events that
carry stats, recovery actions and anything else that affects how the media is
transferred. The sender hands over a frame with its sample position and side
information such as the audio level, and the RTP transport takes care of
SSRCs, payload types and header extensions; the receiver may ask for missing
frames, and the RTP transport turns that into an RTCP NACK.&lt;/p&gt;
&lt;p&gt;The hardest parts of a sender or receiver are network adaptation, efficient
error recovery, and the control of the encoder and decoder. The point of the
logical interface is to define exactly what information each component needs
to receive and send to implement those features well.&lt;/p&gt;
&lt;p&gt;With one sender and one receiver there is no need to worry about the general
WebRTC case of several media streams: routing RTP and RTCP packets and setting
up the pipeline are straightforward.&lt;/p&gt;
&lt;h2 id=&quot;audiosender-and-audioreceiver&quot;&gt;AudioSender and AudioReceiver&lt;/h2&gt;
&lt;p&gt;The sender encodes the captured audio with Opus and, as decided in part 0,
sends plenty of redundancy by default instead of adapting to the network. The
receiver holds the jitter buffer and decodes, building the concealment and
the time stretching on the tools Opus already provides.&lt;/p&gt;
&lt;p&gt;Both have a special requirement: interacting efficiently with the audio
capture and rendering layers of the operating system. For them I make an
exception to the everything-is-an-event rule of the sans I/O post, the second
one after time, and add the following functions:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Encodes the frame and emits the resulting media events into buffer.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; audio_sender_put_10ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    audio_sender_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; sender&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; audio_frame_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; frame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Always produces a frame, whatever has or has not arrived from the network.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; audio_receiver_get_10ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;audio_receiver_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; receiver&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; audio_frame_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; frame&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives the receiver a NetEq-like interface. NetEq is the core of the
audio receiver in WebRTC, and one of the pieces of WebRTC I love the most in
terms of software design; mine will follow its shape with the Opus tools
from part 0. At a high level, it provides the following interface:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; NetEq_insert_packet&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;NetEq&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ne&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; AudioPacket&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; packet&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; NetEq_get_10ms_of_audio&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;NetEq&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ne&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; AudioFrame&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; audio_out&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To use it in a pipeline you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Call &lt;code&gt;NetEq_insert_packet&lt;/code&gt; each time an audio packet arrives from the network,
which is the receiver&#39;s &lt;code&gt;*_process_event&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;NetEq_get_10ms_of_audio&lt;/code&gt; each time the audio device needs audio to play.
&lt;ul&gt;
&lt;li&gt;This is a lot like rendering in video games: you wait for the vsync signal
and then render the next frame. The idea is to get a similar signal from the
audio device: when it starts playing a segment, generate the next one.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NetEq_get_10ms_of_audio&lt;/code&gt; always succeeds. If there are no packets to play,
NetEq generates concealment audio, and it time-stretches the signal to
compensate for network jitter and even for the drift between the clocks of
the two systems involved in the call.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One of the great ideas of NetEq is that the &lt;code&gt;get_10ms_of_audio&lt;/code&gt; calls &lt;em&gt;are&lt;/em&gt; its
clock, and that clock is driven by the playback device. The device asks for
audio and must get it now, synchronously, and that same call advances the
timing of the component. A function call is the most straightforward way to
implement this behaviour.&lt;/p&gt;
&lt;p&gt;The sender takes the same shape for symmetry, and because the alternative,
copying 10 ms of PCM into an event so that the sender can read it back a
moment later, buys nothing.&lt;/p&gt;
&lt;h1 id=&quot;the-peer-connection&quot;&gt;The peer connection&lt;/h1&gt;
&lt;p&gt;I don&#39;t like the peer connection abstraction in general, but I have to
recognise that it is practical. A peer-connection-like API falls out of
wiring the components into a pipeline, as the
&lt;a href=&quot;https://davidnegro.me/posts/sans-io-in-c/&quot;&gt;sans I/O post&lt;/a&gt; suggests:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ice_transport_t&lt;/span&gt;&lt;span&gt; ice_transport&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    crypto_transport_t&lt;/span&gt;&lt;span&gt; crypto_transport&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    rtp_transport_t&lt;/span&gt;&lt;span&gt; rtp_transport&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    datachannel_transport_t&lt;/span&gt;&lt;span&gt; datachannel_transport&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    audio_sender_t&lt;/span&gt;&lt;span&gt; audio_sender&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    audio_receiver_t&lt;/span&gt;&lt;span&gt; audio_receiver&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_buffer_t&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; peer_connection_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; SDP stays its own thing even here: the peer connection takes the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; negotiated result, plus the local settings.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_init&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; sdp_negotiated_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; sdp&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; peer_connection_config_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; config&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; pc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_deinit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The pipeline functions of the sans I/O post.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint64_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; now&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; put_10ms also propagates the events it produces.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_put_10ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; audio_frame_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; frame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_get_10ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; audio_frame_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; frame&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;///&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Pushes a data channel message as an event and propagates it.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; peer_connection_send_message&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    peer_connection_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; pc&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint32_t&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The sans I/O post delivers the packets to send and the events for the
application through two callbacks provided by the I/O layer. Whether that or
its external-buffer variant is the right shape here is still open, so do not
take any of this code as final.&lt;/p&gt;
&lt;h1 id=&quot;a-brief-note-about-allocations&quot;&gt;A brief note about allocations&lt;/h1&gt;
&lt;p&gt;A good C program, as opposed to a C++ or Rust one, makes allocations explicit
and part of the design. For SDP it is already clear to me that an &lt;code&gt;arena_t&lt;/code&gt;
is the right tool, so it appears in those signatures. The rest of the code in
this post leaves allocators out: I do not know yet how I will structure them.&lt;/p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;I have spent a substantial amount of time iterating on this design and I think
it is time to stop and start implementing it. Six components and an SDP step
that runs once: that is the whole design. Depending on how the code turns
out, some component boundaries may still move.&lt;/p&gt;
&lt;p&gt;In the next post we will start implementing the SDP logic.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="es">
        <title>Sans I/O in C</title>
        <published>2026-10-18T00:00:00+00:00</published>
        <updated>2026-10-18T00:00:00+00:00</updated>
        
        <author>
          <name>David Negro Martín</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://davidnegro.me/es/posts/sans-io-in-c/"/>
        <id>https://davidnegro.me/es/posts/sans-io-in-c/</id>
        
        <content type="html" xml:base="https://davidnegro.me/es/posts/sans-io-in-c/">&lt;p&gt;I was overthinking the next post of the WebRTC from scratch series, the one
that sets the design rules of the project. One of the things I wanted to try was
an approach called &quot;sans I/O&quot;, and while working out how to apply it in C I
ended up with a very simple way to implement it. I liked the result enough to
write this post about it.&lt;/p&gt;
&lt;h1 id=&quot;what-sans-i-o-is&quot;&gt;What sans I/O is&lt;/h1&gt;
&lt;p&gt;Sans I/O denotes an implementation of a network protocol that performs no I/O
of its own. The protocol is written as a deterministic state machine: it is
informed of what happened (a packet arrived, time passed), updates its state
and describes what it wants done (send this packet). An I/O layer produces
those inputs and translates the outputs into system calls.&lt;/p&gt;
&lt;p&gt;The term originates in the Python community;
&lt;a rel=&quot;external&quot; href=&quot;https://sans-io.readthedocs.io/&quot;&gt;sans-io.readthedocs.io&lt;/a&gt; documents the
approach and lists libraries written this way.&lt;/p&gt;
&lt;p&gt;The following properties follow from this separation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Testability.&lt;/strong&gt; No real I/O is involved. A component is a deterministic
machine with defined input and output event streams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Composability.&lt;/strong&gt; Two protocols are combined by mapping the output events
of one to the input events of the other.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Portability.&lt;/strong&gt; The protocol never touches sockets, timers or threads,
which is where platform-specific code tends to live.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;implementing-it-in-c&quot;&gt;Implementing it in C&lt;/h1&gt;
&lt;p&gt;There is no standard way of implementing sans I/O. What follows is the
structure I arrived at after applying it to a protocol like WebRTC,
packet-oriented and composed of several protocols (one of which, ICE, needs
close control of the network layer), with C and a simple implementation in
mind.&lt;/p&gt;
&lt;p&gt;An implementation consists of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Events&lt;/strong&gt;, serialized bytes tagged with their kind.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Components&lt;/strong&gt;, structs holding the state of one protocol.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Three functions per component&lt;/strong&gt;: how its state changes and which events
it emits when it receives an event, the same when time passes, and when it
next needs to be told the time.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Everything that enters or leaves a component is an event, with one exception:
time. Reading the clock is itself a form of I/O, and a component that calls
&lt;code&gt;time()&lt;/code&gt; is no longer deterministic.&lt;/p&gt;
&lt;h2 id=&quot;the-event-buffer&quot;&gt;The event buffer&lt;/h2&gt;
&lt;p&gt;Events are serialized one after another into a flat buffer, each as a small
header (kind and size) followed by its payload. A component receives the buffer
in every call in which it may emit events.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; read_offset&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; write_offset&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; capacity&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; An event as a component sees it: its kind and its payload.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; kind&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; SERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt;ptr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;                 \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    do&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;                                      \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        memcpy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;               \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt;ptr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;         \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    do&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;                                      \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        assert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;      \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        memcpy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; ptr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;               \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; -=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;field&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #F47067);&quot;&gt;              \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Appends an event and returns a pointer to its payload. The caller must fill&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; the payload before doing anything else with the buffer.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; event_buffer_push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint16_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint16_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    assert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;write_offset&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        &amp;lt;=&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;capacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; dst &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;write_offset&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    SERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; kind&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    SERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;write_offset&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; dst &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; dst&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Takes the next event out of the buffer. Returns 0 if there is none.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; event_buffer_pop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; src &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;read_offset&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; unread &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;write_offset&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; -&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;read_offset&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;unread &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;src&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; unread&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;src&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; unread&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    event&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; src&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;read_offset&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; src &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; event_buffer_clear&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;read_offset&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;write_offset&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A ring buffer is the usual choice for a queue, but it is not needed here. The
buffer is drained completely after every external stimulus (a datagram, a tick,
a call from the application) and then cleared, so it never holds many events.
The one constraint is that it must not grow while events are being propagated,
since the pointer returned by &lt;code&gt;event_buffer_pop&lt;/code&gt; remains in use while the
components handle the event.&lt;/p&gt;
&lt;h2 id=&quot;defining-events&quot;&gt;Defining events&lt;/h2&gt;
&lt;p&gt;An event is defined by a kind (an integer) and a payload layout. The two UDP
events may be defined as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint8_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ip&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;16&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; IPv6, or IPv4-mapped&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; port&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; endpoint_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Provisional; the final kinds are defined in Events and composition.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; SEND_UDP&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; RECEIVE_UDP&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Payload of SEND_UDP and RECEIVE_UDP: the socket, the destination or source&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; endpoint, then the datagram. Writes it at dst and returns the event.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; build_udp_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint16_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; endpoint_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint16_t&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_t&lt;/span&gt;&lt;span&gt; event &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; dst&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    SERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    SERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    memcpy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; dst &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; push_udp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint16_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; endpoint_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint16_t&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt; dst &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; event_buffer_push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;socket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    build_udp_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;defining-components&quot;&gt;Defining components&lt;/h2&gt;
&lt;p&gt;A component consists of a struct and three functions: &lt;code&gt;*_process_event&lt;/code&gt;,
&lt;code&gt;*_process_time&lt;/code&gt; and &lt;code&gt;*_next_deadline&lt;/code&gt;. The I/O layer calls the second before
delivering any event, so a component may
rely on its copy of &lt;code&gt;now&lt;/code&gt; while handling events. The third returns the earliest
time at which the component needs &lt;code&gt;*_process_time&lt;/code&gt; to be called again, or
&lt;code&gt;UINT64_MAX&lt;/code&gt; if it has nothing pending. It is a hint for the caller, not a
contract: &lt;code&gt;*_process_time&lt;/code&gt; may be called at any time, and a loop that ignores
the deadlines and ticks at a fixed rate remains correct. A component reads only
its own state and writes only to the buffer, which is what allows it to be
tested in isolation.&lt;/p&gt;
&lt;p&gt;The running example is a UDP ping. The server echoes back whatever it
receives:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; The server has no state.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span&gt; unused&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ping_server_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ping_server_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_server_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; server&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; !=&lt;/span&gt;&lt;span&gt; RECEIVE_UDP&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    endpoint_t&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    push_udp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; SEND_UDP&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The client has state and tracks time, all of it derived from its inputs:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; PING_SEND&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;   3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; app -&amp;gt; client; payload: the endpoint_t to ping&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; PING_RESULT&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; client -&amp;gt; app; payload: a ping_result_t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    bool&lt;/span&gt;&lt;span&gt; ok&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; false: timed out or cancelled by a new PING_SEND&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint64_t&lt;/span&gt;&lt;span&gt; latency_ms&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ping_result_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; PING_TIMEOUT_MS&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint64_t&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; sequence&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; payload of the last ping sent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    bool&lt;/span&gt;&lt;span&gt; waiting&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;       //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; a ping is in flight; the fields below are valid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    endpoint_t&lt;/span&gt;&lt;span&gt; server&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint64_t&lt;/span&gt;&lt;span&gt; sent_at&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ping_client_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ping_client_finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_client_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; bool&lt;/span&gt;&lt;span&gt; ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_result_t&lt;/span&gt;&lt;span&gt; result &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;ok&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;now&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; -&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sent_at&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    memcpy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        event_buffer_push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; PING_RESULT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;waiting&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ping_client_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_client_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    switch&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    case&lt;/span&gt;&lt;span&gt; PING_SEND&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;waiting&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            ping_client_finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event.data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event.size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt;server&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sequence&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;++&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sent_at&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;now&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;waiting&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        push_udp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; SEND_UDP&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt;socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt;server&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            &amp;amp;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt;sequence&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;sequence&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    case&lt;/span&gt;&lt;span&gt; RECEIVE_UDP&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        int&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;        endpoint_t&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;waiting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; memcmp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;remote&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;server&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sequence&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; memcmp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sequence&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            ping_client_finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ping_client_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_client_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint64_t&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;now&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; =&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;waiting&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; now &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sent_at&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span&gt; PING_TIMEOUT_MS&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        ping_client_finish&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ping_client_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; ping_client_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; client&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;!&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;waiting&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        return&lt;/span&gt;&lt;span&gt; UINT64_MAX&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;sent_at&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span&gt; PING_TIMEOUT_MS&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;events-and-composition&quot;&gt;Events and composition&lt;/h2&gt;
&lt;p&gt;Composing components means delivering the events emitted by one of them to
another. Some sans I/O implementations give each component separate input and
output streams for this purpose. This post advocates a different arrangement:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;every event is broadcast to every component;&lt;/li&gt;
&lt;li&gt;a component ignores the events that do not concern it;&lt;/li&gt;
&lt;li&gt;the kind of an event determines which components consume it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cost is composability at run time, since events become tied to one pipeline, a fixed arrangement of components.
Consider the ping client, which emits &lt;code&gt;SEND_UDP&lt;/code&gt; and consumes &lt;code&gt;RECEIVE_UDP&lt;/code&gt;,
wired to an I/O layer that consumes &lt;code&gt;SEND_UDP&lt;/code&gt; and emits &lt;code&gt;RECEIVE_UDP&lt;/code&gt;.
Inserting a component that encrypts and decrypts the datagrams requires new
kinds, and at least one component has to change the events it emits or handles:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;event&lt;/th&gt;&lt;th&gt;emitted by&lt;/th&gt;&lt;th&gt;handled by&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;SEND_UDP&lt;/code&gt;&lt;/td&gt;&lt;td&gt;ping client&lt;/td&gt;&lt;td&gt;crypto&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;SEND_UDP_ENCRYPTED&lt;/code&gt;&lt;/td&gt;&lt;td&gt;crypto&lt;/td&gt;&lt;td&gt;I/O layer&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;RECEIVE_UDP_ENCRYPTED&lt;/code&gt;&lt;/td&gt;&lt;td&gt;I/O layer&lt;/td&gt;&lt;td&gt;crypto&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;RECEIVE_UDP&lt;/code&gt;&lt;/td&gt;&lt;td&gt;crypto&lt;/td&gt;&lt;td&gt;ping client&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;In exchange, as long as components need not be composed dynamically (in the
manner of GStreamer, where pipelines are assembled at run time), the glue code
becomes trivial. A component that talks to several others needs nothing extra,
and a component can be made to receive whichever events are of interest to it,
for instance one that tracks metrics.&lt;/p&gt;
&lt;p&gt;Since the kind determines the handlers, they can be encoded in the kind itself,
which avoids the cost of broadcasting. For the ping client, crypto and network
pipeline, the events may be defined as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; High byte of a kind: one bit per handler of the event. Low byte: which event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; it is.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_IO&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;     0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_APP&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_PING&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;   0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0400&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; TO_CRYPTO&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0800&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; SEND_UDP&lt;/span&gt;&lt;span&gt;              (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_CRYPTO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; plaintext out: encrypt it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; SEND_UDP_ENCRYPTED&lt;/span&gt;&lt;span&gt;    (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_IO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;      //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ciphertext out: send it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; RECEIVE_UDP_ENCRYPTED&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_CRYPTO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ciphertext in: decrypt it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; RECEIVE_UDP&lt;/span&gt;&lt;span&gt;           (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_PING&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; plaintext in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; PING_SEND&lt;/span&gt;&lt;span&gt;             (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_PING&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; PING_RESULT&lt;/span&gt;&lt;span&gt;           (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; |&lt;/span&gt;&lt;span&gt; TO_APP&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;the-pipeline&quot;&gt;The pipeline&lt;/h2&gt;
&lt;p&gt;A pipeline wires several components into a single component-like shape: a
struct holding the components and the buffer, and the same three functions.
&lt;code&gt;*_process_event&lt;/code&gt; pushes the event and propagates, &lt;code&gt;*_process_time&lt;/code&gt; passes the
time to every component and propagates, and &lt;code&gt;*_next_deadline&lt;/code&gt; is the minimum of
the components&#39; deadlines. Propagation pops events until the buffer is drained
and hands each one to the components its kind names. Events addressed to the
I/O layer or to the application leave the pipeline through two functions that
the I/O layer provides:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_client_t&lt;/span&gt;&lt;span&gt; client&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Turns SEND_UDP into SEND_UDP_ENCRYPTED and back (not shown).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    crypto_t&lt;/span&gt;&lt;span&gt; crypto&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_buffer_t&lt;/span&gt;&lt;span&gt; buffer&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; crypto_ping_t&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Provided by the I/O layer: SEND_UDP_ENCRYPTED is sent, PING_RESULT is&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; reported.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; io_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;crypto_ping_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; app_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;crypto_ping_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; crypto_ping_propagate_events&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;crypto_ping_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ping&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_t&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;event_buffer_pop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt; TO_PING&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            ping_client_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt; TO_CRYPTO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            crypto_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;crypto&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt; TO_IO&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            io_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt; TO_APP&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            app_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    event_buffer_clear&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; crypto_ping_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;crypto_ping_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    memcpy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        event_buffer_push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    crypto_ping_propagate_events&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; crypto_ping_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;crypto_ping_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; uint64_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; now&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    ping_client_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    crypto_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;crypto&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    crypto_ping_propagate_events&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; crypto_ping_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; crypto_ping_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; ping&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint64_t&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; ping_client_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    uint64_t&lt;/span&gt;&lt;span&gt; b &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; crypto_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;crypto&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; b &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;?&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;:&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In a well-formed protocol the loop terminates: the components eventually have
nothing further to emit and wait for the network, the application or time.&lt;/p&gt;
&lt;p&gt;The two functions provided by the I/O layer run while the buffer is being
drained, so they must not call back into the pipeline. A request that arises
from an event, such as sending the next ping after a result, is recorded and
submitted once the call that produced it returns.&lt;/p&gt;
&lt;p&gt;The interface of the pipeline differs from a component&#39;s in one respect: its
functions take no buffer, since the events that leave the pipeline are
delivered by calls to &lt;code&gt;io_process_event&lt;/code&gt; and &lt;code&gt;app_process_event&lt;/code&gt;. The exact
interface can be had by giving &lt;code&gt;*_process_event&lt;/code&gt; and &lt;code&gt;*_process_time&lt;/code&gt; an
external &lt;code&gt;event_buffer_t*&lt;/code&gt; and copying into it the events addressed to the I/O
layer or the application, in place of the two calls. This makes the pipeline
usable as a component of a larger pipeline, at the price of a second buffer and
a copy per outgoing event. When no such nesting is needed, the direct calls are
simpler, and this post uses them.&lt;/p&gt;
&lt;h2 id=&quot;the-i-o-layer&quot;&gt;The I/O layer&lt;/h2&gt;
&lt;p&gt;The I/O layer is the code that drives a component. It feeds the component with
events and timestamps and acts on the I/O and application events the component
emits. It implements:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The handling of I/O and application events: each is mapped to a socket call
or to application logic. In the pipeline of the previous section this is
&lt;code&gt;io_process_event&lt;/code&gt; and &lt;code&gt;app_process_event&lt;/code&gt;, which the I/O layer must provide.&lt;/li&gt;
&lt;li&gt;A loop over external occurrences (datagrams, the passage of time, requests
from the application) that submits each one to the component and waits
between them no longer than the next deadline.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The program below uses a blocking socket and waits until the next deadline. A
non-blocking socket, an &lt;code&gt;epoll&lt;/code&gt; loop (whose timeout argument is exactly this
deadline), a thread per socket or the replay of a recorded session would make
the same calls, and no protocol code would change. Adapting the protocol to an
I/O model amounts to writing this loop.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; memory&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;4096&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    crypto_ping_t&lt;/span&gt;&lt;span&gt; ping &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;.buffer &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;memory&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; memory&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    endpoint_t&lt;/span&gt;&lt;span&gt; server &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; ... &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;*/&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; from&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; datagram&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;1500&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Open the socket, initialize client and crypto...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    crypto_ping_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; now_ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    crypto_ping_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        &amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;event_t&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span&gt;PING_SEND&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;server&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;server&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; Wait for a datagram until the next deadline, or forever if there is&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt;        //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #768390);&quot;&gt; none.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        uint64_t&lt;/span&gt;&lt;span&gt; now &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; now_ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        uint64_t&lt;/span&gt;&lt;span&gt; deadline &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; crypto_ping_next_deadline&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        int&lt;/span&gt;&lt;span&gt; wait_ms &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;deadline &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;==&lt;/span&gt;&lt;span&gt; UINT64_MAX&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            wait_ms &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;deadline &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            wait_ms &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;deadline &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;-&lt;/span&gt;&lt;span&gt; now&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        int&lt;/span&gt;&lt;span&gt; received &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; poll_udp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ping&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; wait_ms&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;from&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; datagram&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;datagram&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        crypto_ping_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; now_ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;received &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; payload&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;from&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;datagram&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            crypto_ping_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;                &amp;amp;&lt;/span&gt;&lt;span&gt;ping&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;                build_udp_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    payload&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; RECEIVE_UDP_ENCRYPTED&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; ping&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; from&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    datagram&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; received&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;testing&quot;&gt;Testing&lt;/h1&gt;
&lt;p&gt;A unit test is a sequence of calls to &lt;code&gt;*_process_event&lt;/code&gt; and &lt;code&gt;*_process_time&lt;/code&gt;
followed by assertions on the events emitted and on the state of the component.
No mocks are involved.&lt;/p&gt;
&lt;p&gt;A unit test turns into a fuzzing test by deriving that sequence of calls from
the fuzzer&#39;s input instead.&lt;/p&gt;
&lt;p&gt;A kind of test that I have found useful with network protocols in the past is
to simulate the network and let a client and a server component talk to each
other. It requires no more than a separate &lt;code&gt;event_buffer_t&lt;/code&gt; per endpoint and a
mapping of the I/O events of one onto the buffer of the other. The test is then
the I/O layer of the previous section reduced to a loop that drains the
buffers. The following one simulates a ping round trip:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#24292E, #ADBAC7); background-color: light-dark(#FFFFFF, #22272E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; deliver&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;event_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; event&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; event_buffer_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; to&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; endpoint_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; from&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    endpoint_t&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; remote&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    push_udp&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;to&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; RECEIVE_UDP&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; socket&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; from&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt; test_ping_round_trip&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; client_memory&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #F69D50);&quot;&gt; server_memory&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;1024&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_buffer_t&lt;/span&gt;&lt;span&gt; client_buffer &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;        0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;client_memory&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client_memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_buffer_t&lt;/span&gt;&lt;span&gt; server_buffer &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;        0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;server_memory&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; server_memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_client_t&lt;/span&gt;&lt;span&gt; client &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;.socket &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_server_t&lt;/span&gt;&lt;span&gt; server&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    endpoint_t&lt;/span&gt;&lt;span&gt; client_endpoint &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;.port &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    endpoint_t&lt;/span&gt;&lt;span&gt; server_endpoint &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;.port &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    ping_result_t&lt;/span&gt;&lt;span&gt; result &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt;    event_t&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    ping_client_process_time&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;client_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 1000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    memcpy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;        event_buffer_push&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;client_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; PING_SEND&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;server_endpoint&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        &amp;amp;&lt;/span&gt;&lt;span&gt;server_endpoint&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;server_endpoint&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;event_buffer_pop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;client_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            ping_client_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;client&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;client_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span&gt; SEND_UDP&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;                deliver&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;server_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; client_endpoint&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span&gt; PING_RESULT&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;                DESERIALIZE&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;event_buffer_pop&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;server_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;            ping_server_process_event&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;server&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;server_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;kind&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span&gt; SEND_UDP&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;                deliver&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;event&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;client_buffer&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; server_endpoint&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt;            break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #DCBDFB);&quot;&gt;    assert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;ok&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;latency_ms&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F47067);&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #6CB6FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h1 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h1&gt;
&lt;p&gt;The implementation needed to apply sans I/O is tiny: a flat buffer with a push
and a pop, a handful of event kinds and three functions per component. There is
no framework to learn, nothing allocated after start-up, and the I/O layer is
whatever loop the program already has. In return, tests need no mocks and the
protocol code runs unchanged wherever the loop does.&lt;/p&gt;
&lt;p&gt;I haven&#39;t used this on anything bigger than a ping yet. The plan is to put it
to work soon, with ICE, DTLS, SCTP and RTP as the components of the
&lt;a href=&quot;https://davidnegro.me/posts/webrtc-from-scratch-0/&quot;&gt;WebRTC from scratch&lt;/a&gt; series.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="es">
        <title>WebRTC from scratch: part 0</title>
        <published>2026-10-10T00:00:00+00:00</published>
        <updated>2026-10-10T00:00:00+00:00</updated>
        
        <author>
          <name>David Negro Martín</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://davidnegro.me/es/posts/webrtc-from-scratch-0/"/>
        <id>https://davidnegro.me/es/posts/webrtc-from-scratch-0/</id>
        
        <content type="html" xml:base="https://davidnegro.me/es/posts/webrtc-from-scratch-0/">&lt;p&gt;Amazon finally gave Alexa an LLM, but it is not as smart as I expected, and it feels clunky next to ChatGPT&#39;s voice mode. OpenAI has published the API behind that voice mode, and I have a pile of Raspberry Pis gathering dust at home. So: let&#39;s build an Alexa on top of ChatGPT.&lt;/p&gt;
&lt;h1 id=&quot;the-realtime-api&quot;&gt;The Realtime API&lt;/h1&gt;
&lt;p&gt;OpenAI&#39;s &lt;a rel=&quot;external&quot; href=&quot;https://developers.openai.com/api/docs/guides/realtime&quot;&gt;Realtime API&lt;/a&gt; offers two transports: WebSockets and WebRTC. WebSockets is the easy route, but WebRTC gives the lowest latency and avoids the &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Head-of-line_blocking&quot;&gt;head-of-line blocking&lt;/a&gt; that any TCP-based transport suffers from.&lt;/p&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;https://developers.openai.com/api/docs/guides/voice-webrtc?api=realtime&quot;&gt;WebRTC flavour&lt;/a&gt; is simple. The client sets up a PeerConnection with one &lt;code&gt;sendrecv&lt;/code&gt; audio transceiver, which carries your microphone one way and the agent&#39;s voice the other, plus one DataChannel for control messages. The SDP negotiation is a single HTTP request/response where the client is the offerer, and the same request carries extra configuration such as the initial prompt.&lt;/p&gt;
&lt;h1 id=&quot;why-implement-webrtc&quot;&gt;Why implement WebRTC?&lt;/h1&gt;
&lt;p&gt;Why not?&lt;/p&gt;
&lt;p&gt;The trick is to not implement all of WebRTC. The standard is huge because it covers every case: any peer, any network, any number of tracks, any codec. Drop everything this project doesn&#39;t need and what remains is a small slice that fits in a side project.&lt;/p&gt;
&lt;p&gt;I&#39;ve seen this work before. The first time I dealt with WebRTC I was working at a company with a mature low-latency streaming server, and we wanted to add web clients. The project&#39;s development experience wasn&#39;t great: lots of manual build steps, and adding a dependency was a nightmare. When we saw what building the official WebRTC stack involved, we immediately looked for alternatives.&lt;/p&gt;
&lt;p&gt;That&#39;s how I found a project (which, unfortunately, I&#39;ve never been able to find again) that advertised itself as a &quot;UDP socket for the web&quot;. It was tiny, because it didn&#39;t try to implement the whole standard, and so easy to compile and read that I used it as a starting point. After learning how WebRTC actually works, I built a simple RTP/SRTP transport on top of it. Our server already produced H.264 and Opus, both supported by WebRTC, so a packetizer and some RTCP handling were enough for a working prototype.&lt;/p&gt;
&lt;p&gt;That is the plan here too: implement WebRTC, but only the parts needed to talk to the Realtime API.&lt;/p&gt;
&lt;h1 id=&quot;how&quot;&gt;How&lt;/h1&gt;
&lt;h2 id=&quot;in-plain-c&quot;&gt;In plain C&lt;/h2&gt;
&lt;p&gt;I&#39;ve spent my whole career in C++, and I loved it, even its weirdest corners, but I&#39;ve come to value the simplicity and portability of C. It is also the &lt;em&gt;lingua franca&lt;/em&gt; of programming languages: a C API can later be exposed to virtually any other language.&lt;/p&gt;
&lt;h2 id=&quot;with-a-few-third-party-dependencies&quot;&gt;With a few third-party dependencies&lt;/h2&gt;
&lt;p&gt;A word on what &quot;from scratch&quot; means here. I&#39;ll use a few small dependencies, each one vendored and hidden behind an explicit interface so they are easy to swap:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://openssl-library.org/&quot;&gt;OpenSSL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/cisco/libsrtp&quot;&gt;libsrtp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/sctplab/usrsctp&quot;&gt;usrsctp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;usrsctp&lt;/code&gt; looks barely maintained and most WebRTC projects are migrating to other SCTP implementations, so if I feel motivated I may end up writing my own.&lt;/p&gt;
&lt;h2 id=&quot;with-ai-for-the-boring-parts&quot;&gt;With AI for the boring parts&lt;/h2&gt;
&lt;p&gt;Yes, I plan to use AI, especially for the unglamorous parts: this is a side project, and I want it to have unit tests, fuzzers and the rest of a &quot;modern project&quot; without typing all of that by hand. Still, every line will be reviewed by me, the design will be done by a human (me), and I&#39;ll explain every design decision along the way.&lt;/p&gt;
&lt;h1 id=&quot;stripping-webrtc-down&quot;&gt;Stripping WebRTC down&lt;/h1&gt;
&lt;h2 id=&quot;peer-to-peer-ice&quot;&gt;Peer-to-peer (ICE)&lt;/h2&gt;
&lt;p&gt;ICE has a reduced variant, &lt;a rel=&quot;external&quot; href=&quot;https://www.rfc-editor.org/rfc/rfc8445#section-2.5&quot;&gt;ICE Lite&lt;/a&gt;, meant for servers with a public IP that don&#39;t need NAT traversal. It is the minimum a server must implement to connect with a client, and it is probably what OpenAI runs. Following the same philosophy, we can design a &quot;lite client&quot; that only connects to lite servers (or to any server with a public IP).&lt;/p&gt;
&lt;p&gt;A lite client behaves like a lite server, responding to connectivity checks, and additionally sends a connectivity check with the &lt;code&gt;USE-CANDIDATE&lt;/code&gt; attribute to the server&#39;s endpoint.&lt;/p&gt;
&lt;h2 id=&quot;audio&quot;&gt;Audio&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Make the pipeline Opus-centric.&lt;/strong&gt; &lt;a rel=&quot;external&quot; href=&quot;https://opus-codec.org/&quot;&gt;Opus&lt;/a&gt; gives us tools that simplify the hardest part, the receiver:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Packet loss concealment&lt;/strong&gt;: Opus can generate concealment audio when a packet is lost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pitch estimation&lt;/strong&gt;: Opus computes the pitch of the signal and exposes it through its API, which is exactly what you need for time stretching.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With these two, a good jitter buffer is easy to build without all the signal processing that WebRTC ships with.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Send plenty of redundancy by default, and don&#39;t make the sender adaptive&lt;/strong&gt; (at least for now). Assume the network is congested, even if that costs some bitrate on a clean one.&lt;/p&gt;
&lt;h2 id=&quot;only-audio-and-one-data-channel&quot;&gt;Only audio and one data channel&lt;/h2&gt;
&lt;p&gt;No video, no multiple audio tracks, a single data channel. Instead of a generic SDP parser and negotiation, we&#39;ll implement exactly what we need: two m-lines, one for audio (Opus only) and one for the data channel.&lt;/p&gt;
&lt;p&gt;That&#39;s the whole scope: a C library that speaks just enough WebRTC to put a Raspberry Pi on a call with the Realtime API. In the next post I&#39;ll draft its public API and the structure of the project.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="es">
        <title>Hola, mundo</title>
        <published>2026-10-04T00:00:00+00:00</published>
        <updated>2026-10-04T00:00:00+00:00</updated>
        
        <author>
          <name>David Negro Martín</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://davidnegro.me/es/posts/hello-world/"/>
        <id>https://davidnegro.me/es/posts/hello-world/</id>
        
        <content type="html" xml:base="https://davidnegro.me/es/posts/hello-world/">&lt;p&gt;¡Holi! ¡Bienvenido a este blog!&lt;/p&gt;
&lt;p&gt;Este es mi primer blog. Planeo escribir sobre cosas que me gustan y me
interesan, por ejemplo:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Programación&lt;/li&gt;
&lt;li&gt;Análisis de datos&lt;/li&gt;
&lt;li&gt;Política&lt;/li&gt;
&lt;li&gt;Cosas&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Diviértete leyendo mi rincón de esta, nuestra internet.&lt;/p&gt;
</content>
        
    </entry>
</feed>
