1 // Copyright 2019 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 CAST_COMMON_PUBLIC_CAST_SOCKET_H_
6 #define CAST_COMMON_PUBLIC_CAST_SOCKET_H_
7 
8 #include <array>
9 #include <memory>
10 #include <vector>
11 
12 #include "platform/api/tls_connection.h"
13 #include "util/weak_ptr.h"
14 
15 namespace cast {
16 namespace channel {
17 class CastMessage;
18 }  // namespace channel
19 }  // namespace cast
20 
21 namespace openscreen {
22 namespace cast {
23 
24 // Represents a simple message-oriented socket for communicating with the Cast
25 // V2 protocol.  It isn't thread-safe, so it should only be used on the same
26 // TaskRunner thread as its TlsConnection.
27 class CastSocket : public TlsConnection::Client {
28  public:
29   class Client {
30    public:
31     virtual ~Client() = default;
32 
33     // Called when a terminal error on |socket| has occurred.
34     virtual void OnError(CastSocket* socket, Error error) = 0;
35 
36     virtual void OnMessage(CastSocket* socket,
37                            ::cast::channel::CastMessage message) = 0;
38   };
39 
40   CastSocket(std::unique_ptr<TlsConnection> connection, Client* client);
41   ~CastSocket();
42 
43   // Sends |message| immediately unless the underlying TLS connection is
44   // write-blocked, in which case |message| will be queued.  An error will be
45   // returned if |message| cannot be serialized for any reason, even while
46   // write-blocked.
47   [[nodiscard]] Error Send(const ::cast::channel::CastMessage& message);
48 
49   void SetClient(Client* client);
50 
51   std::array<uint8_t, 2> GetSanitizedIpAddress();
52 
socket_id()53   int socket_id() const { return socket_id_; }
54 
set_audio_only(bool audio_only)55   void set_audio_only(bool audio_only) { audio_only_ = audio_only; }
audio_only()56   bool audio_only() const { return audio_only_; }
57 
58   // TlsConnection::Client overrides.
59   void OnError(TlsConnection* connection, Error error) override;
60   void OnRead(TlsConnection* connection, std::vector<uint8_t> block) override;
61 
GetWeakPtr()62   WeakPtr<CastSocket> GetWeakPtr() const { return weak_factory_.GetWeakPtr(); }
63 
64  private:
65   enum class State : bool {
66     kOpen = true,
67     kError = false,
68   };
69 
70   static int g_next_socket_id_;
71 
72   const std::unique_ptr<TlsConnection> connection_;
73   Client* client_;  // May never be null.
74   const int socket_id_;
75   bool audio_only_ = false;
76   std::vector<uint8_t> read_buffer_;
77   State state_ = State::kOpen;
78 
79   WeakPtrFactory<CastSocket> weak_factory_{this};
80 };
81 
82 // Returns socket->socket_id() if |socket| is not null, otherwise 0.
ToCastSocketId(CastSocket * socket)83 constexpr int ToCastSocketId(CastSocket* socket) {
84   return socket ? socket->socket_id() : 0;
85 }
86 
87 }  // namespace cast
88 }  // namespace openscreen
89 
90 #endif  // CAST_COMMON_PUBLIC_CAST_SOCKET_H_
91