diff --git a/Source/Engine/Networking/Drivers/ENetDriver.cpp b/Source/Engine/Networking/Drivers/ENetDriver.cpp index 6025c406a..5850fc499 100644 --- a/Source/Engine/Networking/Drivers/ENetDriver.cpp +++ b/Source/Engine/Networking/Drivers/ENetDriver.cpp @@ -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; diff --git a/Source/Engine/Networking/Drivers/ENetDriver.h b/Source/Engine/Networking/Drivers/ENetDriver.h index 15d775534..e951b6fb2 100644 --- a/Source/Engine/Networking/Drivers/ENetDriver.h +++ b/Source/Engine/Networking/Drivers/ENetDriver.h @@ -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" /// /// Low-level network transport interface implementation based on ENet library. /// -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& targets) override; private: + bool IsServer() const { return _host != nullptr && _peer == nullptr; } - + private: + NetworkConfig _config; NetworkPeer* _networkHost; void* _host = nullptr; void* _peer = nullptr; - Dictionary _peerMap; }; - diff --git a/Source/Engine/Networking/INetworkDriver.h b/Source/Engine/Networking/INetworkDriver.h index 1eff47861..2908f0d5f 100644 --- a/Source/Engine/Networking/INetworkDriver.h +++ b/Source/Engine/Networking/INetworkDriver.h @@ -2,8 +2,8 @@ #pragma once -#include "Engine/Scripting/ScriptingType.h" #include "Types.h" +#include "Engine/Scripting/ScriptingType.h" /// /// 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: - + /// /// Finalizes an instance of the class. /// virtual ~INetworkDriver() = default; -public: - /// /// Initializes the instance of this network driver using given configuration. /// /// The peer that this driver has been assigned to. /// The network config to use to configure this driver. - virtual void Initialize(NetworkPeer* host, const NetworkConfig& config) = 0; + API_FUNCTION() virtual void Initialize(NetworkPeer* host, const NetworkConfig& config) = 0; /// /// Disposes this driver making it no longer usable. /// Reserved for resource deallocation etc. /// - virtual void Dispose() = 0; - + API_FUNCTION() virtual void Dispose() = 0; + /// /// Starts listening for incoming connections. /// Once this is called, this driver becomes a server. /// /// True when succeeded. - virtual bool Listen() = 0; - + API_FUNCTION() virtual bool Listen() = 0; + /// /// Starts connection handshake with the end point specified in the structure. /// Once this is called, this driver becomes a client. /// /// True when succeeded. - virtual bool Connect() = 0; - + API_FUNCTION() virtual bool Connect() = 0; + /// /// Disconnects from the server. /// /// Can be used only by the client! - virtual void Disconnect() = 0; - + API_FUNCTION() virtual void Disconnect() = 0; + /// /// Disconnects given connection from the server. /// /// Can be used only by the server! - virtual void Disconnect(const NetworkConnection& connection) = 0; + API_FUNCTION() virtual void Disconnect(const NetworkConnection& connection) = 0; /// /// Tries to pop an network event from the queue. /// /// The pointer to event structure. /// True when succeeded and the event can be processed. - virtual bool PopEvent(NetworkEvent* eventPtr) = 0; - + API_FUNCTION() virtual bool PopEvent(NetworkEvent* eventPtr) = 0; + /// /// Sends given message over specified channel to the server. /// @@ -76,8 +73,9 @@ public: /// /// Do not recycle the message after calling this. /// This function automatically recycles the message. - virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) = 0; - + /// + API_FUNCTION() virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) = 0; + /// /// Sends given message over specified channel to the given client connection (target). /// @@ -89,8 +87,8 @@ public: /// Do not recycle the message after calling this. /// This function automatically recycles the message. /// - virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) = 0; - + API_FUNCTION() virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) = 0; + /// /// Sends given message over specified channel to the given client connection (target). /// @@ -102,9 +100,8 @@ public: /// Do not recycle the message after calling this. /// This function automatically recycles the message. /// - virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets) = 0; + API_FUNCTION() virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets) = 0; // TODO: Stats API // TODO: Simulation API - }; diff --git a/Source/Engine/Networking/NetworkMessage.h b/Source/Engine/Networking/NetworkMessage.h index f96bf73b5..001dc3e94 100644 --- a/Source/Engine/Networking/NetworkMessage.h +++ b/Source/Engine/Networking/NetworkMessage.h @@ -16,37 +16,34 @@ API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkMessa { DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkMessage); public: + /// /// The raw message buffer. /// - API_FIELD() - uint8* Buffer = nullptr; + API_FIELD() uint8* Buffer = nullptr; /// /// The unique, internal message identifier. /// - API_FIELD() - uint32 MessageId = 0; + API_FIELD() uint32 MessageId = 0; /// /// The size in bytes of the buffer that this message has. /// - API_FIELD() - uint32 BufferSize = 0; + API_FIELD() uint32 BufferSize = 0; /// /// The length in bytes of this message. /// - API_FIELD() - uint32 Length = 0; + API_FIELD() uint32 Length = 0; /// /// The position in bytes in buffer where the next read/write will occur. /// - API_FIELD() - uint32 Position = 0; + API_FIELD() uint32 Position = 0; public: + /// /// Initializes default values of the structure. /// @@ -55,13 +52,14 @@ public: /// /// Initializes values of the structure. /// - 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: + /// /// Writes raw bytes into the message. /// @@ -73,7 +71,7 @@ public: Platform::MemoryCopy(Buffer + Position, bytes, numBytes); Position += numBytes; } - + /// /// Reads raw bytes from the message into the given byte array. /// @@ -92,8 +90,6 @@ public: #define DECL_READWRITE(type, name) \ FORCE_INLINE void Write##name(type value) { WriteBytes(reinterpret_cast(&value), sizeof(type)); } \ FORCE_INLINE type Read##name() { type value = 0; ReadBytes(reinterpret_cast(&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: /// /// Writes data of type Vector2 into the message. /// @@ -180,7 +176,6 @@ public: return Quaternion(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle()); } -public: /// /// Writes data of type String into the message. UTF-16 encoded. /// @@ -221,6 +216,7 @@ public: } public: + /// /// Returns true if the message is valid for reading or writing. /// @@ -229,3 +225,9 @@ public: return Buffer != nullptr && BufferSize > 0; } }; + +template<> +struct TIsPODType +{ + enum { Value = true }; +}; diff --git a/Source/ThirdParty/enet/enet.Build.cs b/Source/ThirdParty/enet/enet.Build.cs index 83fa3161a..493abcc14 100644 --- a/Source/ThirdParty/enet/enet.Build.cs +++ b/Source/ThirdParty/enet/enet.Build.cs @@ -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"); } }