• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 #ifndef OSP_IMPL_QUIC_QUIC_CONNECTION_FACTORY_IMPL_H_
6 #define OSP_IMPL_QUIC_QUIC_CONNECTION_FACTORY_IMPL_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "osp/impl/quic/quic_connection_factory.h"
13 #include "platform/api/udp_socket.h"
14 #include "platform/base/ip_address.h"
15 #include "third_party/chromium_quic/src/base/at_exit.h"
16 #include "third_party/chromium_quic/src/net/quic/quic_chromium_alarm_factory.h"
17 #include "third_party/chromium_quic/src/net/third_party/quic/quartc/quartc_factory.h"
18 
19 namespace openscreen {
20 namespace osp {
21 
22 class QuicTaskRunner;
23 
24 class QuicConnectionFactoryImpl final : public QuicConnectionFactory {
25  public:
26   explicit QuicConnectionFactoryImpl(TaskRunner* task_runner);
27   ~QuicConnectionFactoryImpl() override;
28 
29   // UdpSocket::Client overrides.
30   void OnError(UdpSocket* socket, Error error) override;
31   void OnSendError(UdpSocket* socket, Error error) override;
32   void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override;
33 
34   // QuicConnectionFactory overrides.
35   void SetServerDelegate(ServerDelegate* delegate,
36                          const std::vector<IPEndpoint>& endpoints) override;
37   std::unique_ptr<QuicConnection> Connect(
38       const IPEndpoint& endpoint,
39       QuicConnection::Delegate* connection_delegate) override;
40 
41   void OnConnectionClosed(QuicConnection* connection);
42 
43  private:
44   ::base::AtExitManager exit_manager_;
45   scoped_refptr<QuicTaskRunner> quic_task_runner_;
46   std::unique_ptr<::net::QuicChromiumAlarmFactory> alarm_factory_;
47   std::unique_ptr<::quic::QuartcFactory> quartc_factory_;
48 
49   ServerDelegate* server_delegate_ = nullptr;
50 
51   std::vector<std::unique_ptr<UdpSocket>> sockets_;
52 
53   struct OpenConnection {
54     QuicConnection* connection;
55     UdpSocket* socket;  // References one of the owned |sockets_|.
56   };
57   std::map<IPEndpoint, OpenConnection> connections_;
58 
59   // NOTE: Must be provided in constructor and stored as an instance variable
60   // rather than using the static accessor method to allow for UTs to mock this
61   // layer.
62   TaskRunner* const task_runner_;
63 };
64 
65 }  // namespace osp
66 }  // namespace openscreen
67 
68 #endif  // OSP_IMPL_QUIC_QUIC_CONNECTION_FACTORY_IMPL_H_
69