Added sync code to client ordered list

This commit is contained in:
Yousef Khadadeh 2025-12-09 13:00:38 +03:00
parent c51b7e7eb1
commit 9649135f43
16 changed files with 233 additions and 143 deletions

View file

@ -2,6 +2,7 @@
#include "Helpers/Networking/NetLibrary.hpp"
#include "Server/CommandList.hpp"
#include <chrono>
#include "World.hpp"
void ConnectionManager::init_local_p2p(World& initWorld, const std::string& serverLocalID) {
localServer = std::make_unique<MainServer>(initWorld, serverLocalID);
@ -9,10 +10,11 @@ void ConnectionManager::init_local_p2p(World& initWorld, const std::string& serv
lastKeepAliveTime = std::chrono::steady_clock::now();
}
void ConnectionManager::connect_p2p(const std::string& serverFullID) {
void ConnectionManager::connect_p2p(World& initWorld, const std::string& serverFullID) {
client = std::make_shared<NetClient>(serverFullID);
lastKeepAliveTime = std::chrono::steady_clock::now();
NetLibrary::register_client(client);
initWorld.netObjMan.set_client(client, SERVER_UPDATE_NETWORK_OBJECT);
}
void ConnectionManager::update() {

View file

@ -5,7 +5,7 @@
class ConnectionManager {
public:
void init_local_p2p(World& initWorld, const std::string& serverLocalID);
void connect_p2p(const std::string& serverFullID);
void connect_p2p(World& initWorld, const std::string& serverFullID);
void update();
void client_add_recv_callback(uint32_t commandID, const NetClientRecvCallback &callback);
template <typename... Args> void client_send_items_to_server(const std::string& channel, Args&&... items) {

View file

@ -6,6 +6,7 @@ enum ServerCommands : MessageCommandType {
SERVER_FRAGMENT = 0,
SERVER_KEEP_ALIVE,
SERVER_INITIAL_DATA,
SERVER_UPDATE_NETWORK_OBJECT,
SERVER_MOVE_CAMERA,
SERVER_MOVE_SCREEN_MOUSE,
SERVER_PLACE_SINGLE_COMPONENT,
@ -29,6 +30,7 @@ enum ClientCommands : MessageCommandType {
CLIENT_FRAGMENT = 0,
CLIENT_KEEP_ALIVE,
CLIENT_INITIAL_DATA,
CLIENT_UPDATE_NETWORK_OBJECT,
CLIENT_USER_CONNECT,
CLIENT_USER_DISCONNECT,
CLIENT_PLACE_SINGLE_COMPONENT,

View file

@ -40,6 +40,8 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
netServer = std::make_shared<NetServer>(serverLocalID);
NetLibrary::register_server(netServer);
world.netObjMan.set_server(netServer, CLIENT_UPDATE_NETWORK_OBJECT);
netServer->add_recv_callback(SERVER_INITIAL_DATA, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
bool isDirectConnect;
@ -83,6 +85,9 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
}
clients.emplace(newClient.serverID, newClient);
});
netServer->add_recv_callback(SERVER_UPDATE_NETWORK_OBJECT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
world.netObjMan.read_update_message(message, client);
});
netServer->add_recv_callback(SERVER_MOVE_CAMERA, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
auto& c = clients[client->customID];
message(c.camCoords, c.windowSize);

View file

@ -27,12 +27,12 @@
#endif
World::World(MainProgram& initMain, OpenWorldInfo& worldInfo):
netObjMan(initMain.netObjectTypeList, worldInfo.conType != CONNECTIONTYPE_CLIENT),
main(initMain),
rMan(*this),
drawProg(*this),
bMan(*this),
gridMan(*this),
netObjMan(initMain.netObjectTypeList, worldInfo.conType != CONNECTIONTYPE_CLIENT)
gridMan(*this)
{
set_canvas_background_color(main.defaultCanvasBackgroundColor);
displayName = main.displayName;
@ -49,7 +49,7 @@ World::World(MainProgram& initMain, OpenWorldInfo& worldInfo):
switch(conType) {
case CONNECTIONTYPE_CLIENT: {
clientStillConnecting = true;
con.connect_p2p(netSource);
con.connect_p2p(*this, netSource);
set_name("");
break;
}
@ -75,10 +75,13 @@ World::World(MainProgram& initMain, OpenWorldInfo& worldInfo):
if(netObjMan.is_server()) {
stringListTest = netObjMan.make_obj<NetObjOrderedList<std::string>>();
stringListTest->emplace_back_direct(stringListTest, "Hello");
stringListTest->emplace_back_direct(stringListTest, "World");
stringListTest->emplace_back_direct(stringListTest, "Test");
for(const NetObjOrderedListObjectInfoPtr<std::string>& o : stringListTest->get_data())
std::cout << "At position: " << o->get_pos() << " we have string: \"" << *o->get_obj() << "\" with object id " << o->get_obj().get_net_id().to_string() << std::endl;
stringListTest = netObjMan.make_obj<NetObjOrderedList<std::string>>();
stringListTest->emplace_back_direct(stringListTest, "World!");
//stringListTest->emplace_back_direct(stringListTest, "World");
//stringListTest->emplace_back_direct(stringListTest, "Test");
//for(const NetObjOrderedListObjectInfoPtr<std::string>& o : stringListTest->get_data())
// std::cout << "At position: " << o->get_pos() << " we have string: \"" << *o->get_obj() << "\" with object id " << o->get_obj().get_net_id().to_string() << std::endl;
}
}
@ -116,6 +119,14 @@ void World::init_client_callbacks() {
clients[id].cursorColor = cursorColor;
add_chat_message(clients[id].displayName, "joined", Toolbar::ChatMessage::Type::JOIN);
});
con.client_add_recv_callback(CLIENT_UPDATE_NETWORK_OBJECT, [&](cereal::PortableBinaryInputArchive& message) {
if(!con.host_exists()) { // Dont update a direct connected client (which is just the server)
netObjMan.read_update_message(message, nullptr);
std::cout << "Client print update: " << std::endl;
for(const NetworkingObjects::NetObjOrderedListObjectInfoPtr<std::string>& o : stringListTest->get_data())
std::cout << "At position: " << o->get_pos() << " we have string: \"" << *o->get_obj() << "\" with object id " << o->get_obj().get_net_id().to_string() << std::endl;
}
});
con.client_add_recv_callback(CLIENT_USER_DISCONNECT, [&](cereal::PortableBinaryInputArchive& message) {
ServerPortionID id;
message(id);
@ -163,9 +174,19 @@ void World::init_client_callbacks() {
});
}
size_t testTimer = 0;
size_t strToAddNum = 0;
void World::focus_update() {
con.update();
//testTimer++;
//if(testTimer == 1000) {
// std::cout << "added: " << strToAddNum << std::endl;
// stringListTest->emplace_back_direct(stringListTest, std::to_string(strToAddNum++));
// testTimer = 0;
//}
if(con.is_host_disconnected()) {
Logger::get().log("USERINFO", "Host connection failed");
clientStillConnecting = false;

View file

@ -36,6 +36,9 @@ class World {
World(MainProgram& initMain, OpenWorldInfo& worldInfo);
// NOTE: Keep at the very beginning so that it's destroyed last
NetworkingObjects::NetObjManager netObjMan;
void save_file(cereal::PortableBinaryOutputArchive& a) const;
void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version);
@ -47,7 +50,6 @@ class World {
BookmarkManager bMan;
ConnectionManager con;
GridManager gridMan;
NetworkingObjects::NetObjManager netObjMan;
std::deque<Toolbar::ChatMessage> chatMessages;