aboutsummaryrefslogtreecommitdiff
path: root/cast/sender/public/sender_socket_factory.h
blob: 0b9b05aa24492a0d6f3430f19189e282fff92828 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2019 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 CAST_SENDER_PUBLIC_SENDER_SOCKET_FACTORY_H_
#define CAST_SENDER_PUBLIC_SENDER_SOCKET_FACTORY_H_

#include <openssl/x509.h>

#include <memory>
#include <set>
#include <utility>
#include <vector>

#include "cast/common/public/cast_socket.h"
#include "platform/api/serial_delete_ptr.h"
#include "platform/api/task_runner.h"
#include "platform/api/tls_connection_factory.h"
#include "platform/base/ip_address.h"

namespace openscreen {
namespace cast {

class AuthContext;

class SenderSocketFactory final : public TlsConnectionFactory::Client,
                                  public CastSocket::Client {
 public:
  class Client {
   public:
    virtual void OnConnected(SenderSocketFactory* factory,
                             const IPEndpoint& endpoint,
                             std::unique_ptr<CastSocket> socket) = 0;
    virtual void OnError(SenderSocketFactory* factory,
                         const IPEndpoint& endpoint,
                         Error error) = 0;

   protected:
    virtual ~Client();
  };

  enum class DeviceMediaPolicy {
    kNone = 0,
    kAudioOnly,
    kIncludesVideo,
  };

  // |client| and |task_runner| must outlive |this|.
  SenderSocketFactory(Client* client, TaskRunner* task_runner);
  ~SenderSocketFactory();

  // |factory| cannot be nullptr and must outlive |this|.
  void set_factory(TlsConnectionFactory* factory);

  // Begins connecting to a Cast device at |endpoint|.  If a successful
  // connection is made, including device authentication, the new CastSocket
  // will be passed to |client_|'s OnConnected method.  The new CastSocket will
  // have its client set to |client|.  If any part of the connection process
  // fails, |client_|'s OnError method is called instead.  This includes if the
  // device's media policy, as determined by authentication, is audio-only and
  // |media_policy| is kIncludesVideo.
  void Connect(const IPEndpoint& endpoint,
               DeviceMediaPolicy media_policy,
               CastSocket::Client* client);

  // TlsConnectionFactory::Client overrides.
  void OnAccepted(TlsConnectionFactory* factory,
                  std::vector<uint8_t> der_x509_peer_cert,
                  std::unique_ptr<TlsConnection> connection) override;
  void OnConnected(TlsConnectionFactory* factory,
                   std::vector<uint8_t> der_x509_peer_cert,
                   std::unique_ptr<TlsConnection> connection) override;
  void OnConnectionFailed(TlsConnectionFactory* factory,
                          const IPEndpoint& remote_address) override;
  void OnError(TlsConnectionFactory* factory, Error error) override;

 private:
  struct PendingConnection {
    IPEndpoint endpoint;
    DeviceMediaPolicy media_policy;
    CastSocket::Client* client;
  };

  struct PendingAuth {
    IPEndpoint endpoint;
    DeviceMediaPolicy media_policy;
    SerialDeletePtr<CastSocket> socket;
    CastSocket::Client* client;
    std::unique_ptr<AuthContext> auth_context;
    bssl::UniquePtr<X509> peer_cert;
  };

  friend bool operator<(const std::unique_ptr<PendingAuth>& a, int b);
  friend bool operator<(int a, const std::unique_ptr<PendingAuth>& b);

  std::vector<PendingConnection>::iterator FindPendingConnection(
      const IPEndpoint& endpoint);

  // CastSocket::Client overrides.
  void OnError(CastSocket* socket, Error error) override;
  void OnMessage(CastSocket* socket,
                 ::cast::channel::CastMessage message) override;

  Client* const client_;
  TaskRunner* const task_runner_;
  TlsConnectionFactory* factory_ = nullptr;
  std::vector<PendingConnection> pending_connections_;
  std::vector<std::unique_ptr<PendingAuth>> pending_auth_;
};

}  // namespace cast
}  // namespace openscreen

#endif  // CAST_SENDER_PUBLIC_SENDER_SOCKET_FACTORY_H_