aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/quic/quic_connection_factory_impl.h
blob: 8e29579d3b63eed8dac1e372cdd00dfd65a1929b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef OSP_IMPL_QUIC_QUIC_CONNECTION_FACTORY_IMPL_H_
#define OSP_IMPL_QUIC_QUIC_CONNECTION_FACTORY_IMPL_H_

#include <map>
#include <memory>

#include "osp/impl/quic/quic_connection_factory.h"
#include "platform/api/udp_socket.h"
#include "platform/base/ip_address.h"
#include "third_party/chromium_quic/src/base/at_exit.h"
#include "third_party/chromium_quic/src/net/quic/quic_chromium_alarm_factory.h"
#include "third_party/chromium_quic/src/net/third_party/quic/quartc/quartc_factory.h"

namespace openscreen {
namespace osp {

class QuicTaskRunner;

class QuicConnectionFactoryImpl final : public QuicConnectionFactory {
 public:
  QuicConnectionFactoryImpl(platform::TaskRunner* task_runner);
  ~QuicConnectionFactoryImpl() override;

  // UdpSocket::Client overrides.
  void OnError(platform::UdpSocket* socket, Error error) override;
  void OnSendError(platform::UdpSocket* socket, Error error) override;
  void OnRead(platform::UdpSocket* socket,
              ErrorOr<platform::UdpPacket> packet) override;

  // QuicConnectionFactory overrides.
  void SetServerDelegate(ServerDelegate* delegate,
                         const std::vector<IPEndpoint>& endpoints) override;
  std::unique_ptr<QuicConnection> Connect(
      const IPEndpoint& endpoint,
      QuicConnection::Delegate* connection_delegate) override;

  void OnConnectionClosed(QuicConnection* connection);

 private:
  ::base::AtExitManager exit_manager_;
  scoped_refptr<QuicTaskRunner> quic_task_runner_;
  std::unique_ptr<::net::QuicChromiumAlarmFactory> alarm_factory_;
  std::unique_ptr<::quic::QuartcFactory> quartc_factory_;

  ServerDelegate* server_delegate_ = nullptr;

  std::vector<platform::UdpSocketUniquePtr> sockets_;

  struct OpenConnection {
    QuicConnection* connection;
    platform::UdpSocket* socket;  // References one of the owned |sockets_|.
  };
  std::map<IPEndpoint, OpenConnection, IPEndpointComparator> connections_;

  // NOTE: Must be provided in constructor and stored as an instance variable
  // rather than using the static accessor method to allow for UTs to mock this
  // layer.
  platform::TaskRunner* const task_runner_;
};

}  // namespace osp
}  // namespace openscreen

#endif  // OSP_IMPL_QUIC_QUIC_CONNECTION_FACTORY_IMPL_H_