cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
26
namespace
msgpack_rpc::transport
{
27
33
template
<
typename
Connection>
34
class
ConnectionList
{
35
public
:
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
73
void
async_close_all
() {
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
83
private
:
85
std::unordered_map<Connection*, std::weak_ptr<Connection>>
list_
{};
86
88
std::mutex
mutex_
{};
89
};
90
91
}
// namespace msgpack_rpc::transport
msgpack_rpc::transport::ConnectionList::append
void append(const std::shared_ptr< Connection > &connection)
Add a connection.
Definition
connection_list.h:46
msgpack_rpc::transport::ConnectionList::mutex_
std::mutex mutex_
Mutex.
Definition
connection_list.h:88
msgpack_rpc::transport::ConnectionList::remove
void remove(Connection *connection)
Remove a connection.
Definition
connection_list.h:65
msgpack_rpc::transport::ConnectionList::async_close_all
void async_close_all()
Asynchronously close all connections.
Definition
connection_list.h:73
msgpack_rpc::transport::ConnectionList::list_
std::unordered_map< Connection *, std::weak_ptr< Connection > > list_
List of connections.
Definition
connection_list.h:85
msgpack_rpc::transport::ConnectionList::remove
void remove(const std::shared_ptr< Connection > &connection)
Remove a connection.
Definition
connection_list.h:56
msgpack_rpc::transport::ConnectionList::ConnectionList
ConnectionList()=default
Constructor.
msgpack_rpc::transport::Connection
Class of connections.
Definition
connection.h:63
msgpack_rpc::transport
Namespace of transport of messages.
Definition
backends.h:31
src
msgpack_rpc
transport
connection_list.h
Generated on
for cpp-msgpack-rpc by
1.18.0