Cleanup ENetDriver and use interface properly for scripting

This commit is contained in:
Wojtek Figat
2021-10-04 12:23:01 +02:00
parent c3c0a4ef0d
commit ecf926a537
5 changed files with 52 additions and 51 deletions

View File

@@ -50,6 +50,11 @@ void SendPacketToPeer(ENetPeer* peer, const NetworkChannelType channelType, cons
// TODO: To reduce latency, we can use `enet_host_flush` to flush all packets. Maybe some API, like NetworkManager::FlushQueues()?
}
ENetDriver::ENetDriver(const SpawnParams& params)
: PersistentScriptingObject(params)
{
}
void ENetDriver::Initialize(NetworkPeer* host, const NetworkConfig& config)
{
_networkHost = host;

View File

@@ -6,43 +6,42 @@
#include "Engine/Networking/INetworkDriver.h"
#include "Engine/Networking/NetworkConnection.h"
#include "Engine/Networking/NetworkConfig.h"
#include "Engine/Core/Collections/Dictionary.h"
#include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Scripting/ScriptingType.h"
/// <summary>
/// Low-level network transport interface implementation based on ENet library.
/// </summary>
API_CLASS(Namespace="FlaxEngine.Networking", Sealed) class FLAXENGINE_API ENetDriver : public INetworkDriver
API_CLASS(Namespace="FlaxEngine.Networking", Sealed) class FLAXENGINE_API ENetDriver : public PersistentScriptingObject, public INetworkDriver
{
DECLARE_SCRIPTING_TYPE_MINIMAL(ENetDriver);
DECLARE_SCRIPTING_TYPE(ENetDriver);
public:
// [INetworkDriver]
void Initialize(NetworkPeer* host, const NetworkConfig& config) override;
void Dispose() override;
bool Listen() override;
bool Connect() override;
void Disconnect() override;
void Disconnect(const NetworkConnection& connection) 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;
private:
bool IsServer() const
{
return _host != nullptr && _peer == nullptr;
}
private:
NetworkConfig _config;
NetworkPeer* _networkHost;
void* _host = nullptr;
void* _peer = nullptr;
Dictionary<uint32, void*> _peerMap;
};

View File

@@ -2,8 +2,8 @@
#pragma once
#include "Engine/Scripting/ScriptingType.h"
#include "Types.h"
#include "Engine/Scripting/ScriptingType.h"
/// <summary>
/// Basic interface for the low-level network transport/driver.
@@ -11,62 +11,59 @@
API_INTERFACE(Namespace="FlaxEngine.Networking") class FLAXENGINE_API INetworkDriver
{
DECLARE_SCRIPTING_TYPE_MINIMAL(INetworkDriver);
public:
/// <summary>
/// Finalizes an instance of the <see cref="INetworkDriver"/> class.
/// </summary>
virtual ~INetworkDriver() = default;
public:
/// <summary>
/// Initializes the instance of this network driver using given configuration.
/// </summary>
/// <param name="host">The peer that this driver has been assigned to.</param>
/// <param name="config">The network config to use to configure this driver.</param>
virtual void Initialize(NetworkPeer* host, const NetworkConfig& config) = 0;
API_FUNCTION() virtual void Initialize(NetworkPeer* host, const NetworkConfig& config) = 0;
/// <summary>
/// Disposes this driver making it no longer usable.
/// Reserved for resource deallocation etc.
/// </summary>
virtual void Dispose() = 0;
API_FUNCTION() virtual void Dispose() = 0;
/// <summary>
/// Starts listening for incoming connections.
/// Once this is called, this driver becomes a server.
/// </summary>
/// <returns>True when succeeded.</returns>
virtual bool Listen() = 0;
API_FUNCTION() virtual bool Listen() = 0;
/// <summary>
/// Starts connection handshake with the end point specified in the <seealso cref="NetworkConfig"/> structure.
/// Once this is called, this driver becomes a client.
/// </summary>
/// <returns>True when succeeded.</returns>
virtual bool Connect() = 0;
API_FUNCTION() virtual bool Connect() = 0;
/// <summary>
/// Disconnects from the server.
/// </summary>
/// <remarks>Can be used only by the client!</remarks>
virtual void Disconnect() = 0;
API_FUNCTION() virtual void Disconnect() = 0;
/// <summary>
/// Disconnects given connection from the server.
/// </summary>
/// <remarks>Can be used only by the server!</remarks>
virtual void Disconnect(const NetworkConnection& connection) = 0;
API_FUNCTION() virtual void Disconnect(const NetworkConnection& connection) = 0;
/// <summary>
/// Tries to pop an network event from the queue.
/// </summary>
/// <param name="eventPtr">The pointer to event structure.</param>
/// <returns>True when succeeded and the event can be processed.</returns>
virtual bool PopEvent(NetworkEvent* eventPtr) = 0;
API_FUNCTION() virtual bool PopEvent(NetworkEvent* eventPtr) = 0;
/// <summary>
/// Sends given message over specified channel to the server.
/// </summary>
@@ -76,8 +73,9 @@ public:
/// <remarks>
/// Do not recycle the message after calling this.
/// This function automatically recycles the message.
virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) = 0;
/// </remarks>
API_FUNCTION() virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) = 0;
/// <summary>
/// Sends given message over specified channel to the given client connection (target).
/// </summary>
@@ -89,8 +87,8 @@ public:
/// Do not recycle the message after calling this.
/// This function automatically recycles the message.
/// </remarks>
virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) = 0;
API_FUNCTION() virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) = 0;
/// <summary>
/// Sends given message over specified channel to the given client connection (target).
/// </summary>
@@ -102,9 +100,8 @@ public:
/// Do not recycle the message after calling this.
/// This function automatically recycles the message.
/// </remarks>
virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets) = 0;
API_FUNCTION() virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection, HeapAllocation>& targets) = 0;
// TODO: Stats API
// TODO: Simulation API
};

View File

@@ -16,37 +16,34 @@ API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkMessa
{
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkMessage);
public:
/// <summary>
/// The raw message buffer.
/// </summary>
API_FIELD()
uint8* Buffer = nullptr;
API_FIELD() uint8* Buffer = nullptr;
/// <summary>
/// The unique, internal message identifier.
/// </summary>
API_FIELD()
uint32 MessageId = 0;
API_FIELD() uint32 MessageId = 0;
/// <summary>
/// The size in bytes of the buffer that this message has.
/// </summary>
API_FIELD()
uint32 BufferSize = 0;
API_FIELD() uint32 BufferSize = 0;
/// <summary>
/// The length in bytes of this message.
/// </summary>
API_FIELD()
uint32 Length = 0;
API_FIELD() uint32 Length = 0;
/// <summary>
/// The position in bytes in buffer where the next read/write will occur.
/// </summary>
API_FIELD()
uint32 Position = 0;
API_FIELD() uint32 Position = 0;
public:
/// <summary>
/// Initializes default values of the <seealso cref="NetworkMessage"/> structure.
/// </summary>
@@ -55,13 +52,14 @@ public:
/// <summary>
/// Initializes values of the <seealso cref="NetworkMessage"/> structure.
/// </summary>
NetworkMessage(uint8* buffer, uint32 messageId, uint32 bufferSize, uint32 length, uint32 position) :
Buffer(buffer), MessageId(messageId), BufferSize(bufferSize), Length(length), Position(position)
NetworkMessage(uint8* buffer, uint32 messageId, uint32 bufferSize, uint32 length, uint32 position)
: Buffer(buffer), MessageId(messageId), BufferSize(bufferSize), Length(length), Position(position)
{ }
~NetworkMessage() = default;
public:
/// <summary>
/// Writes raw bytes into the message.
/// </summary>
@@ -73,7 +71,7 @@ public:
Platform::MemoryCopy(Buffer + Position, bytes, numBytes);
Position += numBytes;
}
/// <summary>
/// Reads raw bytes from the message into the given byte array.
/// </summary>
@@ -92,8 +90,6 @@ public:
#define DECL_READWRITE(type, name) \
FORCE_INLINE void Write##name(type value) { WriteBytes(reinterpret_cast<uint8*>(&value), sizeof(type)); } \
FORCE_INLINE type Read##name() { type value = 0; ReadBytes(reinterpret_cast<uint8*>(&value), sizeof(type)); return value; }
public:
DECL_READWRITE(int8, Int8)
DECL_READWRITE(uint8, UInt8)
DECL_READWRITE(int16, Int16)
@@ -105,8 +101,8 @@ public:
DECL_READWRITE(float, Single)
DECL_READWRITE(double, Double)
DECL_READWRITE(bool, Boolean)
#undef DECL_READWRITE
public:
/// <summary>
/// Writes data of type Vector2 into the message.
/// </summary>
@@ -180,7 +176,6 @@ public:
return Quaternion(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
}
public:
/// <summary>
/// Writes data of type String into the message. UTF-16 encoded.
/// </summary>
@@ -221,6 +216,7 @@ public:
}
public:
/// <summary>
/// Returns true if the message is valid for reading or writing.
/// </summary>
@@ -229,3 +225,9 @@ public:
return Buffer != nullptr && BufferSize > 0;
}
};
template<>
struct TIsPODType<NetworkMessage>
{
enum { Value = true };
};

View File

@@ -1,6 +1,5 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System.IO;
using Flax.Build;
using Flax.Build.NativeCpp;
@@ -26,7 +25,6 @@ public class ENet : DepsModule
{
base.Setup(options);
var depsRoot = options.DepsFolder;
options.PublicDefinitions.Add("ENET");
}
}