1# Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE.
2LSQUIC APIs
3===========
4
5LSQUIC exposes the following object types to the user:
6
7    - Engine Settings   (struct lsquic_engine_settings)
8    - Stream Interface  (struct lsquic_stream_if) 
9    - Engine API        (struct lsquic_engine_api)
10    - Engine
11    - Connection
12    - Stream
13
14The first three -- engine settings, engine APIs, and stream interface --
15are used to instantiate the engine.  After engine is instantiated, the
16user code need only concern itself with engine, connections, and streams.
17
18
19Engine Settings
20---------------
21
22Engine settings is the struct lsquic_engine_settings.  It contains various
23QUIC settings and LSQUIC parameters.  The usual way to use it is to initialize
24it to default values using lsquic_engine_init_settings(), modify any values
25if necessary, and pass it as parameter to lsquic_engine_new().
26
27QUIC settings are specified by the following members:
28
29    lsquic_engine_settings      QUIC
30    member                      parameter
31    ----------------------      ---------
32    es_cfcw                     CFCW
33    es_sfcw                     SFCW
34    es_max_streams_in           MIDS
35    es_ua                       UAID
36    es_versions                 VER
37    es_idle_conn_to             ICSL
38    es_silent_close             SCLS
39    es_support_srej             COPT/SREJ
40    es_support_nstp             COPT/NSTP
41    es_support_tcid0            TCID
42
43The following parameters affect run-time behavior:
44
45    es_rw_once                  Important: affects event dispatch
46    es_handshake_to
47    es_support_push
48    es_pace_packets
49
50Other noteworthy settings:
51
52    es_max_header_list_size
53    es_progress_check
54
55To be sure your settings are good (in other words, passing this struct won't
56trip up the engine constructor), use lsquic_engine_check_settings().
57
58
59Stream Interface
60----------------
61
62The stream interface, lsquic_stream_if, specifies callbacks LSQUIC engine
63will call for connections and streams.
64
65The following callbacks should be specified for connection:
66
67    on_new_conn             This is called when connection is created.
68
69    on_goaway_received      This function is called when we receive GOAWAY
70                            frame from peer.  This callback is optional.
71
72    on_conn_closed          Connection is closed: all streams have been
73                            destroyed.
74
75The streams have four callbacks:
76
77    on_new_stream           Stream has been created.
78
79    on_read                 Stream can be read from (see Events).
80
81    on_write                Stream can be written to (see Events).
82
83    on_close                Stream has been closed.
84
85For both connections and streams, the "on new" callback return value can
86be used to specify user-supplied data.  This data pointer is optional and
87can be NULL.  It can also refer to the same data for the connection and
88its streams.  "on close" callbacks should be used to free user-supplied
89data.
90
91
92Engine API
93----------
94
95The engine API, struct lsquic_engine_api, is a combination structure to
96make calling lsquic_engine_new() manageable.  It holds references to
97struct lsquic_engine_settings and struct lsquic_stream_if, as well as:
98
99    - Interface for sending outgoing packets, ea_packets_out
100    - Interface for allocating memory for outgoing packet buffers
101      (optional).
102    - Interface for share memory hash, ea_shi
103    - Optional interface for reporting connections whose handshake
104      did not complete (ea_bad_handshake)
105
106ea_packets_out is a pointer to a function of type lsquic_packets_out_f.
107The engine calls this function when it is appropriate to send out packets
108for one or more connections, which it gives to the function in a batch.
109This batch is an array of struct lsquic_out_spec.
110
111
112Engine
113------
114
115The engine is instantiated using lsquic_engine_new().  The first parameter
116is a list flags and the second parameter is the reference to the engine
117api.  The engine settings are specified, they are copied; changing
118the setting after the engine has been created will not affect engine's
119behavior.  If the settings are not specified, the engine will use default
120settings created by lsquic_engine_init_settings().
121
122Once the engine is instantiated, there are four main ways to use it to
123drive QUIC connections:
124
125    1. Create a connection using lsquic_engine_connect().
126    2. Feed it incoming packets using lsquic_engine_packet_in() function.
127    3. Process connections using one of the connection queue functions
128       (see Connection Queues).
129    4. Accept outgoing packets for sending (and send them!) using
130       ea_packets_out callback.
131
132
133Connection Management
134---------------------
135
136A connection needs to be processed once in a while.  It needs to be
137processed when one of the following is true:
138
139    - There are incoming packets;
140    - A stream is both readable by the user code and the user code wants
141      to read from it;
142    - A stream is both writeable by the user code and the user code wants
143      to write to it;
144    - User has written to stream outside of on_write() callbacks (that is
145      allowed) and now there are packets ready to be sent;
146    - A timer (pacer, retransmission, idle, etc) has expired;
147    - A control frame needs to be sent out;
148    - A stream needs to be serviced or created.
149
150Each of these use cases is handled by a single function:
151
152    lsquic_engine_process_conns()
153
154The connections to which the conditions above apply are processed (or
155"ticked") in the least recently ticked order.  After calling this function,
156you can see when is the next time a connection needs to be processed using
157
158    lsquic_engine_earliest_adv_tick()
159
160Based on this value, next event can be scheduled (in the event loop of
161your choice).
162
163Connection
164----------
165
166A connection is created using lsquic_engine_connect().  When on_new_conn()
167is called, the client code should call lsquic_conn_make_stream() one or
168more times.  One new stream will be created for each one of those calls.
169
170Several auxiliary functions are available:
171
172    - lsquic_conn_id()
173    - lsquic_conn_going_away()
174    - lsquic_conn_get_peer_ctx()
175    - lsquic_conn_get_stream_by_id()
176    - lsquic_conn_get_ctx()
177
178
179Stream
180------
181
182LSQUIC stream hides QUIC and HTTP/2 framing complexities from the user.
183What it presents is a way to send HTTP headers and, optionally, body to
184peer.  On read side, the user gets what looks like HTTP/1.1 stream.
185
186Expected usage for client is to express the desire to write to stream
187using lsquic_stream_wantwrite() call.  Once on_write() is called:
188
189    1. Write headers using lsquic_stream_send_headers()
190    2. Optionally write payload body using of of lsquic_stream_write(),
191       lsquic_stream_writev(), or lsquic_stream_writef().
192
193That done, shutdown write side using lsquic_stream_shutdown(), unregister
194for write events and register for read events using lsquic_stream_wantread().
195
196Read and parse HTTP/1.1 stream from on_read() callback until end-of-stream
197or an error is encountered.
198
199Then unregister the read event and shutdown the read side.  The stream will
200be closed after that at some point and on_close() callback will be called,
201at which point resources can be freed.  (Internally, the stream object is
202not destroyed until either all the packets carrying its data are ACKed or
203the connection is destroyed).
204
205on_read() and on_write() callbacks are dispatched differently based on the
206value of es_rw_once:
207
208If es_rw_once is false, then the callbacks are dispatched in a loop until
209the user unregisters the event or the stream becomes unreadable (or
210unwriteable).
211
212If es_rw_once is true, on_read() and on_write() are called once "per tick".
213It is the up to the user to read and write enough data.
214
215
216Events
217------
218
219Stream events are persistent: once call lsquic_stream_wantwrite() or
220lsquic_stream_wantread(), the event stays active until turned off.
221
222Note that when an error is encountered (such as a stream reset), the
223stream becomes readable and writeable: this allows user code to collect
224the error.
225
226
227Versions
228--------
229
230QUIC version are listed in enum lsquic_version.  To specify a list of
231versions, they are usually placed in a bitmask, e.g. es_versions.
232