aboutsummaryrefslogtreecommitdiff
path: root/cast/common/channel/cast_socket_message_port.cc
blob: bdc33f20538df7baf026d410c3c62ec2357a7e93 (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
// 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.

#include "cast/common/channel/cast_socket_message_port.h"

#include <utility>

#include "cast/common/channel/message_util.h"
#include "cast/common/channel/proto/cast_channel.pb.h"
#include "cast/common/channel/virtual_connection.h"

namespace openscreen {
namespace cast {

CastSocketMessagePort::CastSocketMessagePort(VirtualConnectionRouter* router)
    : router_(router) {}

CastSocketMessagePort::~CastSocketMessagePort() {
  ResetClient();
}

// NOTE: we assume here that this message port is already the client for
// the passed in socket, so leave the socket's client unchanged. However,
// since sockets should map one to one with receiver sessions, we reset our
// client. The consumer of this message port should call SetClient with the new
// message port client after setting the socket.
void CastSocketMessagePort::SetSocket(WeakPtr<CastSocket> socket) {
  ResetClient();
  socket_ = socket;
}

int CastSocketMessagePort::GetSocketId() {
  return ToCastSocketId(socket_.get());
}

void CastSocketMessagePort::SetClient(MessagePort::Client* client,
                                      std::string client_sender_id) {
  ResetClient();

  client_ = client;
  client_sender_id_ = std::move(client_sender_id);
  router_->AddHandlerForLocalId(client_sender_id_, this);
}

void CastSocketMessagePort::ResetClient() {
  if (!client_) {
    return;
  }

  client_ = nullptr;
  router_->RemoveHandlerForLocalId(client_sender_id_);
  router_->RemoveConnectionsByLocalId(client_sender_id_);
  client_sender_id_.clear();
}

void CastSocketMessagePort::PostMessage(
    const std::string& destination_sender_id,
    const std::string& message_namespace,
    const std::string& message) {
  if (!client_) {
    OSP_DLOG_WARN << "Not posting message due to nullptr client_";
    return;
  }

  if (!socket_) {
    client_->OnError(Error::Code::kAlreadyClosed);
    return;
  }

  VirtualConnection connection{client_sender_id_, destination_sender_id,
                               socket_->socket_id()};
  if (!router_->GetConnectionData(connection)) {
    router_->AddConnection(connection, VirtualConnection::AssociatedData{});
  }

  const Error send_error = router_->Send(
      std::move(connection), MakeSimpleUTF8Message(message_namespace, message));
  if (!send_error.ok()) {
    client_->OnError(std::move(send_error));
  }
}

void CastSocketMessagePort::OnMessage(VirtualConnectionRouter* router,
                                      CastSocket* socket,
                                      ::cast::channel::CastMessage message) {
  OSP_DCHECK(router == router_);
  OSP_DCHECK(!socket || socket_.get() == socket);

  // Message ports are for specific virtual connections, and do not pass-through
  // broadcasts.
  if (message.destination_id() == kBroadcastId) {
    return;
  }

  if (!client_) {
    OSP_DLOG_WARN << "Dropping message due to nullptr client_";
    return;
  }

  client_->OnMessage(message.source_id(), message.namespace_(),
                     message.payload_utf8());
}

}  // namespace cast
}  // namespace openscreen