Files

249 lines
6.4 KiB
C++
Raw Permalink Normal View History

2016-08-16 16:51:22 +02:00
/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
2025-02-11 16:46:21 +01:00
* Copyright (C) 2016, 2019-2025 Tom Zander <tom@flowee.org>
2016-08-16 16:51:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "NetworkConnection.h"
#include "NetworkManager.h"
#include "NetworkManager_p.h"
2019-08-24 13:20:37 +02:00
#include <Message.h>
2019-04-06 11:59:49 +02:00
2016-08-16 16:51:22 +02:00
NetworkConnection::NetworkConnection()
: m_id(-1),
m_callbacksId(-1)
{
}
NetworkConnection::NetworkConnection(NetworkManager *parent, int id)
: m_id(id),
m_callbacksId(-1)
{
auto priv = parent->priv().lock();
2022-01-26 15:27:30 +01:00
if (priv) {
auto iter = priv->connections.find(id);
if (iter != priv->connections.end())
m_parent = iter->second;
}
2016-08-16 16:51:22 +02:00
}
NetworkConnection::NetworkConnection(std::shared_ptr<NetworkManagerConnection> &parent, int id)
: m_parent(parent),
m_id(id),
m_callbacksId(-1)
{
}
NetworkConnection::NetworkConnection(NetworkConnection &&other)
: m_parent(std::move(other.m_parent)),
m_id(other.m_id),
m_callbacksId(other.m_callbacksId)
{
other.m_id = -1;
other.m_parent.reset();
}
NetworkConnection& NetworkConnection::operator=(NetworkConnection && other)
{
if (other.m_id != m_id && m_callbacksId != -1)
clear();
m_parent = std::move(other.m_parent);
other.m_parent.reset();
m_id = other.m_id;
other.m_id = -1;
m_callbacksId = other.m_callbacksId;
2021-02-22 15:52:34 +01:00
other.m_callbacksId = -1;
2016-08-16 16:51:22 +02:00
return *this;
}
2019-04-06 11:59:49 +02:00
void NetworkConnection::dummy() const
{
// intentionally empty
}
2019-06-03 21:42:37 +02:00
void NetworkConnection::clear()
2016-08-16 16:51:22 +02:00
{
auto d = m_parent.lock();
if (d) {
2025-02-11 16:46:21 +01:00
if (m_callbacksId >= 0) {
d->m_strand.post(std::bind(&NetworkManagerConnection::removeAllCallbacksFor, d, m_callbacksId), std::allocator<void>());
}
2016-08-16 16:51:22 +02:00
m_callbacksId = -1;
m_parent.reset();
}
}
2016-08-16 16:51:22 +02:00
NetworkConnection::~NetworkConnection()
{
clear();
2016-08-16 16:51:22 +02:00
}
2016-08-16 16:51:22 +02:00
bool NetworkConnection::isValid() const
{
if (m_id <= 0) // default constructor called
return false;
auto d = m_parent.lock();
return d != nullptr;
}
bool NetworkConnection::isConnected() const
{
auto d = m_parent.lock();
if (d)
return d->isConnected();
return false;
}
EndPoint NetworkConnection::endPoint() const
{
auto d = m_parent.lock();
2019-08-29 22:33:04 +02:00
if (d)
2016-08-16 16:51:22 +02:00
return d->endPoint();
return EndPoint();
}
void NetworkConnection::accept(AcceptLimit cl)
2016-08-16 16:51:22 +02:00
{
auto d = m_parent.lock();
if (d && d->isConnected())
d->accept(cl);
2016-08-16 16:51:22 +02:00
}
void NetworkConnection::connect()
{
auto d = m_parent.lock();
if (d.get())
d->connect();
}
void NetworkConnection::disconnect()
{
auto d = m_parent.lock();
if (d) {
if (d->m_strand.running_in_this_thread())
d->disconnect();
else
2025-02-11 16:46:21 +01:00
d->m_strand.post(std::bind(&NetworkManagerConnection::disconnect, d), std::allocator<void>());
2016-08-16 16:51:22 +02:00
}
}
2020-04-01 20:21:02 +02:00
void NetworkConnection::shutdown()
{
auto d = m_parent.lock();
2020-05-10 00:46:41 +02:00
if (d)
2025-02-11 16:46:21 +01:00
d->m_strand.post(std::bind(&NetworkManagerConnection::recycleConnection, d), std::allocator<void>());
2020-04-01 20:21:02 +02:00
}
2016-08-16 16:51:22 +02:00
void NetworkConnection::send(const Message &message, MessagePriority priority)
{
auto d = m_parent.lock();
if (d)
d->queueMessage(message, priority);
}
void NetworkConnection::setOnConnected(const std::function<void(const EndPoint&)> &callback)
{
auto d = m_parent.lock();
if (d) {
if (m_callbacksId < 0)
m_callbacksId = d->nextCallbackId();
2025-02-11 16:46:21 +01:00
d->m_strand.dispatch(std::bind(&NetworkManagerConnection::addOnConnectedCallback, d, m_callbacksId, callback), std::allocator<void>());
2016-08-16 16:51:22 +02:00
}
}
void NetworkConnection::setOnDisconnected(const std::function<void(const EndPoint&)> &callback)
{
auto d = m_parent.lock();
if (d) {
if (m_callbacksId < 0)
m_callbacksId = d->nextCallbackId();
2025-02-11 16:46:21 +01:00
d->m_strand.dispatch(std::bind(&NetworkManagerConnection::addOnDisconnectedCallback, d, m_callbacksId, callback), std::allocator<void>());
2016-08-16 16:51:22 +02:00
}
}
void NetworkConnection::setOnIncomingMessage(const std::function<void(const Message&)> &callback)
{
auto d = m_parent.lock();
if (d) {
if (m_callbacksId < 0)
m_callbacksId = d->nextCallbackId();
2025-02-11 16:46:21 +01:00
d->m_strand.dispatch(std::bind(&NetworkManagerConnection::addOnIncomingMessageCallback, d, m_callbacksId, callback), std::allocator<void>());
2016-08-16 16:51:22 +02:00
}
}
2020-04-01 20:21:02 +02:00
void NetworkConnection::setOnError(const std::function<void (int, const boost::system::error_code &)> &callback)
{
auto d = m_parent.lock();
if (d) {
if (m_callbacksId < 0)
m_callbacksId = d->nextCallbackId();
2025-02-11 16:46:21 +01:00
d->m_strand.dispatch(std::bind(&NetworkManagerConnection::addOnError, d, m_callbacksId, callback), std::allocator<void>());
2020-04-01 20:21:02 +02:00
}
}
2020-04-01 20:21:02 +02:00
void NetworkConnection::setLoginMessageCreator(const std::function<Message ()> &creator)
{
auto d = m_parent.lock();
if (d) {
2025-02-11 16:46:21 +01:00
d->m_strand.dispatch(std::bind(&NetworkManagerConnection::setLoginCreator, d, creator), std::allocator<void>());
}
2020-04-01 20:21:02 +02:00
}
2016-08-16 16:51:22 +02:00
void NetworkConnection::punishPeer(int punishment)
{
auto d = m_parent.lock();
if (d.get()) {
d->punish(punishment);
}
}
void NetworkConnection::postOnStrand(const std::function<void()> &task)
{
auto d = m_parent.lock();
if (d)
2020-05-03 21:01:57 +02:00
d->runOnStrand(task);
2016-08-16 16:51:22 +02:00
}
void NetworkConnection::setMessageHeaderLegacy(bool on)
{
auto d = m_parent.lock();
if (d)
2020-04-01 20:21:02 +02:00
d->setMessageHeaderType(on ? NetworkManagerConnection::LegacyP2P : NetworkManagerConnection::FloweeNative);
}
2020-05-09 19:58:44 +02:00
void NetworkConnection::setMessageQueueSizes(int main, int priority)
{
2021-08-05 22:02:45 +02:00
assert(main >= 10);
assert(priority >= 3);
2020-05-09 19:58:44 +02:00
auto d = m_parent.lock();
if (d)
d->setMessageQueueSizes(main, priority);
2020-05-09 19:58:44 +02:00
}
2024-08-21 16:53:34 +02:00
void NetworkConnection::setCertificate(const Streaming::ConstBuffer &buffer)
{
assert(!buffer.isEmpty());
auto d = m_parent.lock();
if (d) {
if (!d->endPoint().encrypted)
throw std::runtime_error("Mismatch, not encrypted connection");
d->setCertificate(buffer);
}
}