• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This header contains interfaces that abstract away different backing
6 // protocols for WebTransport.
7 
8 #ifndef QUICHE_WEB_TRANSPORT_WEB_TRANSPORT_H_
9 #define QUICHE_WEB_TRANSPORT_WEB_TRANSPORT_H_
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <memory>
14 #include <string>
15 
16 // The dependencies of this API should be kept minimal and independent of
17 // specific transport implementations.
18 #include "absl/strings/string_view.h"
19 #include "absl/time/time.h"
20 #include "absl/types/span.h"
21 #include "quiche/common/platform/api/quiche_export.h"
22 #include "quiche/common/quiche_callbacks.h"
23 #include "quiche/common/quiche_stream.h"
24 
25 namespace webtransport {
26 
27 enum class Perspective { kClient, kServer };
28 
29 // A numeric ID uniquely identifying a WebTransport stream. Note that by design,
30 // those IDs are not available in the Web API, and the IDs do not necessarily
31 // match between client and server perspective, since there may be a proxy
32 // between them.
33 using StreamId = uint32_t;
34 // Application-specific error code used for resetting either the read or the
35 // write half of the stream.
36 using StreamErrorCode = uint32_t;
37 // Application-specific error code used for closing a WebTransport session.
38 using SessionErrorCode = uint32_t;
39 
40 // An outcome of a datagram send call.
41 enum class DatagramStatusCode {
42   // Datagram has been successfully sent or placed into the datagram queue.
43   kSuccess,
44   // Datagram has not been sent since the underlying QUIC connection is blocked
45   // by the congestion control.  Note that this can only happen if the queue is
46   // full.
47   kBlocked,
48   // Datagram has not been sent since it is too large to fit into a single
49   // UDP packet.
50   kTooBig,
51   // An unspecified internal error.
52   kInternalError,
53 };
54 
55 // An outcome of a datagram send call, in both enum and human-readable form.
56 struct QUICHE_EXPORT DatagramStatus {
DatagramStatusDatagramStatus57   explicit DatagramStatus(DatagramStatusCode code, std::string error_message)
58       : code(code), error_message(std::move(error_message)) {}
59 
60   DatagramStatusCode code;
61   std::string error_message;
62 };
63 
64 enum class StreamType {
65   kUnidirectional,
66   kBidirectional,
67 };
68 
69 // Based on
70 // https://w3c.github.io/webtransport/#dictdef-webtransportdatagramstats.
71 struct QUICHE_EXPORT DatagramStats {
72   uint64_t expired_outgoing;
73   uint64_t lost_outgoing;
74 
75   // droppedIncoming is not present, since in the C++ API, we immediately
76   // deliver datagrams via callback, meaning there is no queue where things
77   // would be dropped.
78 };
79 
80 // Based on https://w3c.github.io/webtransport/#web-transport-stats
81 // Note that this is currently not a complete implementation of that API, as
82 // some of those still need to be clarified in
83 // https://github.com/w3c/webtransport/issues/537
84 struct QUICHE_EXPORT SessionStats {
85   absl::Duration min_rtt;
86   absl::Duration smoothed_rtt;
87   absl::Duration rtt_variation;
88 
89   uint64_t estimated_send_rate_bps;  // In bits per second.
90 
91   DatagramStats datagram_stats;
92 };
93 
94 // The stream visitor is an application-provided object that gets notified about
95 // events related to a WebTransport stream.  The visitor object is owned by the
96 // stream itself, meaning that if the stream is ever fully closed, the visitor
97 // will be garbage-collected.
98 class QUICHE_EXPORT StreamVisitor : public quiche::ReadStreamVisitor,
99                                     public quiche::WriteStreamVisitor {
100  public:
~StreamVisitor()101   virtual ~StreamVisitor() {}
102 
103   // Called when RESET_STREAM is received for the stream.
104   virtual void OnResetStreamReceived(StreamErrorCode error) = 0;
105   // Called when STOP_SENDING is received for the stream.
106   virtual void OnStopSendingReceived(StreamErrorCode error) = 0;
107   // Called when the write side of the stream is closed and all of the data sent
108   // has been acknowledged ("Data Recvd" state of RFC 9000).  Primarily used by
109   // the state machine of the Web API.
110   virtual void OnWriteSideInDataRecvdState() = 0;
111 };
112 
113 // A stream (either bidirectional or unidirectional) that is contained within a
114 // WebTransport session.
115 class QUICHE_EXPORT Stream : public quiche::ReadStream,
116                              public quiche::WriteStream,
117                              public quiche::TerminableStream {
118  public:
~Stream()119   virtual ~Stream() {}
120 
121   // An ID that is unique within the session.  Those are not exposed to the user
122   // via the web API, but can be used internally for bookkeeping and
123   // diagnostics.
124   virtual StreamId GetStreamId() const = 0;
125 
126   // Resets the read or the write side of the stream with the specified error
127   // code.
128   virtual void ResetWithUserCode(StreamErrorCode error) = 0;
129   virtual void SendStopSending(StreamErrorCode error) = 0;
130 
131   // A general-purpose stream reset method that may be used when a specific
132   // error code is not available.
133   virtual void ResetDueToInternalError() = 0;
134   // If the stream has not been already reset, reset the stream. This is
135   // primarily used in the JavaScript API when the stream object has been
136   // garbage collected.
137   virtual void MaybeResetDueToStreamObjectGone() = 0;
138 
139   virtual StreamVisitor* visitor() = 0;
140   virtual void SetVisitor(std::unique_ptr<StreamVisitor> visitor) = 0;
141 };
142 
143 // Visitor that gets notified about events related to a WebTransport session.
144 class QUICHE_EXPORT SessionVisitor {
145  public:
~SessionVisitor()146   virtual ~SessionVisitor() {}
147 
148   // Notifies the visitor when the session is ready to exchange application
149   // data.
150   virtual void OnSessionReady() = 0;
151 
152   // Notifies the visitor when the session has been closed.
153   virtual void OnSessionClosed(SessionErrorCode error_code,
154                                const std::string& error_message) = 0;
155 
156   // Notifies the visitor when a new stream has been received.  The stream in
157   // question can be retrieved using AcceptIncomingBidirectionalStream() or
158   // AcceptIncomingUnidirectionalStream().
159   virtual void OnIncomingBidirectionalStreamAvailable() = 0;
160   virtual void OnIncomingUnidirectionalStreamAvailable() = 0;
161 
162   // Notifies the visitor when a new datagram has been received.
163   virtual void OnDatagramReceived(absl::string_view datagram) = 0;
164 
165   // Notifies the visitor that a new outgoing stream can now be created.
166   virtual void OnCanCreateNewOutgoingBidirectionalStream() = 0;
167   virtual void OnCanCreateNewOutgoingUnidirectionalStream() = 0;
168 };
169 
170 // An abstract interface for a WebTransport session.
171 //
172 // *** AN IMPORTANT NOTE ABOUT STREAM LIFETIMES ***
173 // Stream objects are managed internally by the underlying QUIC stack, and can
174 // go away at any time due to the peer resetting the stream. Because of that,
175 // any pointers to the stream objects returned by this class MUST NEVER be
176 // retained long-term, except inside the stream visitor (the stream visitor is
177 // owned by the stream object). If you need to store a reference to a stream,
178 // consider one of the two following options:
179 //   (1) store a stream ID,
180 //   (2) store a weak pointer to the stream visitor, and then access the stream
181 //       via the said visitor (the visitor is guaranteed to be alive as long as
182 //       the stream is alive).
183 class QUICHE_EXPORT Session {
184  public:
~Session()185   virtual ~Session() {}
186 
187   // Closes the WebTransport session in question with the specified |error_code|
188   // and |error_message|.
189   virtual void CloseSession(SessionErrorCode error_code,
190                             absl::string_view error_message) = 0;
191 
192   // Return the earliest incoming stream that has been received by the session
193   // but has not been accepted.  Returns nullptr if there are no incoming
194   // streams.  See the class note regarding the lifetime of the returned stream
195   // object.
196   virtual Stream* AcceptIncomingBidirectionalStream() = 0;
197   virtual Stream* AcceptIncomingUnidirectionalStream() = 0;
198 
199   // Returns true if flow control allows opening a new stream.
200   //
201   // IMPORTANT: See the class note regarding the lifetime of the returned stream
202   // object.
203   virtual bool CanOpenNextOutgoingBidirectionalStream() = 0;
204   virtual bool CanOpenNextOutgoingUnidirectionalStream() = 0;
205 
206   // Opens a new WebTransport stream, or returns nullptr if that is not possible
207   // due to flow control.  See the class note regarding the lifetime of the
208   // returned stream object.
209   //
210   // IMPORTANT: See the class note regarding the lifetime of the returned stream
211   // object.
212   virtual Stream* OpenOutgoingBidirectionalStream() = 0;
213   virtual Stream* OpenOutgoingUnidirectionalStream() = 0;
214 
215   // Returns the WebTransport stream with the corresponding ID.
216   //
217   // IMPORTANT: See the class note regarding the lifetime of the returned stream
218   // object.
219   virtual Stream* GetStreamById(StreamId id) = 0;
220 
221   virtual DatagramStatus SendOrQueueDatagram(absl::string_view datagram) = 0;
222   // Returns a conservative estimate of the largest datagram size that the
223   // session would be able to send.
224   virtual uint64_t GetMaxDatagramSize() const = 0;
225   // Sets the largest duration that a datagram can spend in the queue before
226   // being silently dropped.
227   virtual void SetDatagramMaxTimeInQueue(absl::Duration max_time_in_queue) = 0;
228 
229   // Returns stats that generally follow the semantics of W3C WebTransport API.
230   virtual DatagramStats GetDatagramStats() = 0;
231   virtual SessionStats GetSessionStats() = 0;
232 
233   // Sends a DRAIN_WEBTRANSPORT_SESSION capsule or an equivalent signal to the
234   // peer indicating that the session is draining.
235   virtual void NotifySessionDraining() = 0;
236   // Notifies that either the session itself (DRAIN_WEBTRANSPORT_SESSION
237   // capsule), or the underlying connection (HTTP GOAWAY) is being drained by
238   // the peer.
239   virtual void SetOnDraining(quiche::SingleUseCallback<void()> callback) = 0;
240 };
241 
242 }  // namespace webtransport
243 
244 #endif  // QUICHE_WEB_TRANSPORT_WEB_TRANSPORT_H_
245