19#include <dhtnet/channel_utils.h>
22using namespace std::literals;
28using Key = std::pair<std::string, DeviceId>;
36 std::map<std::string, std::map<DeviceId, std::vector<std::shared_ptr<dhtnet::ChannelSocket>>>>
connections_;
45 const std::string& peerId,
58 std::unique_lock
lk(pimpl_->connectionsMtx_);
59 for (
const auto& [peerId,
_] : pimpl_->connections_) {
60 pimpl_->onPeerStateChanged_(peerId,
false);
63 pimpl_->connections_.clear();
75 if (pimpl_->connectionManager_.isConnecting(deviceId,
channelName)) {
76 JAMI_LOG(
"Already connecting to {}", deviceId);
79 pimpl_->connectionManager_
85 const std::string& peerId,
91 JAMI_WARNING(
"onChannelShutdown: No connections found for peer {}", peerId);
96 JAMI_WARNING(
"onChannelShutdown: No connections found for device {} of peer {}", device.toString(), peerId);
106 if (
peerIt->second.empty()) {
112std::shared_ptr<dhtnet::ChannelSocket>
115 std::lock_guard
lk(pimpl_->connectionsMtx_);
116 auto it = pimpl_->connections_.find(peer);
117 if (
it == pimpl_->connections_.end())
127std::vector<std::shared_ptr<dhtnet::ChannelSocket>>
130 std::vector<std::shared_ptr<dhtnet::ChannelSocket>>
sockets;
131 std::lock_guard
lk(pimpl_->connectionsMtx_);
132 auto it = pimpl_->connections_.find(peer);
133 if (
it == pimpl_->connections_.end())
136 for (
auto& [deviceId, channels] :
it->second) {
137 for (
auto& channel : channels) {
155 std::shared_ptr<dhtnet::ChannelSocket> socket)
159 auto peerId =
cert->issuer->getId().toString();
160 auto device =
cert->getLongId();
161 std::lock_guard
lk(pimpl_->connectionsMtx_);
167 pimpl_->onPeerStateChanged_(peerId,
true);
170 onMessage(cert, msg.t, msg.c);
171 return std::error_code();
175 [w = pimpl_->weak_from_this(), peerId, device, s = std::weak_ptr(socket)](
const std::error_code& ) {
176 if (auto shared = w.lock())
177 shared->onChannelShutdown(s.lock(), peerId, device);
184 const std::shared_ptr<dhtnet::ChannelSocket>&
conn)
188 std::unique_lock
lk(pimpl_->connectionsMtx_);
189 auto it = pimpl_->connections_.find(peer);
190 if (
it != pimpl_->connections_.end()) {
194 channels.erase(std::remove(channels.begin(), channels.end(),
conn), channels.end());
195 if (channels.empty()) {
197 if (
it->second.empty()) {
198 pimpl_->connections_.erase(
it);
199 pimpl_->onPeerStateChanged_(peer,
false);
214 msgpack::pack(
buffer, message);
A Channel handler is used to make the link between JamiAccount and ConnectionManager Its role is to m...
static bool sendMessage(const std::shared_ptr< dhtnet::ChannelSocket > &, const Message &message)
std::vector< std::shared_ptr< dhtnet::ChannelSocket > > getChannels(const std::string &peer) const
void closeChannel(const std::string &peer, const DeviceId &device, const std::shared_ptr< dhtnet::ChannelSocket > &conn)
std::shared_ptr< dhtnet::ChannelSocket > getChannel(const std::string &peer, const DeviceId &deviceId) const
std::function< void(const std::string &, bool)> OnPeerStateChanged
std::function< void(const std::shared_ptr< dht::crypto::Certificate > &, std::string &, const std::string &)> OnMessage
bool onRequest(const std::shared_ptr< dht::crypto::Certificate > &peer, const std::string &name) override
Determine if we accept or not the message request.
MessageChannelHandler(dhtnet::ConnectionManager &cm, OnMessage onMessage, OnPeerStateChanged onPeer)
void onReady(const std::shared_ptr< dht::crypto::Certificate > &peer, const std::string &name, std::shared_ptr< dhtnet::ChannelSocket > channel) override
Launch message process.
void connect(const DeviceId &deviceId, const std::string &, ConnectCb &&cb, const std::string &connectionType, bool forceNewConnection=false) override
Ask for a new message channel.
#define JAMI_WARNING(formatstr,...)
#define JAMI_LOG(formatstr,...)
static constexpr auto MESSAGE_SCHEME
std::function< void(std::shared_ptr< dhtnet::ChannelSocket >, const DeviceId &)> ConnectCb
void emitSignal(Args... args)
std::pair< std::string, DeviceId > Key
std::string concat(Args &&... args)
std::map< std::string, std::map< DeviceId, std::vector< std::shared_ptr< dhtnet::ChannelSocket > > > > connections_
void onChannelShutdown(const std::shared_ptr< dhtnet::ChannelSocket > &socket, const std::string &peerId, const DeviceId &device)
OnPeerStateChanged onPeerStateChanged_
Impl(dhtnet::ConnectionManager &cm, OnMessage onMessage, OnPeerStateChanged onPeer)
std::recursive_mutex connectionsMtx_
dhtnet::ConnectionManager & connectionManager_