infinipaint/include/Helpers/Networking/NetServer.hpp

141 lines
6.5 KiB
C++
Raw Normal View History

/*
* InfiniPaint
* Copyright (C) 2025-2026 Yousef Khadadeh
*
* 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 <https://www.gnu.org/licenses/>.
*/
2025-07-06 20:43:53 +03:00
#pragma once
#include <memory>
#include <string>
#include <rtc/rtc.hpp>
#include <mutex>
#include <unordered_map>
#include <cereal/archives/portable_binary.hpp>
#include <cereal/cereal.hpp>
2026-01-08 19:44:42 +03:00
#include <array>
2025-07-06 20:43:53 +03:00
#include "ByteStream.hpp"
#include "NetLibrary.hpp"
class NetServer : public std::enable_shared_from_this<NetServer> {
public:
struct ClientData;
typedef std::function<void(std::shared_ptr<ClientData>, cereal::PortableBinaryInputArchive&)> NetServerRecvCallback;
typedef std::function<void(std::shared_ptr<ClientData>)> NetServerConnectCallback;
typedef std::function<void(std::shared_ptr<ClientData>)> NetServerDisconnectCallback;
struct ClientData : public std::enable_shared_from_this<ClientData> {
2025-07-06 20:43:53 +03:00
std::unordered_map<std::string, std::queue<std::shared_ptr<std::stringstream>>> messages;
std::unordered_map<std::string, PartialFragmentMessage> pfm;
MessageOrder nextMessageOrderToSend = 0;
MessageOrder nextMessageOrderToExpect = 0;
std::unordered_map<std::string, std::shared_ptr<rtc::DataChannel>> channels;
std::mutex receivedMessagesMutex;
struct ReceivedMessage {
std::string channel;
rtc::binary data;
};
std::queue<ReceivedMessage> receivedMessages;
2025-10-02 19:10:58 +03:00
struct OutgoingMessage {
MessageOrder order;
std::shared_ptr<std::stringstream> ss;
};
std::unordered_map<std::string, std::queue<OutgoingMessage>> messageQueues;
2025-07-06 20:43:53 +03:00
std::array<uint64_t, 2> customID;
2025-07-06 20:43:53 +03:00
void send_queued_messages(NetServer& server);
void parse_received_messages(NetServer& server);
2026-06-02 21:57:53 +03:00
void parse_multi_command_id(NetServer& server, cereal::PortableBinaryInputArchive& a);
2025-07-06 20:43:53 +03:00
void send_str_as_bytes(std::shared_ptr<rtc::DataChannel> channel, const std::string& str);
NetLibrary::DownloadProgress get_progress_into_fragmented_message(const std::string& channel) const;
2025-07-06 20:43:53 +03:00
std::atomic<bool> setToDisconnect = false;
std::chrono::steady_clock::time_point lastMessageTime;
friend class NetServer;
friend class NetClient;
};
NetServer(const std::string& serverLocalID);
~NetServer();
void update();
2025-12-06 14:16:50 +03:00
template <typename... Args> void send_items_to_client(const std::shared_ptr<ClientData>& client, const std::string& channel, Args&&... items) {
send_string_stream_to_client(client, channel, get_string_stream_from_items(items...));
2025-07-06 20:43:53 +03:00
}
template <typename... Args> void send_items_to_all_clients(const std::string& channel, Args&&... items) {
2025-12-06 14:16:50 +03:00
send_string_stream_to_all_clients(channel, get_string_stream_from_items(items...));
}
template <typename... Args> void send_items_to_all_clients_except(const std::shared_ptr<ClientData>& client, const std::string& channel, Args&&... items) {
send_string_stream_to_all_clients_except(client, channel, get_string_stream_from_items(items...));
2025-07-06 20:43:53 +03:00
}
2025-12-06 14:16:50 +03:00
template <typename... Args> void send_items_to_client_if(std::function<bool(const std::shared_ptr<ClientData>&)> clientChecker, const std::string& channel, Args&&... items) {
send_string_stream_to_client_if(clientChecker, channel, get_string_stream_from_items(items...));
2025-07-06 20:43:53 +03:00
}
2025-12-06 14:16:50 +03:00
template <typename... Args> std::shared_ptr<std::stringstream> get_string_stream_from_items(Args&&... items) {
2025-12-23 14:10:33 +03:00
auto ss(std::make_shared<std::stringstream>(std::ios::binary | std::ios::out));
2025-07-06 20:43:53 +03:00
{
cereal::PortableBinaryOutputArchive m(*ss);
(m(items), ...);
}
2025-12-06 14:16:50 +03:00
return ss;
2025-07-06 20:43:53 +03:00
}
2025-12-06 14:16:50 +03:00
void send_string_stream_to_client(const std::shared_ptr<ClientData>& client, const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
void send_string_stream_to_all_clients(const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
void send_string_stream_to_all_clients_except(const std::shared_ptr<ClientData>& client, const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
void send_string_stream_to_client_if(std::function<bool(const std::shared_ptr<ClientData>&)> clientChecker, const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
2026-06-02 21:57:53 +03:00
void send_multi_command_to_all_clients(const std::string& channel, const std::function<void()>& captureSendBlock);
2025-12-06 14:16:50 +03:00
bool is_disconnected() const;
2025-07-06 20:43:53 +03:00
void add_recv_callback(MessageCommandType commandID, const NetServerRecvCallback& callback);
void add_disconnect_callback(const NetServerDisconnectCallback& callback);
2025-12-16 12:02:08 +03:00
const std::vector<std::shared_ptr<ClientData>>& get_client_list() const;
2025-07-06 20:43:53 +03:00
private:
void parse_received_messages();
void send_queued_messages();
void client_connected(std::shared_ptr<rtc::PeerConnection> connection, const std::string& clientLocalID);
std::atomic<bool> isDisconnected = false;
std::string localID;
std::vector<std::shared_ptr<ClientData>> clients;
// Adding an add list, because i dont want to block the entire clients vector with a mutex
std::mutex clientsToAddMutex;
std::vector<std::shared_ptr<ClientData>> clientsToAdd;
std::unordered_map<MessageCommandType, NetServerRecvCallback> recvCallbacks;
NetServerDisconnectCallback disconnectCallback;
NetClient* directConnectClient = nullptr;
std::shared_ptr<ClientData> directConnectClientData;
2026-06-02 21:57:53 +03:00
struct MultiCommandData {
std::string channelToSendTo;
std::vector<std::string> commands;
};
std::weak_ptr<MultiCommandData> multiCommandData;
2025-07-06 20:43:53 +03:00
friend class NetLibrary;
friend class NetClient;
};