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_CHANNEL_CAST_SOCKET_MESSAGE_PORT_H_
6 #define CAST_COMMON_CHANNEL_CAST_SOCKET_MESSAGE_PORT_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "cast/common/channel/cast_message_handler.h"
13 #include "cast/common/channel/virtual_connection_router.h"
14 #include "cast/common/public/cast_socket.h"
15 #include "cast/common/public/message_port.h"
16 #include "util/weak_ptr.h"
17 
18 namespace openscreen {
19 namespace cast {
20 
21 class CastSocketMessagePort : public MessagePort, public CastMessageHandler {
22  public:
23   // The router is expected to outlive this message port.
24   explicit CastSocketMessagePort(VirtualConnectionRouter* router);
25   ~CastSocketMessagePort() override;
26 
client_sender_id()27   const std::string& client_sender_id() const { return client_sender_id_; }
28 
29   void SetSocket(WeakPtr<CastSocket> socket);
30 
31   // Returns current socket identifier, or ToCastSocketId(nullptr) if not
32   // connected.
33   int GetSocketId();
34 
35   // MessagePort overrides.
36   void SetClient(MessagePort::Client* client,
37                  std::string client_sender_id) override;
38   void ResetClient() override;
39   void PostMessage(const std::string& destination_sender_id,
40                    const std::string& message_namespace,
41                    const std::string& message) override;
42 
43   // CastMessageHandler overrides.
44   void OnMessage(VirtualConnectionRouter* router,
45                  CastSocket* socket,
46                  ::cast::channel::CastMessage message) override;
47 
48  private:
49   VirtualConnectionRouter* const router_;
50   std::string client_sender_id_;
51   MessagePort::Client* client_ = nullptr;
52   WeakPtr<CastSocket> socket_;
53 };
54 
55 }  // namespace cast
56 }  // namespace openscreen
57 
58 #endif  // CAST_COMMON_CHANNEL_CAST_SOCKET_MESSAGE_PORT_H_
59