aboutsummaryrefslogtreecommitdiff
path: root/cast/common/channel/virtual_connection_manager.h
blob: e8b1b7082a90626030f3a8a907df10acb985f835 (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
// 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.

#ifndef CAST_COMMON_CHANNEL_VIRTUAL_CONNECTION_MANAGER_H_
#define CAST_COMMON_CHANNEL_VIRTUAL_CONNECTION_MANAGER_H_

#include <cstdint>
#include <map>
#include <string>

#include "absl/types/optional.h"
#include "cast/common/channel/virtual_connection.h"

namespace openscreen {
namespace cast {

// Maintains a collection of open VirtualConnections and associated data.
class VirtualConnectionManager {
 public:
  VirtualConnectionManager();
  ~VirtualConnectionManager();

  void AddConnection(VirtualConnection virtual_connection,
                     VirtualConnection::AssociatedData associated_data);

  // Returns true if a connection matching |virtual_connection| was found and
  // removed.
  bool RemoveConnection(const VirtualConnection& virtual_connection,
                        VirtualConnection::CloseReason reason);

  // Returns the number of connections removed.
  size_t RemoveConnectionsByLocalId(const std::string& local_id,
                                    VirtualConnection::CloseReason reason);
  size_t RemoveConnectionsBySocketId(int32_t socket_id,
                                     VirtualConnection::CloseReason reason);

  // Returns the AssociatedData for |virtual_connection| if a connection exists,
  // nullopt otherwise.  The pointer isn't stable in the long term, so if it
  // actually needs to be stored for later, the caller should make a copy.
  absl::optional<const VirtualConnection::AssociatedData*> GetConnectionData(
      const VirtualConnection& virtual_connection) const;

 private:
  // This struct simply stores the remainder of the data {VirtualConnection,
  // VirtVirtualConnection::AssociatedData} that is not broken up into map keys
  // for |connections_|.
  struct VCTail {
    std::string peer_id;
    VirtualConnection::AssociatedData data;
  };

  std::map<int32_t /* socket_id */,
           std::multimap<std::string /* local_id */, VCTail>>
      connections_;
};

}  // namespace cast
}  // namespace openscreen

#endif  // CAST_COMMON_CHANNEL_VIRTUAL_CONNECTION_MANAGER_H_