Fixed comparison operators for net object pointers
This commit is contained in:
parent
26d807ce87
commit
4640158343
8 changed files with 62 additions and 18 deletions
|
|
@ -25,4 +25,8 @@ namespace NetworkingObjects {
|
|||
server = initServer;
|
||||
updateCommandID = initUpdateCommandID;
|
||||
}
|
||||
void NetObjManager::disconnect() {
|
||||
server = nullptr;
|
||||
client = nullptr;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ namespace NetworkingObjects {
|
|||
void read_update_message(cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>& clientReceivedFrom);
|
||||
void set_client(std::shared_ptr<NetClient> initClient, MessageCommandType initUpdateCommandID);
|
||||
void set_server(std::shared_ptr<NetServer> initServer, MessageCommandType initUpdateCommandID);
|
||||
void disconnect();
|
||||
bool is_server() const;
|
||||
template <typename T> NetObjOwnerPtr<T> read_create_message(cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>& clientReceivedFrom) {
|
||||
NetObjID id;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,10 @@ namespace NetworkingObjects {
|
|||
std::add_lvalue_reference_t<T> operator*() const requires(!std::is_void_v<T>);
|
||||
T* operator->() const;
|
||||
operator bool() const;
|
||||
std::strong_ordering operator<=>(const NetObjOwnerPtr<T>& rhs) const;
|
||||
bool operator!=(const NetObjOwnerPtr<T>& rhs) const;
|
||||
bool operator==(const NetObjOwnerPtr<T>& rhs) const;
|
||||
bool operator!=(const NetObjTemporaryPtr<T>& rhs) const;
|
||||
bool operator==(const NetObjTemporaryPtr<T>& rhs) const;
|
||||
void send_client_update(const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void send_server_update_to_client(const std::shared_ptr<NetServer::ClientData>& clientToSendTo, const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void send_server_update_to_all_clients_except(const std::shared_ptr<NetServer::ClientData>& clientToNotSendTo, const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
|
|
|
|||
|
|
@ -64,8 +64,20 @@ namespace NetworkingObjects {
|
|||
return rawPtr;
|
||||
}
|
||||
|
||||
template <typename T> std::strong_ordering NetObjOwnerPtr<T>::operator<=>(const NetObjOwnerPtr<T>& rhs) const {
|
||||
return rawPtr <=> rhs.rawPtr;
|
||||
template <typename T> bool NetObjOwnerPtr<T>::operator!=(const NetObjOwnerPtr<T>& rhs) const {
|
||||
return rawPtr != rhs.rawPtr;
|
||||
}
|
||||
|
||||
template <typename T> bool NetObjOwnerPtr<T>::operator==(const NetObjOwnerPtr<T>& rhs) const {
|
||||
return rawPtr == rhs.rawPtr;
|
||||
}
|
||||
|
||||
template <typename T> bool NetObjOwnerPtr<T>::operator!=(const NetObjTemporaryPtr<T>& rhs) const {
|
||||
return rawPtr != rhs.get();
|
||||
}
|
||||
|
||||
template <typename T> bool NetObjOwnerPtr<T>::operator==(const NetObjTemporaryPtr<T>& rhs) const {
|
||||
return rawPtr == rhs.get();
|
||||
}
|
||||
|
||||
template <typename T> void NetObjOwnerPtr<T>::send_client_update(const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,10 @@ namespace NetworkingObjects {
|
|||
std::add_lvalue_reference_t<T> operator*() const requires(!std::is_void_v<T>);
|
||||
T* operator->() const;
|
||||
operator bool() const;
|
||||
std::strong_ordering operator<=>(const NetObjTemporaryPtr<T>& rhs) const;
|
||||
bool operator!=(const NetObjTemporaryPtr<T>& rhs) const;
|
||||
bool operator==(const NetObjTemporaryPtr<T>& rhs) const;
|
||||
bool operator!=(const NetObjOwnerPtr<T>& rhs) const;
|
||||
bool operator==(const NetObjOwnerPtr<T>& rhs) const;
|
||||
void send_client_update(const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void send_server_update_to_client(const std::shared_ptr<NetServer::ClientData>& clientToSendTo, const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void send_server_update_to_all_clients_except(const std::shared_ptr<NetServer::ClientData>& clientToNotSendTo, const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
|
|
|
|||
|
|
@ -57,8 +57,20 @@ namespace NetworkingObjects {
|
|||
return rawPtr;
|
||||
}
|
||||
|
||||
template <typename T> std::strong_ordering NetObjTemporaryPtr<T>::operator<=>(const NetObjTemporaryPtr<T>& rhs) const {
|
||||
return rawPtr <=> rhs.rawPtr;
|
||||
template <typename T> bool NetObjTemporaryPtr<T>::operator!=(const NetObjTemporaryPtr<T>& rhs) const {
|
||||
return rawPtr != rhs.rawPtr;
|
||||
}
|
||||
|
||||
template <typename T> bool NetObjTemporaryPtr<T>::operator==(const NetObjTemporaryPtr<T>& rhs) const {
|
||||
return rawPtr == rhs.rawPtr;
|
||||
}
|
||||
|
||||
template <typename T> bool NetObjTemporaryPtr<T>::operator!=(const NetObjOwnerPtr<T>& rhs) const {
|
||||
return rawPtr != rhs.get();
|
||||
}
|
||||
|
||||
template <typename T> bool NetObjTemporaryPtr<T>::operator==(const NetObjOwnerPtr<T>& rhs) const {
|
||||
return rawPtr == rhs.get();
|
||||
}
|
||||
|
||||
template <typename T> void NetObjTemporaryPtr<T>::send_client_update(const std::string& channel, std::function<void(const NetObjTemporaryPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const {
|
||||
|
|
|
|||
|
|
@ -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<ClientData>& o, cereal::PortableBinaryOutputArchive & a) {
|
||||
o.send_server_update_to_all_clients(RELIABLE_COMMAND_CHANNEL, [&chatMessage](const NetObjTemporaryPtr<ClientData>& o, cereal::PortableBinaryOutputArchive & a) {
|
||||
a(ClientDataCommand::SEND_CHAT_MESSAGE, chatMessage);
|
||||
});
|
||||
break;
|
||||
|
|
@ -116,6 +116,7 @@ void ClientData::set_camera_coords(const NetworkingObjects::NetObjTemporaryPtr<C
|
|||
}
|
||||
|
||||
void ClientData::send_chat_message(const NetworkingObjects::NetObjTemporaryPtr<ClientData>& o, World& world, const std::string& chatMessage) {
|
||||
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<ClientData>& o, cereal::PortableBinaryOutputArchive & a) {
|
||||
a(ClientDataCommand::SEND_CHAT_MESSAGE, chatMessage);
|
||||
|
|
|
|||
|
|
@ -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<NetServer::ClientData> 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<NetworkingObjects::NetObjOrderedList<WorldGrid>>(message, nullptr);
|
||||
drawProg.read_components_client(message);
|
||||
canvasTheme.read_create_message(message);
|
||||
|
||||
clients = netObjMan.read_create_message<NetworkingObjects::NetObjUnorderedSet<ClientData>>(message, nullptr);
|
||||
init_client_data_list_callbacks();
|
||||
ownClientData = netObjMan.get_obj_from_id<ClientData>(clientDataObjID);
|
||||
ownClientData = netObjMan.get_obj_temporary_ref_from_id<ClientData>(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()) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue