xref: /aosp_15_r20/external/openscreen/osp/impl/quic/quic_server.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1*3f982cf4SFabien Sanglard // Copyright 2018 The Chromium Authors. All rights reserved.
2*3f982cf4SFabien Sanglard // Use of this source code is governed by a BSD-style license that can be
3*3f982cf4SFabien Sanglard // found in the LICENSE file.
4*3f982cf4SFabien Sanglard 
5*3f982cf4SFabien Sanglard #ifndef OSP_IMPL_QUIC_QUIC_SERVER_H_
6*3f982cf4SFabien Sanglard #define OSP_IMPL_QUIC_QUIC_SERVER_H_
7*3f982cf4SFabien Sanglard 
8*3f982cf4SFabien Sanglard #include <cstdint>
9*3f982cf4SFabien Sanglard #include <map>
10*3f982cf4SFabien Sanglard #include <memory>
11*3f982cf4SFabien Sanglard 
12*3f982cf4SFabien Sanglard #include "osp/impl/quic/quic_connection_factory.h"
13*3f982cf4SFabien Sanglard #include "osp/impl/quic/quic_service_common.h"
14*3f982cf4SFabien Sanglard #include "osp/public/protocol_connection_server.h"
15*3f982cf4SFabien Sanglard #include "platform/api/task_runner.h"
16*3f982cf4SFabien Sanglard #include "platform/api/time.h"
17*3f982cf4SFabien Sanglard #include "platform/base/ip_address.h"
18*3f982cf4SFabien Sanglard #include "util/alarm.h"
19*3f982cf4SFabien Sanglard 
20*3f982cf4SFabien Sanglard namespace openscreen {
21*3f982cf4SFabien Sanglard namespace osp {
22*3f982cf4SFabien Sanglard 
23*3f982cf4SFabien Sanglard // This class is the default implementation of ProtocolConnectionServer for the
24*3f982cf4SFabien Sanglard // library.  It manages connections to other endpoints as well as the lifetime
25*3f982cf4SFabien Sanglard // of each incoming and outgoing stream.  It works in conjunction with a
26*3f982cf4SFabien Sanglard // QuicConnectionFactory implementation and MessageDemuxer.
27*3f982cf4SFabien Sanglard // QuicConnectionFactory provides the ability to make a new QUIC
28*3f982cf4SFabien Sanglard // connection from packets received on its server sockets.  Incoming data is
29*3f982cf4SFabien Sanglard // given to the QuicServer by the underlying QUIC implementation (through
30*3f982cf4SFabien Sanglard // QuicConnectionFactory) and this is in turn handed to MessageDemuxer for
31*3f982cf4SFabien Sanglard // routing CBOR messages.
32*3f982cf4SFabien Sanglard class QuicServer final : public ProtocolConnectionServer,
33*3f982cf4SFabien Sanglard                          public QuicConnectionFactory::ServerDelegate,
34*3f982cf4SFabien Sanglard                          public ServiceConnectionDelegate::ServiceDelegate {
35*3f982cf4SFabien Sanglard  public:
36*3f982cf4SFabien Sanglard   QuicServer(const ServerConfig& config,
37*3f982cf4SFabien Sanglard              MessageDemuxer* demuxer,
38*3f982cf4SFabien Sanglard              std::unique_ptr<QuicConnectionFactory> connection_factory,
39*3f982cf4SFabien Sanglard              ProtocolConnectionServer::Observer* observer,
40*3f982cf4SFabien Sanglard              ClockNowFunctionPtr now_function,
41*3f982cf4SFabien Sanglard              TaskRunner* task_runner);
42*3f982cf4SFabien Sanglard   ~QuicServer() override;
43*3f982cf4SFabien Sanglard 
44*3f982cf4SFabien Sanglard   // ProtocolConnectionServer overrides.
45*3f982cf4SFabien Sanglard   bool Start() override;
46*3f982cf4SFabien Sanglard   bool Stop() override;
47*3f982cf4SFabien Sanglard   bool Suspend() override;
48*3f982cf4SFabien Sanglard   bool Resume() override;
49*3f982cf4SFabien Sanglard   std::unique_ptr<ProtocolConnection> CreateProtocolConnection(
50*3f982cf4SFabien Sanglard       uint64_t endpoint_id) override;
51*3f982cf4SFabien Sanglard 
52*3f982cf4SFabien Sanglard   // QuicProtocolConnection::Owner overrides.
53*3f982cf4SFabien Sanglard   void OnConnectionDestroyed(QuicProtocolConnection* connection) override;
54*3f982cf4SFabien Sanglard 
55*3f982cf4SFabien Sanglard   // ServiceConnectionDelegate::ServiceDelegate overrides.
56*3f982cf4SFabien Sanglard   uint64_t OnCryptoHandshakeComplete(ServiceConnectionDelegate* delegate,
57*3f982cf4SFabien Sanglard                                      uint64_t connection_id) override;
58*3f982cf4SFabien Sanglard   void OnIncomingStream(
59*3f982cf4SFabien Sanglard       std::unique_ptr<QuicProtocolConnection> connection) override;
60*3f982cf4SFabien Sanglard   void OnConnectionClosed(uint64_t endpoint_id,
61*3f982cf4SFabien Sanglard                           uint64_t connection_id) override;
62*3f982cf4SFabien Sanglard   void OnDataReceived(uint64_t endpoint_id,
63*3f982cf4SFabien Sanglard                       uint64_t connection_id,
64*3f982cf4SFabien Sanglard                       const uint8_t* data,
65*3f982cf4SFabien Sanglard                       size_t data_size) override;
66*3f982cf4SFabien Sanglard 
67*3f982cf4SFabien Sanglard  private:
68*3f982cf4SFabien Sanglard   void CloseAllConnections();
69*3f982cf4SFabien Sanglard 
70*3f982cf4SFabien Sanglard   // QuicConnectionFactory::ServerDelegate overrides.
71*3f982cf4SFabien Sanglard   QuicConnection::Delegate* NextConnectionDelegate(
72*3f982cf4SFabien Sanglard       const IPEndpoint& source) override;
73*3f982cf4SFabien Sanglard   void OnIncomingConnection(
74*3f982cf4SFabien Sanglard       std::unique_ptr<QuicConnection> connection) override;
75*3f982cf4SFabien Sanglard 
76*3f982cf4SFabien Sanglard   // Deletes dead QUIC connections then returns the time interval before this
77*3f982cf4SFabien Sanglard   // method should be run again.
78*3f982cf4SFabien Sanglard   void Cleanup();
79*3f982cf4SFabien Sanglard 
80*3f982cf4SFabien Sanglard   const std::vector<IPEndpoint> connection_endpoints_;
81*3f982cf4SFabien Sanglard   std::unique_ptr<QuicConnectionFactory> connection_factory_;
82*3f982cf4SFabien Sanglard 
83*3f982cf4SFabien Sanglard   std::unique_ptr<ServiceConnectionDelegate> pending_connection_delegate_;
84*3f982cf4SFabien Sanglard 
85*3f982cf4SFabien Sanglard   // Maps an IPEndpoint to a generated endpoint ID.  This is used to insulate
86*3f982cf4SFabien Sanglard   // callers from post-handshake changes to a connections actual peer endpoint.
87*3f982cf4SFabien Sanglard   std::map<IPEndpoint, uint64_t> endpoint_map_;
88*3f982cf4SFabien Sanglard 
89*3f982cf4SFabien Sanglard   // Value that will be used for the next new endpoint in a Connect call.
90*3f982cf4SFabien Sanglard   uint64_t next_endpoint_id_ = 0;
91*3f982cf4SFabien Sanglard 
92*3f982cf4SFabien Sanglard   // Maps endpoint addresses to data about connections that haven't successfully
93*3f982cf4SFabien Sanglard   // completed the QUIC handshake.
94*3f982cf4SFabien Sanglard   std::map<IPEndpoint, ServiceConnectionData> pending_connections_;
95*3f982cf4SFabien Sanglard 
96*3f982cf4SFabien Sanglard   // Maps endpoint IDs to data about connections that have successfully
97*3f982cf4SFabien Sanglard   // completed the QUIC handshake.
98*3f982cf4SFabien Sanglard   std::map<uint64_t, ServiceConnectionData> connections_;
99*3f982cf4SFabien Sanglard 
100*3f982cf4SFabien Sanglard   // Connections (endpoint IDs) that need to be destroyed, but have to wait for
101*3f982cf4SFabien Sanglard   // the next event loop due to the underlying QUIC implementation's way of
102*3f982cf4SFabien Sanglard   // referencing them.
103*3f982cf4SFabien Sanglard   std::vector<uint64_t> delete_connections_;
104*3f982cf4SFabien Sanglard 
105*3f982cf4SFabien Sanglard   Alarm cleanup_alarm_;
106*3f982cf4SFabien Sanglard };
107*3f982cf4SFabien Sanglard 
108*3f982cf4SFabien Sanglard }  // namespace osp
109*3f982cf4SFabien Sanglard }  // namespace openscreen
110*3f982cf4SFabien Sanglard 
111*3f982cf4SFabien Sanglard #endif  // OSP_IMPL_QUIC_QUIC_SERVER_H_
112