infinipaint/include/Helpers/Networking/NetClient.hpp

106 lines
3.9 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 <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;
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
send_string_stream_to_server(channel, ss);
2025-07-06 20:43:53 +03:00
}
2025-12-06 14:16:50 +03:00
void send_string_stream_to_server(const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
2026-06-02 21:57:53 +03:00
void send_multi_command_to_server(const std::string& channel, const std::function<void()>& captureSendBlock);
2025-07-06 20:43:53 +03:00
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;
2025-12-09 13:00:38 +03:00
~NetClient();
2025-07-06 20:43:53 +03:00
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);
2026-06-02 21:57:53 +03:00
void parse_multi_command_id(cereal::PortableBinaryInputArchive& inArchive);
2025-07-06 20:43:53 +03:00
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
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;
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
std::chrono::steady_clock::time_point lastMessageTime;
friend class NetLibrary;
friend class NetServer;
};