From 35ebdb0ffe5beabe4606db7b37b153c2e2af86ab Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 28 Nov 2023 11:24:17 +0100 Subject: [PATCH] Refactor `INetworkDriver::PopEvent` to use network event as output parameter rather than raw pointer #1992 --- .../Engine/Networking/Drivers/ENetDriver.cpp | 18 ++++---- Source/Engine/Networking/Drivers/ENetDriver.h | 2 +- .../Networking/Drivers/NetworkLagDriver.cpp | 6 +-- .../Networking/Drivers/NetworkLagDriver.h | 2 +- Source/Engine/Networking/INetworkDriver.h | 2 +- Source/Engine/Networking/NetworkConnection.h | 10 ++++- Source/Engine/Networking/NetworkEvent.h | 16 ++++--- Source/Engine/Networking/NetworkPeer.cpp | 2 +- Source/Engine/Networking/NetworkPeer.h | 42 +++++++------------ Source/Engine/Scripting/ManagedCLR/MClass.h | 2 +- Source/Engine/Scripting/Runtime/DotNet.cpp | 4 +- 11 files changed, 51 insertions(+), 55 deletions(-) diff --git a/Source/Engine/Networking/Drivers/ENetDriver.cpp b/Source/Engine/Networking/Drivers/ENetDriver.cpp index d7a3617a4..c44c83fb8 100644 --- a/Source/Engine/Networking/Drivers/ENetDriver.cpp +++ b/Source/Engine/Networking/Drivers/ENetDriver.cpp @@ -162,7 +162,7 @@ void ENetDriver::Disconnect(const NetworkConnection& connection) } } -bool ENetDriver::PopEvent(NetworkEvent* eventPtr) +bool ENetDriver::PopEvent(NetworkEvent& eventPtr) { ASSERT(_host); ENetEvent event; @@ -173,30 +173,30 @@ bool ENetDriver::PopEvent(NetworkEvent* eventPtr) { // Copy sender data const uint32 connectionId = enet_peer_get_id(event.peer); - eventPtr->Sender.ConnectionId = connectionId; + eventPtr.Sender.ConnectionId = connectionId; switch (event.type) { case ENET_EVENT_TYPE_CONNECT: - eventPtr->EventType = NetworkEventType::Connected; + eventPtr.EventType = NetworkEventType::Connected; if (IsServer()) _peerMap.Add(connectionId, event.peer); break; case ENET_EVENT_TYPE_DISCONNECT: - eventPtr->EventType = NetworkEventType::Disconnected; + eventPtr.EventType = NetworkEventType::Disconnected; if (IsServer()) _peerMap.Remove(connectionId); break; case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: - eventPtr->EventType = NetworkEventType::Timeout; + eventPtr.EventType = NetworkEventType::Timeout; if (IsServer()) _peerMap.Remove(connectionId); break; case ENET_EVENT_TYPE_RECEIVE: - eventPtr->EventType = NetworkEventType::Message; - eventPtr->Message = _networkHost->CreateMessage(); - eventPtr->Message.Length = event.packet->dataLength; - Platform::MemoryCopy(eventPtr->Message.Buffer, event.packet->data, event.packet->dataLength); + eventPtr.EventType = NetworkEventType::Message; + eventPtr.Message = _networkHost->CreateMessage(); + eventPtr.Message.Length = event.packet->dataLength; + Platform::MemoryCopy(eventPtr.Message.Buffer, event.packet->data, event.packet->dataLength); break; default: break; diff --git a/Source/Engine/Networking/Drivers/ENetDriver.h b/Source/Engine/Networking/Drivers/ENetDriver.h index a91263caa..fdabdd5ce 100644 --- a/Source/Engine/Networking/Drivers/ENetDriver.h +++ b/Source/Engine/Networking/Drivers/ENetDriver.h @@ -29,7 +29,7 @@ public: bool Connect() override; void Disconnect() override; void Disconnect(const NetworkConnection& connection) override; - bool PopEvent(NetworkEvent* eventPtr) override; + bool PopEvent(NetworkEvent& eventPtr) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets) override; diff --git a/Source/Engine/Networking/Drivers/NetworkLagDriver.cpp b/Source/Engine/Networking/Drivers/NetworkLagDriver.cpp index 615aab7d6..be07f367c 100644 --- a/Source/Engine/Networking/Drivers/NetworkLagDriver.cpp +++ b/Source/Engine/Networking/Drivers/NetworkLagDriver.cpp @@ -92,7 +92,7 @@ void NetworkLagDriver::Disconnect(const NetworkConnection& connection) _driver->Disconnect(connection); } -bool NetworkLagDriver::PopEvent(NetworkEvent* eventPtr) +bool NetworkLagDriver::PopEvent(NetworkEvent& eventPtr) { if (!_driver) return false; @@ -104,7 +104,7 @@ bool NetworkLagDriver::PopEvent(NetworkEvent* eventPtr) if (e.Lag > 0.0) continue; - *eventPtr = e.Event; + eventPtr = e.Event; _events.RemoveAtKeepOrder(i); return true; } @@ -117,7 +117,7 @@ bool NetworkLagDriver::PopEvent(NetworkEvent* eventPtr) auto& e = _events.AddOne(); e.Lag = (double)Lag; - e.Event = *eventPtr; + e.Event = eventPtr; } return false; } diff --git a/Source/Engine/Networking/Drivers/NetworkLagDriver.h b/Source/Engine/Networking/Drivers/NetworkLagDriver.h index e07545b55..4021523ca 100644 --- a/Source/Engine/Networking/Drivers/NetworkLagDriver.h +++ b/Source/Engine/Networking/Drivers/NetworkLagDriver.h @@ -68,7 +68,7 @@ public: bool Connect() override; void Disconnect() override; void Disconnect(const NetworkConnection& connection) override; - bool PopEvent(NetworkEvent* eventPtr) override; + bool PopEvent(NetworkEvent& eventPtr) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets) override; diff --git a/Source/Engine/Networking/INetworkDriver.h b/Source/Engine/Networking/INetworkDriver.h index e3f23a0a3..1abd4a177 100644 --- a/Source/Engine/Networking/INetworkDriver.h +++ b/Source/Engine/Networking/INetworkDriver.h @@ -71,7 +71,7 @@ public: /// /// The pointer to event structure. /// True when succeeded and the event can be processed. - API_FUNCTION() virtual bool PopEvent(NetworkEvent* eventPtr) = 0; + API_FUNCTION() virtual bool PopEvent(API_PARAM(Out) NetworkEvent& eventPtr) = 0; /// /// Sends given message over specified channel to the server. diff --git a/Source/Engine/Networking/NetworkConnection.h b/Source/Engine/Networking/NetworkConnection.h index c7ae20138..f6a39ea72 100644 --- a/Source/Engine/Networking/NetworkConnection.h +++ b/Source/Engine/Networking/NetworkConnection.h @@ -10,13 +10,19 @@ API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkConnection { DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkConnection); + public: /// /// The identifier of the connection. /// /// Used by network driver implementations. - API_FIELD() - uint32 ConnectionId; + API_FIELD() uint32 ConnectionId; +}; + +template<> +struct TIsPODType +{ + enum { Value = true }; }; inline bool operator==(const NetworkConnection& a, const NetworkConnection& b) diff --git a/Source/Engine/Networking/NetworkEvent.h b/Source/Engine/Networking/NetworkEvent.h index 691d33f0c..eb00aac03 100644 --- a/Source/Engine/Networking/NetworkEvent.h +++ b/Source/Engine/Networking/NetworkEvent.h @@ -43,24 +43,28 @@ API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkEventType API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkEvent { DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkEvent); + public: /// /// The type of the received event. /// - API_FIELD(); - NetworkEventType EventType; + API_FIELD() NetworkEventType EventType; /// /// The message when this event is an "message" event - not valid in any other cases. /// If this is an message-event, make sure to return the message using RecycleMessage function of the peer after processing it! /// - API_FIELD(); - NetworkMessage Message; + API_FIELD() NetworkMessage Message; /// /// The connected of the client that has sent message, connected, disconnected or got a timeout. /// /// Only valid when event has been received on server-peer. - API_FIELD(); - NetworkConnection Sender; + API_FIELD() NetworkConnection Sender; +}; + +template<> +struct TIsPODType +{ + enum { Value = true }; }; diff --git a/Source/Engine/Networking/NetworkPeer.cpp b/Source/Engine/Networking/NetworkPeer.cpp index b39b617e0..c4c0ea712 100644 --- a/Source/Engine/Networking/NetworkPeer.cpp +++ b/Source/Engine/Networking/NetworkPeer.cpp @@ -134,7 +134,7 @@ void NetworkPeer::Disconnect(const NetworkConnection& connection) bool NetworkPeer::PopEvent(NetworkEvent& eventRef) { PROFILE_CPU(); - return NetworkDriver->PopEvent(&eventRef); + return NetworkDriver->PopEvent(eventRef); } NetworkMessage NetworkPeer::CreateMessage() diff --git a/Source/Engine/Networking/NetworkPeer.h b/Source/Engine/Networking/NetworkPeer.h index 6b36e3278..d68584754 100644 --- a/Source/Engine/Networking/NetworkPeer.h +++ b/Source/Engine/Networking/NetworkPeer.h @@ -37,30 +37,26 @@ public: /// Once this is called, this peer becomes a server. /// /// True when succeeded. - API_FUNCTION() - bool Listen(); + API_FUNCTION() bool Listen(); /// /// Starts connection handshake with the end point specified in the structure. /// Once this is called, this peer becomes a client. /// /// True when succeeded. - API_FUNCTION() - bool Connect(); + API_FUNCTION() bool Connect(); /// /// Disconnects from the server. /// /// Can be used only by the client! - API_FUNCTION() - void Disconnect(); + API_FUNCTION() void Disconnect(); /// /// Disconnects given connection from the server. /// /// Can be used only by the server! - API_FUNCTION() - void Disconnect(const NetworkConnection& connection); + API_FUNCTION() void Disconnect(const NetworkConnection& connection); /// /// Tries to pop an network event from the queue. @@ -68,8 +64,7 @@ public: /// The reference to event structure. /// True when succeeded and the event can be processed. /// If this returns message event, make sure to recycle the message using function after processing it! - API_FUNCTION() - bool PopEvent(API_PARAM(out) NetworkEvent& eventRef); + API_FUNCTION() bool PopEvent(API_PARAM(Out) NetworkEvent& eventRef); /// /// Acquires new message from the pool. @@ -77,29 +72,25 @@ public: /// /// The acquired message. /// Make sure to recycle the message to this peer once it is no longer needed! - API_FUNCTION() - NetworkMessage CreateMessage(); + API_FUNCTION() NetworkMessage CreateMessage(); /// /// Returns given message to the pool. /// /// Make sure that this message belongs to the peer and has not been recycled already (debug build checks for this)! - API_FUNCTION() - void RecycleMessage(const NetworkMessage& message); + API_FUNCTION() void RecycleMessage(const NetworkMessage& message); /// /// Acquires new message from the pool and setups it for sending. /// /// The acquired message. - API_FUNCTION() - NetworkMessage BeginSendMessage(); + API_FUNCTION() NetworkMessage BeginSendMessage(); /// /// Aborts given message send. This effectively deinitializes the message and returns it to the pool. /// /// The message. - API_FUNCTION() - void AbortSendMessage(const NetworkMessage& message); + API_FUNCTION() void AbortSendMessage(const NetworkMessage& message); /// /// Sends given message over specified channel to the server. @@ -111,8 +102,7 @@ public: /// Do not recycle the message after calling this. /// This function automatically recycles the message. /// - API_FUNCTION() - bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message); + API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message); /// /// Sends given message over specified channel to the given client connection (target). @@ -125,8 +115,7 @@ public: /// Do not recycle the message after calling this. /// This function automatically recycles the message. /// - API_FUNCTION() - bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const NetworkConnection& target); + API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const NetworkConnection& target); /// /// Sends given message over specified channel to the given client connection (target). @@ -139,8 +128,7 @@ public: /// Do not recycle the message after calling this. /// This function automatically recycles the message. /// - API_FUNCTION() - bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets); + API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets); /// /// Creates new peer using given configuration. @@ -148,15 +136,13 @@ public: /// The configuration to create and setup new peer. /// The peer. /// Peer should be destroyed using once it is no longer in use. Returns null if failed to create a peer (eg. config is invalid). - API_FUNCTION() - static NetworkPeer* CreatePeer(const NetworkConfig& config); + API_FUNCTION() static NetworkPeer* CreatePeer(const NetworkConfig& config); /// /// Shutdowns and destroys given peer. /// /// The peer to destroy. - API_FUNCTION() - static void ShutdownPeer(NetworkPeer* peer); + API_FUNCTION() static void ShutdownPeer(NetworkPeer* peer); public: bool IsValid() const diff --git a/Source/Engine/Scripting/ManagedCLR/MClass.h b/Source/Engine/Scripting/ManagedCLR/MClass.h index 49b153731..a8fbf6db0 100644 --- a/Source/Engine/Scripting/ManagedCLR/MClass.h +++ b/Source/Engine/Scripting/ManagedCLR/MClass.h @@ -18,7 +18,7 @@ private: #elif USE_NETCORE void* _handle; StringAnsi _name; - StringAnsi _namespace_; + StringAnsi _namespace; uint32 _types = 0; mutable uint32 _size = 0; #endif diff --git a/Source/Engine/Scripting/Runtime/DotNet.cpp b/Source/Engine/Scripting/Runtime/DotNet.cpp index 3c90eb4d1..464e60ba6 100644 --- a/Source/Engine/Scripting/Runtime/DotNet.cpp +++ b/Source/Engine/Scripting/Runtime/DotNet.cpp @@ -836,7 +836,7 @@ bool MAssembly::UnloadImage(bool isReloading) MClass::MClass(const MAssembly* parentAssembly, void* handle, const char* name, const char* fullname, const char* namespace_, MTypeAttributes attributes) : _handle(handle) , _name(name) - , _namespace_(namespace_) + , _namespace(namespace_) , _assembly(parentAssembly) , _fullname(fullname) , _hasCachedProperties(false) @@ -915,7 +915,7 @@ StringAnsiView MClass::GetName() const StringAnsiView MClass::GetNamespace() const { - return _namespace_; + return _namespace; } MType* MClass::GetType() const