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_VIRTUAL_CONNECTION_MANAGER_H_ 6 #define CAST_COMMON_CHANNEL_VIRTUAL_CONNECTION_MANAGER_H_ 7 8 #include <cstdint> 9 #include <map> 10 #include <string> 11 12 #include "absl/types/optional.h" 13 #include "cast/common/channel/virtual_connection.h" 14 15 namespace openscreen { 16 namespace cast { 17 18 // Maintains a collection of open VirtualConnections and associated data. 19 class VirtualConnectionManager { 20 public: 21 VirtualConnectionManager(); 22 ~VirtualConnectionManager(); 23 24 void AddConnection(VirtualConnection virtual_connection, 25 VirtualConnection::AssociatedData associated_data); 26 27 // Returns true if a connection matching |virtual_connection| was found and 28 // removed. 29 bool RemoveConnection(const VirtualConnection& virtual_connection, 30 VirtualConnection::CloseReason reason); 31 32 // Returns the number of connections removed. 33 size_t RemoveConnectionsByLocalId(const std::string& local_id, 34 VirtualConnection::CloseReason reason); 35 size_t RemoveConnectionsBySocketId(int socket_id, 36 VirtualConnection::CloseReason reason); 37 38 // Returns the AssociatedData for |virtual_connection| if a connection exists, 39 // nullopt otherwise. The pointer isn't stable in the long term, so if it 40 // actually needs to be stored for later, the caller should make a copy. 41 absl::optional<const VirtualConnection::AssociatedData*> GetConnectionData( 42 const VirtualConnection& virtual_connection) const; 43 44 private: 45 // This struct simply stores the remainder of the data {VirtualConnection, 46 // VirtVirtualConnection::AssociatedData} that is not broken up into map keys 47 // for |connections_|. 48 struct VCTail { 49 std::string peer_id; 50 VirtualConnection::AssociatedData data; 51 }; 52 53 std::map<int /* socket_id */, 54 std::multimap<std::string /* local_id */, VCTail>> 55 connections_; 56 }; 57 58 } // namespace cast 59 } // namespace openscreen 60 61 #endif // CAST_COMMON_CHANNEL_VIRTUAL_CONNECTION_MANAGER_H_ 62