From d3fc12f52ee6fe942ab4af93942c9ba33e551206 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 11 Jan 2024 19:11:25 +0200 Subject: [PATCH] Track the sender of the current packet --- source/game/networking.cpp | 47 ++++++++++++++++++++++++++++++-------- source/game/networking.h | 30 ++++++++++++++++-------- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/source/game/networking.cpp b/source/game/networking.cpp index 9c2894f..7228a82 100644 --- a/source/game/networking.cpp +++ b/source/game/networking.cpp @@ -1,4 +1,5 @@ #include "networking.h" +#include #include #include #include "director.h" @@ -89,20 +90,16 @@ unsigned Networking::get_next_protocol_id() return next_id++; } -void Networking::set_protocol(unsigned id, unique_ptr net_proto) +void Networking::set_receiver(unsigned id, Net::DynamicReceiver *recv) { - if(protocols.size()<=id) - protocols.resize(id+1); - - Protocol &proto = protocols[id]; - proto.protocol = move(net_proto); + protocols[id].receiver = recv; if(state==SERVER) { for(const auto &c: clients) - c->get_communicator().add_protocol(*proto.protocol, proto.receiver); + c->update_receiver(id); } else if(state==CONNECTING || state==CONNECTED) - connection->get_communicator().add_protocol(*proto.protocol, proto.receiver); + connection->update_receiver(id); } uint16_t Networking::intern_string(const string &value) @@ -210,6 +207,16 @@ void Networking::stage_activated(const Events::StageActivated &event) } +void Networking::ProxyReceiver::receive(unsigned i, const Variant &p) +{ + if(receiver) + { + SetForScope set_sender(networking.current_sender, &connection); + receiver->receive(i, p); + } +} + + Networking::Connection::Connection(Networking &n, unique_ptr s): networking(n), socket(move(s)), @@ -230,6 +237,20 @@ void Networking::Connection::error(const exception &) stale = true; } +void Networking::Connection::update_receiver(unsigned id) +{ + if(id>=receivers.size()) + receivers.resize(id+1); + + unique_ptr &proxy_ptr = receivers[id]; + if(!proxy_ptr) + { + proxy_ptr = make_unique(networking, *this); + communicator.add_protocol(*networking.protocols[id].protocol, *proxy_ptr); + } + proxy_ptr->set_receiver(networking.protocols[id].receiver); +} + Networking::ServerConnection::ServerConnection(Networking &n, const Net::SockAddr &addr): Connection(n, make_unique(addr.get_family())) @@ -298,8 +319,14 @@ Networking::ClientConnection::ClientConnection(Networking &n, unique_ptr(networking, *this)).get(); + recv_ptr->set_receiver(networking.protocols[i].receiver); + communicator.add_protocol(*proto, *recv_ptr); + } } void Networking::ClientConnection::protocol_ready(const Net::Protocol &p) diff --git a/source/game/networking.h b/source/game/networking.h index c12f39f..f3d8e82 100644 --- a/source/game/networking.h +++ b/source/game/networking.h @@ -30,20 +30,26 @@ public: Events::NetworkPlayerDeparted, Events::ServerStageActivated>; private: - class SwitchableReceiver: public Net::DynamicReceiver + class Connection; + + class ProxyReceiver: public Net::DynamicReceiver { private: + Networking &networking; + Connection &connection; Net::DynamicReceiver *receiver = nullptr; public: + ProxyReceiver(Networking &n, Connection &c): networking(n), connection(c) { } + void set_receiver(Net::DynamicReceiver *r) { receiver = r; } - void receive(unsigned i, const Variant &p) override { if(receiver) receiver->receive(i, p); } + void receive(unsigned, const Variant &) override; }; struct Protocol { std::unique_ptr protocol; - SwitchableReceiver receiver; + Net::DynamicReceiver *receiver = nullptr; }; class Connection @@ -52,6 +58,7 @@ private: Networking &networking; std::unique_ptr socket; Net::Communicator communicator; + std::vector> receivers; bool stale = false; Connection(Networking &, std::unique_ptr); @@ -61,6 +68,7 @@ private: public: Net::Communicator &get_communicator() { return communicator; } + void update_receiver(unsigned); bool is_stale() const { return stale; } }; @@ -123,6 +131,7 @@ private: std::vector players; unsigned next_player_id = 1; std::vector strings; + Connection *current_sender = nullptr; public: Networking(Director &); @@ -140,7 +149,7 @@ public: private: static unsigned get_next_protocol_id(); - void set_protocol(unsigned, std::unique_ptr); + void set_receiver(unsigned, Net::DynamicReceiver *); public: std::uint16_t intern_string(const std::string &); @@ -170,11 +179,14 @@ template const Net::Protocol &Networking::set_receiver(Net::DynamicReceiver *r) { static unsigned id = get_next_protocol_id(); - if(id>=protocols.size() || !protocols[id].protocol) - set_protocol(id, std::make_unique

()); - - protocols[id].receiver.set_receiver(r); - return *protocols[id].protocol.get(); + if(id>=protocols.size()) + protocols.resize(id+1); + Protocol &proto = protocols[id]; + if(!proto.protocol) + proto.protocol = std::make_unique

(); + + set_receiver(id, r); + return *proto.protocol.get(); } template -- 2.45.2