infinipaint/include/Helpers/Networking/NetClient.hpp
2026-06-02 21:57:53 +03:00

105 lines
3.9 KiB
C++

/*
* 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/>.
*/
#pragma once
#include <memory>
#include <string>
#include <rtc/rtc.hpp>
#include <cereal/cereal.hpp>
#include <unordered_map>
#include <functional>
#include <cereal/cereal.hpp>
#include <cereal/archives/portable_binary.hpp>
#include <queue>
#include "ByteStream.hpp"
#include "NetLibrary.hpp"
typedef std::function<void(cereal::PortableBinaryInputArchive&)> NetClientRecvCallback;
class NetClient : public std::enable_shared_from_this<NetClient> {
public:
NetClient(const std::string& serverFullID);
NetClient(NetServer& server);
template <typename... Args> void send_items_to_server(const std::string& channel, Args&&... items) {
if(isDisconnected)
return;
auto ss(std::make_shared<std::stringstream>(std::ios::binary | std::ios::out));
{
cereal::PortableBinaryOutputArchive m(*ss);
(m(items), ...);
}
send_string_stream_to_server(channel, ss);
}
void send_string_stream_to_server(const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
void send_multi_command_to_server(const std::string& channel, const std::function<void()>& captureSendBlock);
void update();
void add_recv_callback(MessageCommandType commandID, const NetClientRecvCallback& callback);
bool is_disconnected() const;
NetLibrary::DownloadProgress get_progress_into_fragmented_message(const std::string& channel) const;
~NetClient();
private:
std::string localID;
void init_channel(const std::string& channelName, std::shared_ptr<rtc::DataChannel> channel);
void parse_received_messages();
void send_queued_messages();
void send_str_as_bytes(std::shared_ptr<rtc::DataChannel> channel, const std::string& str);
void parse_multi_command_id(cereal::PortableBinaryInputArchive& inArchive);
struct OutgoingMessage {
MessageOrder order;
std::shared_ptr<std::stringstream> ss;
};
std::unordered_map<std::string, std::queue<OutgoingMessage>> messageQueues;
struct ReceiveQueue {
std::mutex mut;
struct ReceivedMessage {
std::string channel;
rtc::binary data;
};
std::queue<ReceivedMessage> messages;
};
std::shared_ptr<ReceiveQueue> receiveQueue;
std::unordered_map<std::string, std::shared_ptr<rtc::DataChannel>> channels;
std::unordered_map<std::string, PartialFragmentMessage> pfm;
std::unordered_map<MessageCommandType, NetClientRecvCallback> recvCallbacks;
NetServer* directConnectServer = nullptr;
std::atomic<bool> isDisconnected = false;
MessageOrder nextMessageOrderToSend = 0;
MessageOrder nextMessageOrderToExpect = 0;
std::string serverFullID;
bool fullyRegistered = false;
struct MultiCommandData {
std::string channelToSendTo;
std::vector<std::string> commands;
};
std::weak_ptr<MultiCommandData> multiCommandData;
std::chrono::steady_clock::time_point lastMessageTime;
friend class NetLibrary;
friend class NetServer;
};