#pragma once #include #include #include #include typedef uint64_t MessageOrder; class NetClient; class NetServer; template std::weak_ptr make_weak_ptr(std::shared_ptr ptr) { return ptr; } #define RELIABLE_COMMAND_CHANNEL "reliableCommand" #define UNRELIABLE_COMMAND_CHANNEL "unreliableCommand" #define RESOURCE_COMMAND_CHANNEL "resourceCommand" #define MAIN_NETLIBRARY_CHANNEL "main00001" typedef uint8_t MessageCommandType; class NetLibrary { public: static void init(); static void update(); static std::string get_random_server_local_id(); static const std::string& get_global_id(); static void register_server(std::shared_ptr server); static void register_client(std::shared_ptr client); static void destroy(); static constexpr size_t LOCALID_LEN = 5; static constexpr size_t GLOBALID_LEN = 20; private: static constexpr size_t FRAGMENT_MESSAGE_STRIDE = 4096; static constexpr size_t MAX_BUFFERED_DATA_PER_CHANNEL = 65500; static constexpr std::chrono::seconds TIMEOUT_DURATION = std::chrono::seconds(15); static void finish_client_registration(std::shared_ptr client); static void assign_client_connection_to_server(const std::string& serverLocalID, const std::string& clientLocalID, std::shared_ptr connection); static void channel_created_in_client(std::shared_ptr channel); static std::string attach_message_order_and_update(MessageOrder& order, const std::string& message); static MessageOrder get_message_order(rtc::binary& message); struct PeerData { std::shared_ptr connection; std::shared_ptr channel; std::queue messageQueue; }; static void setup_main_data_channel(std::shared_ptr peer, const std::string& id); static std::shared_ptr setup_peer_connection(const rtc::Configuration& config, std::weak_ptr wws, std::string id); static std::atomic alreadyInitialized; static std::string signalingAddr; static std::shared_ptr ws; static std::unordered_map> peers; static std::string globalID; static rtc::Configuration config; static std::mutex clientListMutex; static std::mutex serverListMutex; static std::mutex peerListMutex; static std::vector> clients; static std::vector> servers; friend class NetClient; friend class NetServer; static std::promise wsPromise; };