2025-07-06 20:43:53 +03:00
|
|
|
#include "ConnectionManager.hpp"
|
2025-10-09 21:02:19 +03:00
|
|
|
#include "Helpers/Networking/NetLibrary.hpp"
|
2025-12-17 14:46:27 +03:00
|
|
|
#include "CommandList.hpp"
|
2025-07-06 20:43:53 +03:00
|
|
|
#include <chrono>
|
2025-12-09 13:00:38 +03:00
|
|
|
#include "World.hpp"
|
2025-12-09 17:20:27 +03:00
|
|
|
#include <Helpers/Logger.hpp>
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2025-12-09 13:00:38 +03:00
|
|
|
void ConnectionManager::connect_p2p(World& initWorld, const std::string& serverFullID) {
|
2025-07-06 20:43:53 +03:00
|
|
|
client = std::make_shared<NetClient>(serverFullID);
|
|
|
|
|
lastKeepAliveTime = std::chrono::steady_clock::now();
|
|
|
|
|
NetLibrary::register_client(client);
|
2025-12-09 13:00:38 +03:00
|
|
|
initWorld.netObjMan.set_client(client, SERVER_UPDATE_NETWORK_OBJECT);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionManager::update() {
|
|
|
|
|
if(client) {
|
|
|
|
|
client->update();
|
|
|
|
|
if(std::chrono::steady_clock::now() - lastKeepAliveTime > std::chrono::seconds(2)) {
|
|
|
|
|
client->send_items_to_server(UNRELIABLE_COMMAND_CHANNEL, SERVER_KEEP_ALIVE);
|
|
|
|
|
lastKeepAliveTime = std::chrono::steady_clock::now();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionManager::client_add_recv_callback(uint32_t commandID, const NetClientRecvCallback &callback) {
|
|
|
|
|
if(client)
|
|
|
|
|
client->add_recv_callback(commandID, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConnectionManager::is_client_disconnected() {
|
|
|
|
|
return client && client->is_disconnected();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-10 10:34:12 +03:00
|
|
|
bool ConnectionManager::client_exists() {
|
|
|
|
|
return client != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetLibrary::DownloadProgress ConnectionManager::client_get_resource_retrieval_progress() {
|
|
|
|
|
return client->get_progress_into_fragmented_message(RESOURCE_COMMAND_CHANNEL);
|
2025-10-09 21:02:19 +03:00
|
|
|
}
|