Refactor INetworkDriver::PopEvent to use network event as output parameter rather than raw pointer

#1992
This commit is contained in:
Wojtek Figat
2023-11-28 11:24:17 +01:00
parent fd938e8284
commit 35ebdb0ffe
11 changed files with 51 additions and 55 deletions

View File

@@ -162,7 +162,7 @@ void ENetDriver::Disconnect(const NetworkConnection& connection)
} }
} }
bool ENetDriver::PopEvent(NetworkEvent* eventPtr) bool ENetDriver::PopEvent(NetworkEvent& eventPtr)
{ {
ASSERT(_host); ASSERT(_host);
ENetEvent event; ENetEvent event;
@@ -173,30 +173,30 @@ bool ENetDriver::PopEvent(NetworkEvent* eventPtr)
{ {
// Copy sender data // Copy sender data
const uint32 connectionId = enet_peer_get_id(event.peer); const uint32 connectionId = enet_peer_get_id(event.peer);
eventPtr->Sender.ConnectionId = connectionId; eventPtr.Sender.ConnectionId = connectionId;
switch (event.type) switch (event.type)
{ {
case ENET_EVENT_TYPE_CONNECT: case ENET_EVENT_TYPE_CONNECT:
eventPtr->EventType = NetworkEventType::Connected; eventPtr.EventType = NetworkEventType::Connected;
if (IsServer()) if (IsServer())
_peerMap.Add(connectionId, event.peer); _peerMap.Add(connectionId, event.peer);
break; break;
case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT:
eventPtr->EventType = NetworkEventType::Disconnected; eventPtr.EventType = NetworkEventType::Disconnected;
if (IsServer()) if (IsServer())
_peerMap.Remove(connectionId); _peerMap.Remove(connectionId);
break; break;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
eventPtr->EventType = NetworkEventType::Timeout; eventPtr.EventType = NetworkEventType::Timeout;
if (IsServer()) if (IsServer())
_peerMap.Remove(connectionId); _peerMap.Remove(connectionId);
break; break;
case ENET_EVENT_TYPE_RECEIVE: case ENET_EVENT_TYPE_RECEIVE:
eventPtr->EventType = NetworkEventType::Message; eventPtr.EventType = NetworkEventType::Message;
eventPtr->Message = _networkHost->CreateMessage(); eventPtr.Message = _networkHost->CreateMessage();
eventPtr->Message.Length = event.packet->dataLength; eventPtr.Message.Length = event.packet->dataLength;
Platform::MemoryCopy(eventPtr->Message.Buffer, event.packet->data, event.packet->dataLength); Platform::MemoryCopy(eventPtr.Message.Buffer, event.packet->data, event.packet->dataLength);
break; break;
default: default:
break; break;

View File

@@ -29,7 +29,7 @@ public:
bool Connect() override; bool Connect() override;
void Disconnect() override; void Disconnect() override;
void Disconnect(const NetworkConnection& connection) 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) override;
void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) override;
void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets) override;

View File

@@ -92,7 +92,7 @@ void NetworkLagDriver::Disconnect(const NetworkConnection& connection)
_driver->Disconnect(connection); _driver->Disconnect(connection);
} }
bool NetworkLagDriver::PopEvent(NetworkEvent* eventPtr) bool NetworkLagDriver::PopEvent(NetworkEvent& eventPtr)
{ {
if (!_driver) if (!_driver)
return false; return false;
@@ -104,7 +104,7 @@ bool NetworkLagDriver::PopEvent(NetworkEvent* eventPtr)
if (e.Lag > 0.0) if (e.Lag > 0.0)
continue; continue;
*eventPtr = e.Event; eventPtr = e.Event;
_events.RemoveAtKeepOrder(i); _events.RemoveAtKeepOrder(i);
return true; return true;
} }
@@ -117,7 +117,7 @@ bool NetworkLagDriver::PopEvent(NetworkEvent* eventPtr)
auto& e = _events.AddOne(); auto& e = _events.AddOne();
e.Lag = (double)Lag; e.Lag = (double)Lag;
e.Event = *eventPtr; e.Event = eventPtr;
} }
return false; return false;
} }

View File

@@ -68,7 +68,7 @@ public:
bool Connect() override; bool Connect() override;
void Disconnect() override; void Disconnect() override;
void Disconnect(const NetworkConnection& connection) 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) override;
void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) override;
void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets) override; void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets) override;

View File

@@ -71,7 +71,7 @@ public:
/// </summary> /// </summary>
/// <param name="eventPtr">The pointer to event structure.</param> /// <param name="eventPtr">The pointer to event structure.</param>
/// <returns>True when succeeded and the event can be processed.</returns> /// <returns>True when succeeded and the event can be processed.</returns>
API_FUNCTION() virtual bool PopEvent(NetworkEvent* eventPtr) = 0; API_FUNCTION() virtual bool PopEvent(API_PARAM(Out) NetworkEvent& eventPtr) = 0;
/// <summary> /// <summary>
/// Sends given message over specified channel to the server. /// Sends given message over specified channel to the server.

View File

@@ -10,13 +10,19 @@
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkConnection API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkConnection
{ {
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkConnection); DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkConnection);
public: public:
/// <summary> /// <summary>
/// The identifier of the connection. /// The identifier of the connection.
/// </summary> /// </summary>
/// <remarks>Used by network driver implementations.</remarks> /// <remarks>Used by network driver implementations.</remarks>
API_FIELD() API_FIELD() uint32 ConnectionId;
uint32 ConnectionId; };
template<>
struct TIsPODType<NetworkConnection>
{
enum { Value = true };
}; };
inline bool operator==(const NetworkConnection& a, const NetworkConnection& b) inline bool operator==(const NetworkConnection& a, const NetworkConnection& b)

View File

@@ -43,24 +43,28 @@ API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkEventType
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkEvent API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkEvent
{ {
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkEvent); DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkEvent);
public: public:
/// <summary> /// <summary>
/// The type of the received event. /// The type of the received event.
/// </summary> /// </summary>
API_FIELD(); API_FIELD() NetworkEventType EventType;
NetworkEventType EventType;
/// <summary> /// <summary>
/// The message when this event is an "message" event - not valid in any other cases. /// 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! /// If this is an message-event, make sure to return the message using RecycleMessage function of the peer after processing it!
/// </summary> /// </summary>
API_FIELD(); API_FIELD() NetworkMessage Message;
NetworkMessage Message;
/// <summary> /// <summary>
/// The connected of the client that has sent message, connected, disconnected or got a timeout. /// The connected of the client that has sent message, connected, disconnected or got a timeout.
/// </summary> /// </summary>
/// <remarks>Only valid when event has been received on server-peer.</remarks> /// <remarks>Only valid when event has been received on server-peer.</remarks>
API_FIELD(); API_FIELD() NetworkConnection Sender;
NetworkConnection Sender; };
template<>
struct TIsPODType<NetworkEvent>
{
enum { Value = true };
}; };

View File

@@ -134,7 +134,7 @@ void NetworkPeer::Disconnect(const NetworkConnection& connection)
bool NetworkPeer::PopEvent(NetworkEvent& eventRef) bool NetworkPeer::PopEvent(NetworkEvent& eventRef)
{ {
PROFILE_CPU(); PROFILE_CPU();
return NetworkDriver->PopEvent(&eventRef); return NetworkDriver->PopEvent(eventRef);
} }
NetworkMessage NetworkPeer::CreateMessage() NetworkMessage NetworkPeer::CreateMessage()

View File

@@ -37,30 +37,26 @@ public:
/// Once this is called, this peer becomes a server. /// Once this is called, this peer becomes a server.
/// </summary> /// </summary>
/// <returns>True when succeeded.</returns> /// <returns>True when succeeded.</returns>
API_FUNCTION() API_FUNCTION() bool Listen();
bool Listen();
/// <summary> /// <summary>
/// Starts connection handshake with the end point specified in the <seealso cref="NetworkConfig"/> structure. /// Starts connection handshake with the end point specified in the <seealso cref="NetworkConfig"/> structure.
/// Once this is called, this peer becomes a client. /// Once this is called, this peer becomes a client.
/// </summary> /// </summary>
/// <returns>True when succeeded.</returns> /// <returns>True when succeeded.</returns>
API_FUNCTION() API_FUNCTION() bool Connect();
bool Connect();
/// <summary> /// <summary>
/// Disconnects from the server. /// Disconnects from the server.
/// </summary> /// </summary>
/// <remarks>Can be used only by the client!</remarks> /// <remarks>Can be used only by the client!</remarks>
API_FUNCTION() API_FUNCTION() void Disconnect();
void Disconnect();
/// <summary> /// <summary>
/// Disconnects given connection from the server. /// Disconnects given connection from the server.
/// </summary> /// </summary>
/// <remarks>Can be used only by the server!</remarks> /// <remarks>Can be used only by the server!</remarks>
API_FUNCTION() API_FUNCTION() void Disconnect(const NetworkConnection& connection);
void Disconnect(const NetworkConnection& connection);
/// <summary> /// <summary>
/// Tries to pop an network event from the queue. /// Tries to pop an network event from the queue.
@@ -68,8 +64,7 @@ public:
/// <param name="eventRef">The reference to event structure.</param> /// <param name="eventRef">The reference to event structure.</param>
/// <returns>True when succeeded and the event can be processed.</returns> /// <returns>True when succeeded and the event can be processed.</returns>
/// <remarks>If this returns message event, make sure to recycle the message using <see cref="RecycleMessage"/> function after processing it!</remarks> /// <remarks>If this returns message event, make sure to recycle the message using <see cref="RecycleMessage"/> function after processing it!</remarks>
API_FUNCTION() API_FUNCTION() bool PopEvent(API_PARAM(Out) NetworkEvent& eventRef);
bool PopEvent(API_PARAM(out) NetworkEvent& eventRef);
/// <summary> /// <summary>
/// Acquires new message from the pool. /// Acquires new message from the pool.
@@ -77,29 +72,25 @@ public:
/// </summary> /// </summary>
/// <returns>The acquired message.</returns> /// <returns>The acquired message.</returns>
/// <remarks>Make sure to recycle the message to this peer once it is no longer needed!</remarks> /// <remarks>Make sure to recycle the message to this peer once it is no longer needed!</remarks>
API_FUNCTION() API_FUNCTION() NetworkMessage CreateMessage();
NetworkMessage CreateMessage();
/// <summary> /// <summary>
/// Returns given message to the pool. /// Returns given message to the pool.
/// </summary> /// </summary>
/// <remarks>Make sure that this message belongs to the peer and has not been recycled already (debug build checks for this)!</remarks> /// <remarks>Make sure that this message belongs to the peer and has not been recycled already (debug build checks for this)!</remarks>
API_FUNCTION() API_FUNCTION() void RecycleMessage(const NetworkMessage& message);
void RecycleMessage(const NetworkMessage& message);
/// <summary> /// <summary>
/// Acquires new message from the pool and setups it for sending. /// Acquires new message from the pool and setups it for sending.
/// </summary> /// </summary>
/// <returns>The acquired message.</returns> /// <returns>The acquired message.</returns>
API_FUNCTION() API_FUNCTION() NetworkMessage BeginSendMessage();
NetworkMessage BeginSendMessage();
/// <summary> /// <summary>
/// Aborts given message send. This effectively deinitializes the message and returns it to the pool. /// Aborts given message send. This effectively deinitializes the message and returns it to the pool.
/// </summary> /// </summary>
/// <param name="message">The message.</param> /// <param name="message">The message.</param>
API_FUNCTION() API_FUNCTION() void AbortSendMessage(const NetworkMessage& message);
void AbortSendMessage(const NetworkMessage& message);
/// <summary> /// <summary>
/// Sends given message over specified channel to the server. /// Sends given message over specified channel to the server.
@@ -111,8 +102,7 @@ public:
/// Do not recycle the message after calling this. /// Do not recycle the message after calling this.
/// This function automatically recycles the message. /// This function automatically recycles the message.
/// </remarks> /// </remarks>
API_FUNCTION() API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message);
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message);
/// <summary> /// <summary>
/// Sends given message over specified channel to the given client connection (target). /// 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. /// Do not recycle the message after calling this.
/// This function automatically recycles the message. /// This function automatically recycles the message.
/// </remarks> /// </remarks>
API_FUNCTION() API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const NetworkConnection& target);
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const NetworkConnection& target);
/// <summary> /// <summary>
/// Sends given message over specified channel to the given client connection (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. /// Do not recycle the message after calling this.
/// This function automatically recycles the message. /// This function automatically recycles the message.
/// </remarks> /// </remarks>
API_FUNCTION() API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets);
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets);
/// <summary> /// <summary>
/// Creates new peer using given configuration. /// Creates new peer using given configuration.
@@ -148,15 +136,13 @@ public:
/// <param name="config">The configuration to create and setup new peer.</param> /// <param name="config">The configuration to create and setup new peer.</param>
/// <returns>The peer.</returns> /// <returns>The peer.</returns>
/// <remarks>Peer should be destroyed using <see cref="ShutdownPeer"/> once it is no longer in use. Returns null if failed to create a peer (eg. config is invalid).</remarks> /// <remarks>Peer should be destroyed using <see cref="ShutdownPeer"/> once it is no longer in use. Returns null if failed to create a peer (eg. config is invalid).</remarks>
API_FUNCTION() API_FUNCTION() static NetworkPeer* CreatePeer(const NetworkConfig& config);
static NetworkPeer* CreatePeer(const NetworkConfig& config);
/// <summary> /// <summary>
/// Shutdowns and destroys given peer. /// Shutdowns and destroys given peer.
/// </summary> /// </summary>
/// <param name="peer">The peer to destroy.</param> /// <param name="peer">The peer to destroy.</param>
API_FUNCTION() API_FUNCTION() static void ShutdownPeer(NetworkPeer* peer);
static void ShutdownPeer(NetworkPeer* peer);
public: public:
bool IsValid() const bool IsValid() const

View File

@@ -18,7 +18,7 @@ private:
#elif USE_NETCORE #elif USE_NETCORE
void* _handle; void* _handle;
StringAnsi _name; StringAnsi _name;
StringAnsi _namespace_; StringAnsi _namespace;
uint32 _types = 0; uint32 _types = 0;
mutable uint32 _size = 0; mutable uint32 _size = 0;
#endif #endif

View File

@@ -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) MClass::MClass(const MAssembly* parentAssembly, void* handle, const char* name, const char* fullname, const char* namespace_, MTypeAttributes attributes)
: _handle(handle) : _handle(handle)
, _name(name) , _name(name)
, _namespace_(namespace_) , _namespace(namespace_)
, _assembly(parentAssembly) , _assembly(parentAssembly)
, _fullname(fullname) , _fullname(fullname)
, _hasCachedProperties(false) , _hasCachedProperties(false)
@@ -915,7 +915,7 @@ StringAnsiView MClass::GetName() const
StringAnsiView MClass::GetNamespace() const StringAnsiView MClass::GetNamespace() const
{ {
return _namespace_; return _namespace;
} }
MType* MClass::GetType() const MType* MClass::GetType() const