aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/internal_services.h
blob: 042be4c1e485553e52cdde6d4c2927b088e6caec (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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_INTERNAL_SERVICES_H_
#define OSP_IMPL_INTERNAL_SERVICES_H_

#include <memory>
#include <vector>

#include "osp/impl/mdns_platform_service.h"
#include "osp/impl/mdns_responder_service.h"
#include "osp/impl/quic/quic_connection_factory.h"
#include "osp/impl/service_listener_impl.h"
#include "osp/impl/service_publisher_impl.h"
#include "osp/public/mdns_service_listener_factory.h"
#include "osp/public/mdns_service_publisher_factory.h"
#include "osp/public/protocol_connection_client.h"
#include "osp/public/protocol_connection_server.h"
#include "platform/api/network_interface.h"
#include "platform/api/time.h"
#include "platform/api/udp_socket.h"
#include "platform/base/ip_address.h"
#include "platform/base/macros.h"

namespace openscreen {

class TaskRunner;

namespace osp {

// Factory for ServiceListener and ServicePublisher instances; owns internal
// objects needed to instantiate them such as MdnsResponderService and runs an
// event loop.
// TODO(btolsch): This may be renamed and/or split up once QUIC code lands and
// this use case is more concrete.
class InternalServices : UdpSocket::Client {
 public:
  static std::unique_ptr<ServiceListener> CreateListener(
      const MdnsServiceListenerConfig& config,
      ServiceListener::Observer* observer,
      TaskRunner* task_runner);
  static std::unique_ptr<ServicePublisher> CreatePublisher(
      const ServicePublisher::Config& config,
      ServicePublisher::Observer* observer,
      TaskRunner* task_runner);

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

 private:
  class InternalPlatformLinkage final : public MdnsPlatformService {
   public:
    explicit InternalPlatformLinkage(InternalServices* parent);
    ~InternalPlatformLinkage() override;

    std::vector<BoundInterface> RegisterInterfaces(
        const std::vector<NetworkInterfaceIndex>& allowlist) override;
    void DeregisterInterfaces(
        const std::vector<BoundInterface>& registered_interfaces) override;

   private:
    InternalServices* const parent_;
    std::vector<std::unique_ptr<UdpSocket>> open_sockets_;
  };

  // The TaskRunner provided here should live for the duration of this
  // InternalService object's lifetime.
  InternalServices(ClockNowFunctionPtr now_function, TaskRunner* task_runner);
  ~InternalServices() override;

  void RegisterMdnsSocket(UdpSocket* socket);
  void DeregisterMdnsSocket(UdpSocket* socket);

  static InternalServices* ReferenceSingleton(TaskRunner* task_runner);
  static void DereferenceSingleton(void* instance);

  MdnsResponderService mdns_service_;

  TaskRunner* const task_runner_;

  OSP_DISALLOW_COPY_AND_ASSIGN(InternalServices);
};

}  // namespace osp
}  // namespace openscreen

#endif  // OSP_IMPL_INTERNAL_SERVICES_H_