#pragma once #include #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 "main00005" // NOTE: Change this value whenever netcode is incompatible with the previous version typedef uint8_t MessageCommandType; typedef uint8_t NetTypeIDType; class NetLibrary { public: static void init(const std::filesystem::path& p2pConfigPath); static void copy_default_p2p_config_to_path(const std::filesystem::path& newP2PConfigPath); 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 = 10; static constexpr size_t GLOBALID_LEN = 40; struct DownloadProgress { uint64_t downloadedBytes; uint64_t totalBytes; }; private: static constexpr size_t FRAGMENT_MESSAGE_STRIDE = 512; static constexpr size_t MAX_UNRELIABLE_MESSAGE_SIZE = 4096; static constexpr size_t MAX_BUFFERED_DATA_PER_CHANNEL = 64000; static constexpr std::chrono::seconds TIMEOUT_DURATION = std::chrono::seconds(30); enum class LoadP2PSettingsInPathResult { SUCCESS, FAILED_TO_OPEN, FAILED_TO_READ }; static LoadP2PSettingsInPathResult load_p2p_settings_in_path(const std::filesystem::path& p2pConfigPath); static bool is_ordered_channel(std::string_view channelName); static MessageOrder calc_order_for_queued_message(std::string_view channelName, MessageOrder& mOrder); 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(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; };