diff --git a/include/Helpers/NetworkingObjects/NetObjManager.cpp b/include/Helpers/NetworkingObjects/NetObjManager.cpp index dcc2ad7..176282e 100644 --- a/include/Helpers/NetworkingObjects/NetObjManager.cpp +++ b/include/Helpers/NetworkingObjects/NetObjManager.cpp @@ -25,4 +25,8 @@ namespace NetworkingObjects { server = initServer; updateCommandID = initUpdateCommandID; } + void NetObjManager::disconnect() { + server = nullptr; + client = nullptr; + } } diff --git a/include/Helpers/NetworkingObjects/NetObjManager.hpp b/include/Helpers/NetworkingObjects/NetObjManager.hpp index 20dff03..4d0db06 100644 --- a/include/Helpers/NetworkingObjects/NetObjManager.hpp +++ b/include/Helpers/NetworkingObjects/NetObjManager.hpp @@ -23,6 +23,7 @@ namespace NetworkingObjects { void read_update_message(cereal::PortableBinaryInputArchive& a, const std::shared_ptr& clientReceivedFrom); void set_client(std::shared_ptr initClient, MessageCommandType initUpdateCommandID); void set_server(std::shared_ptr initServer, MessageCommandType initUpdateCommandID); + void disconnect(); bool is_server() const; template NetObjOwnerPtr read_create_message(cereal::PortableBinaryInputArchive& a, const std::shared_ptr& clientReceivedFrom) { NetObjID id; diff --git a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp index 4ec9440..7a98f9c 100644 --- a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp @@ -21,7 +21,10 @@ namespace NetworkingObjects { std::add_lvalue_reference_t operator*() const requires(!std::is_void_v); T* operator->() const; operator bool() const; - std::strong_ordering operator<=>(const NetObjOwnerPtr& rhs) const; + bool operator!=(const NetObjOwnerPtr& rhs) const; + bool operator==(const NetObjOwnerPtr& rhs) const; + bool operator!=(const NetObjTemporaryPtr& rhs) const; + bool operator==(const NetObjTemporaryPtr& rhs) const; void send_client_update(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const; void send_server_update_to_client(const std::shared_ptr& clientToSendTo, const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const; void send_server_update_to_all_clients_except(const std::shared_ptr& clientToNotSendTo, const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const; diff --git a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp index 1a0b362..25799e1 100644 --- a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp @@ -64,8 +64,20 @@ namespace NetworkingObjects { return rawPtr; } - template std::strong_ordering NetObjOwnerPtr::operator<=>(const NetObjOwnerPtr& rhs) const { - return rawPtr <=> rhs.rawPtr; + template bool NetObjOwnerPtr::operator!=(const NetObjOwnerPtr& rhs) const { + return rawPtr != rhs.rawPtr; + } + + template bool NetObjOwnerPtr::operator==(const NetObjOwnerPtr& rhs) const { + return rawPtr == rhs.rawPtr; + } + + template bool NetObjOwnerPtr::operator!=(const NetObjTemporaryPtr& rhs) const { + return rawPtr != rhs.get(); + } + + template bool NetObjOwnerPtr::operator==(const NetObjTemporaryPtr& rhs) const { + return rawPtr == rhs.get(); } template void NetObjOwnerPtr::send_client_update(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { diff --git a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp index c7f44db..cca5950 100644 --- a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp @@ -20,7 +20,10 @@ namespace NetworkingObjects { std::add_lvalue_reference_t operator*() const requires(!std::is_void_v); T* operator->() const; operator bool() const; - std::strong_ordering operator<=>(const NetObjTemporaryPtr& rhs) const; + bool operator!=(const NetObjTemporaryPtr& rhs) const; + bool operator==(const NetObjTemporaryPtr& rhs) const; + bool operator!=(const NetObjOwnerPtr& rhs) const; + bool operator==(const NetObjOwnerPtr& rhs) const; void send_client_update(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const; void send_server_update_to_client(const std::shared_ptr& clientToSendTo, const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const; void send_server_update_to_all_clients_except(const std::shared_ptr& clientToNotSendTo, const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const; diff --git a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp index e913cb1..3257b13 100644 --- a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp @@ -57,8 +57,20 @@ namespace NetworkingObjects { return rawPtr; } - template std::strong_ordering NetObjTemporaryPtr::operator<=>(const NetObjTemporaryPtr& rhs) const { - return rawPtr <=> rhs.rawPtr; + template bool NetObjTemporaryPtr::operator!=(const NetObjTemporaryPtr& rhs) const { + return rawPtr != rhs.rawPtr; + } + + template bool NetObjTemporaryPtr::operator==(const NetObjTemporaryPtr& rhs) const { + return rawPtr == rhs.rawPtr; + } + + template bool NetObjTemporaryPtr::operator!=(const NetObjOwnerPtr& rhs) const { + return rawPtr != rhs.get(); + } + + template bool NetObjTemporaryPtr::operator==(const NetObjOwnerPtr& rhs) const { + return rawPtr == rhs.get(); } template void NetObjTemporaryPtr::send_client_update(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { diff --git a/src/ClientData.cpp b/src/ClientData.cpp index 1faea93..18b118c 100644 --- a/src/ClientData.cpp +++ b/src/ClientData.cpp @@ -80,7 +80,7 @@ void ClientData::register_class(World& world) { std::string chatMessage; a(chatMessage); world.add_chat_message(o->displayName, chatMessage, Toolbar::ChatMessage::Type::NORMAL); - o.send_server_update_to_all_clients_except(c, RELIABLE_COMMAND_CHANNEL, [&chatMessage](const NetObjTemporaryPtr& o, cereal::PortableBinaryOutputArchive & a) { + o.send_server_update_to_all_clients(RELIABLE_COMMAND_CHANNEL, [&chatMessage](const NetObjTemporaryPtr& o, cereal::PortableBinaryOutputArchive & a) { a(ClientDataCommand::SEND_CHAT_MESSAGE, chatMessage); }); break; @@ -116,7 +116,8 @@ void ClientData::set_camera_coords(const NetworkingObjects::NetObjTemporaryPtr& o, World& world, const std::string& chatMessage) { - world.add_chat_message(o->displayName, chatMessage, Toolbar::ChatMessage::Type::NORMAL); + if(o.get_obj_man()->is_server()) // Clients will receive the message when it returns from the server to ensure correct order + world.add_chat_message(o->displayName, chatMessage, Toolbar::ChatMessage::Type::NORMAL); o.send_update_to_all(RELIABLE_COMMAND_CHANNEL, [&chatMessage](const NetObjTemporaryPtr& o, cereal::PortableBinaryOutputArchive & a) { a(ClientDataCommand::SEND_CHAT_MESSAGE, chatMessage); }); diff --git a/src/World.cpp b/src/World.cpp index a5c15fa..d4170d2 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -125,7 +125,6 @@ void World::init_net_obj_type_list() { void World::init_server_callbacks() { rMan.init_server_callbacks(); netServer->add_recv_callback(SERVER_INITIAL_DATA, [&](std::shared_ptr client, cereal::PortableBinaryInputArchive& message) { - std::cout << "Received server init data" << std::endl; ClientData::InitStruct newClientData; newClientData.cursorColor = get_random_cursor_color(); message(newClientData.displayName); @@ -167,7 +166,6 @@ void World::init_server_callbacks() { void World::init_client_callbacks() { rMan.init_client_callbacks(); con.client_add_recv_callback(CLIENT_INITIAL_DATA, [&](cereal::PortableBinaryInputArchive& message) { - std::cout << "Received client init data" << std::endl; std::string fileDisplayName; NetworkingObjects::NetObjID clientDataObjID; message(fileDisplayName, clientDataObjID); @@ -176,10 +174,9 @@ void World::init_client_callbacks() { gridMan.grids = netObjMan.read_create_message>(message, nullptr); drawProg.read_components_client(message); canvasTheme.read_create_message(message); - clients = netObjMan.read_create_message>(message, nullptr); init_client_data_list_callbacks(); - ownClientData = netObjMan.get_obj_from_id(clientDataObjID); + ownClientData = netObjMan.get_obj_temporary_ref_from_id(clientDataObjID); drawData.cam.smooth_move_to(*main.world, ownClientData->get_cam_coords(), ownClientData->get_window_size(), true); clientStillConnecting = false; @@ -194,17 +191,28 @@ void World::init_client_callbacks() { void World::focus_update() { con.update(); - if(netServer && netServer->is_disconnected()) { - Logger::get().log("USERINFO", "Host connection failed"); - netSource.clear(); - netServer = nullptr; - conType = CONNECTIONTYPE_LOCAL; - con = ConnectionManager(); + if(netServer) { + if(netServer->is_disconnected()) { + Logger::get().log("USERINFO", "Host connection failed"); + netSource.clear(); + netServer = nullptr; + conType = CONNECTIONTYPE_LOCAL; + con = ConnectionManager(); + netObjMan.disconnect(); + } + else { + netServer->update(); + if(std::chrono::steady_clock::now() - lastKeepAliveSent > std::chrono::seconds(2)) { + netServer->send_items_to_all_clients(UNRELIABLE_COMMAND_CHANNEL, CLIENT_KEEP_ALIVE); + lastKeepAliveSent = std::chrono::steady_clock::now(); + } + } } if(con.is_client_disconnected()) { Logger::get().log("USERINFO", "Client connection failed"); setToDestroy = true; + netObjMan.disconnect(); return; }