xref: /aosp_15_r20/external/openscreen/cast/common/channel/cast_socket.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
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 #include "cast/common/public/cast_socket.h"
6 
7 #include "cast/common/channel/message_framer.h"
8 #include "cast/common/channel/proto/cast_channel.pb.h"
9 #include "util/osp_logging.h"
10 
11 namespace openscreen {
12 namespace cast {
13 
14 using ::cast::channel::CastMessage;
15 using message_serialization::DeserializeResult;
16 
17 CastSocket::Client::~Client() = default;
18 
CastSocket(std::unique_ptr<TlsConnection> connection,Client * client)19 CastSocket::CastSocket(std::unique_ptr<TlsConnection> connection,
20                        Client* client)
21     : connection_(std::move(connection)),
22       client_(client),
23       socket_id_(g_next_socket_id_++) {
24   OSP_DCHECK(client);
25   connection_->SetClient(this);
26 }
27 
~CastSocket()28 CastSocket::~CastSocket() {
29   connection_->SetClient(nullptr);
30 }
31 
Send(const CastMessage & message)32 Error CastSocket::Send(const CastMessage& message) {
33   if (state_ == State::kError) {
34     return Error::Code::kSocketClosedFailure;
35   }
36 
37   const ErrorOr<std::vector<uint8_t>> out =
38       message_serialization::Serialize(message);
39   if (!out) {
40     return out.error();
41   }
42 
43   if (!connection_->Send(out.value().data(), out.value().size())) {
44     return Error::Code::kAgain;
45   }
46   return Error::Code::kNone;
47 }
48 
SetClient(Client * client)49 void CastSocket::SetClient(Client* client) {
50   OSP_DCHECK(client);
51   client_ = client;
52 }
53 
GetSanitizedIpAddress()54 std::array<uint8_t, 2> CastSocket::GetSanitizedIpAddress() {
55   IPEndpoint remote = connection_->GetRemoteEndpoint();
56   std::array<uint8_t, 2> result;
57   uint8_t bytes[16];
58   if (remote.address.IsV4()) {
59     remote.address.CopyToV4(bytes);
60     result[0] = bytes[2];
61     result[1] = bytes[3];
62   } else {
63     remote.address.CopyToV6(bytes);
64     result[0] = bytes[14];
65     result[1] = bytes[15];
66   }
67   return result;
68 }
69 
OnError(TlsConnection * connection,Error error)70 void CastSocket::OnError(TlsConnection* connection, Error error) {
71   state_ = State::kError;
72   client_->OnError(this, error);
73 }
74 
OnRead(TlsConnection * connection,std::vector<uint8_t> block)75 void CastSocket::OnRead(TlsConnection* connection, std::vector<uint8_t> block) {
76   read_buffer_.insert(read_buffer_.end(), block.begin(), block.end());
77   // NOTE: Read as many messages as possible out of |read_buffer_| since we only
78   // get one callback opportunity for this.
79   do {
80     ErrorOr<DeserializeResult> message_or_error =
81         message_serialization::TryDeserialize(
82             absl::Span<uint8_t>(&read_buffer_[0], read_buffer_.size()));
83     if (!message_or_error) {
84       return;
85     }
86     read_buffer_.erase(read_buffer_.begin(),
87                        read_buffer_.begin() + message_or_error.value().length);
88     client_->OnMessage(this, std::move(message_or_error.value().message));
89   } while (!read_buffer_.empty());
90 }
91 
92 int CastSocket::g_next_socket_id_ = 1;
93 
94 }  // namespace cast
95 }  // namespace openscreen
96