#651
This commit is contained in:
Wojtek Figat
2021-10-18 11:02:08 +02:00
parent c7f1a2a77e
commit 302ec73b18
3 changed files with 31 additions and 25 deletions

View File

@@ -4,7 +4,7 @@
#include "Engine/Platform/Network.h"
class PersistentScriptingObject;
class ScriptingObject;
/// <summary>
/// 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:
/// <summary>
/// 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;
/// <summary>
/// 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.
/// </summary>
/// <remarks>Object is managed by the created network peer (will be deleted on peer shutdown).</remarks>
API_FIELD()
PersistentScriptingObject* NetworkDriver = nullptr;
ScriptingObject* NetworkDriver;
public:
/// <summary>
/// The upper limit on how many peers can join when we're listening.
/// </summary>
@@ -55,21 +55,21 @@ public:
/// <remarks>Set it to "any" when you want to listen at all available addresses.</remarks>
/// <remarks>Only IPv4 is supported.</remarks>
API_FIELD()
String Address = String("127.0.0.1");
String Address = String(TEXT("127.0.0.1"));
/// <summary>
/// The port to connect to or listen at.
/// </summary>
API_FIELD()
uint16 Port = 7777;
/// <summary>
/// The size of a message buffer in bytes.
/// Should be lower than the MTU (maximal transmission unit) - typically 1500 bytes.
/// </summary>
API_FIELD()
uint16 MessageSize = 1500;
/// <summary>
/// The amount of pooled messages that can be used at once (receiving and sending!).
/// </summary>

View File

@@ -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<ENetDriver>();
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<INetworkDriver>(Config.NetworkDriver);
ASSERT(NetworkDriver);
NetworkDriver->Initialize(this, Config);
LOG(Info, "NetworkManager initialized using driver = {0}", NetworkDriver->DriverName());

View File

@@ -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"
/// <summary>
/// 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<uint32, HeapAllocation> MessagePool;
public:
/// <summary>
/// Initializes a new instance of the <see cref="NetworkPeer"/> class.
/// </summary>
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:
/// <summary>
/// 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<NetworkConnection, HeapAllocation>& targets);
public:
/// <summary>
/// Creates new peer using given configuration.
/// </summary>
@@ -167,8 +161,9 @@ public:
/// <param name="peer">The peer to destroy.</param>
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();
};