cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
connection_list.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <memory>
23#include <mutex>
24#include <unordered_map>
25
26namespace msgpack_rpc::transport {
27
33template <typename Connection>
35public:
39 ConnectionList() = default;
40
46 void append(const std::shared_ptr<Connection>& connection) {
47 std::unique_lock<std::mutex> lock(mutex_);
48 list_.try_emplace(connection.get(), connection);
49 }
50
56 void remove(const std::shared_ptr<Connection>& connection) {
57 remove(connection.get());
58 }
59
65 void remove(Connection* connection) {
66 std::unique_lock<std::mutex> lock(mutex_);
67 list_.erase(connection);
68 }
69
74 std::unique_lock<std::mutex> lock(mutex_);
75 for (const auto& pair : list_) {
76 const auto connection = pair.second.lock();
77 if (connection) {
78 connection->async_close();
79 }
80 }
81 }
82
83private:
85 std::unordered_map<Connection*, std::weak_ptr<Connection>> list_{};
86
88 std::mutex mutex_{};
89};
90
91} // namespace msgpack_rpc::transport
void append(const std::shared_ptr< Connection > &connection)
Add a connection.
void remove(Connection *connection)
Remove a connection.
void async_close_all()
Asynchronously close all connections.
std::unordered_map< Connection *, std::weak_ptr< Connection > > list_
List of connections.
void remove(const std::shared_ptr< Connection > &connection)
Remove a connection.
ConnectionList()=default
Constructor.
Namespace of transport of messages.
Definition backends.h:31