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);
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;

View File

@@ -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<NetworkConnection, HeapAllocation>& targets) override;

View File

@@ -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;
}

View File

@@ -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<NetworkConnection, HeapAllocation>& targets) override;

View File

@@ -71,7 +71,7 @@ public:
/// </summary>
/// <param name="eventPtr">The pointer to event structure.</param>
/// <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>
/// Sends given message over specified channel to the server.

View File

@@ -10,13 +10,19 @@
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkConnection
{
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkConnection);
public:
/// <summary>
/// The identifier of the connection.
/// </summary>
/// <remarks>Used by network driver implementations.</remarks>
API_FIELD()
uint32 ConnectionId;
API_FIELD() uint32 ConnectionId;
};
template<>
struct TIsPODType<NetworkConnection>
{
enum { Value = true };
};
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
{
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkEvent);
public:
/// <summary>
/// The type of the received event.
/// </summary>
API_FIELD();
NetworkEventType EventType;
API_FIELD() NetworkEventType EventType;
/// <summary>
/// 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!
/// </summary>
API_FIELD();
NetworkMessage Message;
API_FIELD() NetworkMessage Message;
/// <summary>
/// The connected of the client that has sent message, connected, disconnected or got a timeout.
/// </summary>
/// <remarks>Only valid when event has been received on server-peer.</remarks>
API_FIELD();
NetworkConnection Sender;
API_FIELD() 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)
{
PROFILE_CPU();
return NetworkDriver->PopEvent(&eventRef);
return NetworkDriver->PopEvent(eventRef);
}
NetworkMessage NetworkPeer::CreateMessage()

View File

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

View File

@@ -18,7 +18,7 @@ private:
#elif USE_NETCORE
void* _handle;
StringAnsi _name;
StringAnsi _namespace_;
StringAnsi _namespace;
uint32 _types = 0;
mutable uint32 _size = 0;
#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)
: _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