• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CONNECTION_NAMESPACE_HANDLER_H_
6 #define CAST_COMMON_CHANNEL_CONNECTION_NAMESPACE_HANDLER_H_
7 
8 #include <functional>
9 #include <vector>
10 
11 #include "cast/common/channel/cast_message_handler.h"
12 #include "cast/common/channel/proto/cast_channel.pb.h"
13 #include "cast/common/channel/virtual_connection.h"
14 #include "util/json/json_serialization.h"
15 
16 namespace openscreen {
17 namespace cast {
18 
19 class VirtualConnectionRouter;
20 
21 // Handles CastMessages in the connection namespace by opening and closing
22 // VirtualConnections on the socket on which the messages were received.
23 //
24 // This is meant to be used in either/both the initiator or responder role:
25 //
26 // 1. Initiators call Open/CloseRemoteConnection() to establish/close a virtual
27 // connection with a remote peer. Internally, OpenRemoteConnection() sends a
28 // CONNECT request to the remote peer, and the remote peer is expected to
29 // respond with a either a CONNECTED response or a CLOSE response.
30 //
31 // 2. Responders simply handle CONNECT or CLOSE requests, allowing or
32 // disallowing connections based on the VirtualConnectionPolicy, and
33 // ConnectionNamespaceHandler will report new/closed connections to the local
34 // VirtualConnectionRouter to enable/disable message routing.
35 class ConnectionNamespaceHandler : public CastMessageHandler {
36  public:
37   class VirtualConnectionPolicy {
38    public:
39     virtual ~VirtualConnectionPolicy() = default;
40 
41     virtual bool IsConnectionAllowed(
42         const VirtualConnection& virtual_conn) const = 0;
43   };
44 
45   using RemoteConnectionResultCallback = std::function<void(bool)>;
46 
47   // Both |vc_router| and |vc_policy| should outlive this object.
48   ConnectionNamespaceHandler(VirtualConnectionRouter* vc_router,
49                              VirtualConnectionPolicy* vc_policy);
50   ~ConnectionNamespaceHandler() override;
51 
52   // Requests a virtual connection be established. The |result_callback| is
53   // later invoked with true/false to indicate success, based on a response from
54   // the remote.
55   void OpenRemoteConnection(VirtualConnection conn,
56                             RemoteConnectionResultCallback result_callback);
57 
58   // Closes the virtual connection, notifying the remote by sending it a CLOSE
59   // message.
60   void CloseRemoteConnection(VirtualConnection conn);
61 
62   // CastMessageHandler overrides.
63   void OnMessage(VirtualConnectionRouter* router,
64                  CastSocket* socket,
65                  ::cast::channel::CastMessage message) override;
66 
67  private:
68   void HandleConnect(CastSocket* socket,
69                      ::cast::channel::CastMessage message,
70                      Json::Value parsed_message);
71   void HandleClose(CastSocket* socket,
72                    ::cast::channel::CastMessage message,
73                    Json::Value parsed_message);
74   void HandleConnectedResponse(CastSocket* socket,
75                                ::cast::channel::CastMessage message,
76                                Json::Value parsed_message);
77 
78   void SendConnect(VirtualConnection virtual_conn);
79   void SendClose(VirtualConnection virtual_conn);
80   void SendConnectedResponse(const VirtualConnection& virtual_conn,
81                              int max_protocol_version);
82 
83   bool RemoveConnection(const VirtualConnection& conn,
84                         VirtualConnection::CloseReason reason);
85 
86   VirtualConnectionRouter* const vc_router_;
87   VirtualConnectionPolicy* const vc_policy_;
88 
89   struct PendingRequest {
90     VirtualConnection conn;
91     RemoteConnectionResultCallback result_callback;
92   };
93   std::vector<PendingRequest> pending_remote_requests_;
94 };
95 
96 }  // namespace cast
97 }  // namespace openscreen
98 
99 #endif  // CAST_COMMON_CHANNEL_CONNECTION_NAMESPACE_HANDLER_H_
100