diff --git a/Source/Engine/Networking/NetworkConfig.h b/Source/Engine/Networking/NetworkConfig.h index 31f6a5d6f..6497043ab 100644 --- a/Source/Engine/Networking/NetworkConfig.h +++ b/Source/Engine/Networking/NetworkConfig.h @@ -4,7 +4,7 @@ #include "Engine/Platform/Network.h" -class PersistentScriptingObject; +class ScriptingObject; /// /// Network driver implementations enum. @@ -28,7 +28,7 @@ API_ENUM(Namespace="FlaxEngine.Networking") enum class DEPRECATED NetworkDriverT API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkConfig { DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkConfig); -public: + /// /// The network driver that will be used to create the peer. /// To allow two peers to connect, they must use the same host. @@ -37,12 +37,12 @@ public: DEPRECATED NetworkDriverType NetworkDriverType = NetworkDriverType::ENet; /// - /// The network driver instance that will be used to create and manage the peer, send and receive messages. + /// The network driver instance (implements INetworkDriver) that will be used to create and manage the peer, send and receive messages. /// + /// Object is managed by the created network peer (will be deleted on peer shutdown). API_FIELD() - PersistentScriptingObject* NetworkDriver = nullptr; + ScriptingObject* NetworkDriver; -public: /// /// The upper limit on how many peers can join when we're listening. /// @@ -55,21 +55,21 @@ public: /// Set it to "any" when you want to listen at all available addresses. /// Only IPv4 is supported. API_FIELD() - String Address = String("127.0.0.1"); + String Address = String(TEXT("127.0.0.1")); /// /// The port to connect to or listen at. /// API_FIELD() uint16 Port = 7777; - + /// /// The size of a message buffer in bytes. /// Should be lower than the MTU (maximal transmission unit) - typically 1500 bytes. /// API_FIELD() uint16 MessageSize = 1500; - + /// /// The amount of pooled messages that can be used at once (receiving and sending!). /// diff --git a/Source/Engine/Networking/NetworkPeer.cpp b/Source/Engine/Networking/NetworkPeer.cpp index 2e885e54d..9ac132910 100644 --- a/Source/Engine/Networking/NetworkPeer.cpp +++ b/Source/Engine/Networking/NetworkPeer.cpp @@ -2,9 +2,7 @@ #include "NetworkPeer.h" #include "NetworkEvent.h" - #include "Drivers/ENetDriver.h" - #include "Engine/Core/Log.h" #include "Engine/Core/Math/Math.h" #include "Engine/Platform/CPUInfo.h" @@ -19,6 +17,11 @@ void NetworkPeer::Initialize(const NetworkConfig& config) { Config = config; + PRAGMA_DISABLE_DEPRECATION_WARNINGS + if (Config.NetworkDriver == nullptr && Config.NetworkDriverType == NetworkDriverType::ENet) + Config.NetworkDriver = New(); + PRAGMA_ENABLE_DEPRECATION_WARNINGS + ASSERT(NetworkDriver == nullptr); ASSERT(Config.NetworkDriver != nullptr); ASSERT(Config.ConnectionsLimit > 0); @@ -36,6 +39,7 @@ void NetworkPeer::Initialize(const NetworkConfig& config) // Setup network driver NetworkDriver = ToInterface(Config.NetworkDriver); + ASSERT(NetworkDriver); NetworkDriver->Initialize(this, Config); LOG(Info, "NetworkManager initialized using driver = {0}", NetworkDriver->DriverName()); diff --git a/Source/Engine/Networking/NetworkPeer.h b/Source/Engine/Networking/NetworkPeer.h index ba0347ffd..d69e6dc45 100644 --- a/Source/Engine/Networking/NetworkPeer.h +++ b/Source/Engine/Networking/NetworkPeer.h @@ -2,12 +2,11 @@ #pragma once -#include "Engine/Scripting/ScriptingType.h" -#include "Engine/Scripting/ScriptingObjectReference.h" #include "Types.h" #include "NetworkConfig.h" - #include "Engine/Core/Collections/Array.h" +#include "Engine/Scripting/ScriptingType.h" +#include "Engine/Scripting/ScriptingObjectReference.h" /// /// Low-level network peer class. Provides server-client communication functions, message processing and sending. @@ -17,6 +16,7 @@ API_CLASS(sealed, NoSpawn, Namespace = "FlaxEngine.Networking") class FLAXENGINE DECLARE_SCRIPTING_TYPE_NO_SPAWN(NetworkPeer); friend class NetworkManager; public: + int HostId = -1; NetworkConfig Config; INetworkDriver* NetworkDriver = nullptr; @@ -25,22 +25,17 @@ public: Array MessagePool; public: + /// /// Initializes a new instance of the class. /// - NetworkPeer() : PersistentScriptingObject(SpawnParams(Guid::New(), TypeInitializer)) + NetworkPeer() + : PersistentScriptingObject(SpawnParams(Guid::New(), TypeInitializer)) { } - -private: - void Initialize(const NetworkConfig& config); - void Shutdown(); - -private: - void CreateMessageBuffers(); - void DisposeMessageBuffers(); public: + /// /// Starts listening for incoming connections. /// Once this is called, this peer becomes a server. @@ -151,7 +146,6 @@ public: API_FUNCTION() bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const Array& targets); -public: /// /// Creates new peer using given configuration. /// @@ -167,8 +161,9 @@ public: /// The peer to destroy. API_FUNCTION() static void ShutdownPeer(NetworkPeer* peer); - + public: + bool IsValid() const { return NetworkDriver != nullptr && HostId >= 0; @@ -181,6 +176,7 @@ public: } public: + FORCE_INLINE bool operator==(const NetworkPeer& other) const { return HostId == other.HostId; @@ -190,5 +186,11 @@ public: { return HostId != other.HostId; } -}; +private: + + void Initialize(const NetworkConfig& config); + void Shutdown(); + void CreateMessageBuffers(); + void DisposeMessageBuffers(); +};