Remove NetworkManager and move peer creation/shutdown to NetworkPeer class
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "NetworkManager.h"
|
||||
|
||||
#include "NetworkMessage.h"
|
||||
#include "NetworkConfig.h"
|
||||
#include "NetworkPeer.h"
|
||||
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
Array<NetworkPeer*, HeapAllocation> Peers;
|
||||
uint32_t LastHostId = 0;
|
||||
}
|
||||
|
||||
NetworkPeer* NetworkManager::CreatePeer(const NetworkConfig& config)
|
||||
{
|
||||
// Validate the address for listen/connect
|
||||
NetworkEndPoint endPoint = {};
|
||||
const bool isValidEndPoint = NetworkBase::CreateEndPoint(config.Address, String("7777"), NetworkIPVersion::IPv4, endPoint, false);
|
||||
ASSERT(config.Address == String("any") || isValidEndPoint);
|
||||
|
||||
// Alloc new host
|
||||
Peers.Add(New<NetworkPeer>());
|
||||
NetworkPeer* host = Peers.Last();
|
||||
host->HostId = LastHostId++;
|
||||
|
||||
// Initialize the host
|
||||
host->Initialize(config);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
void NetworkManager::ShutdownPeer(NetworkPeer* peer)
|
||||
{
|
||||
ASSERT(peer->IsValid());
|
||||
peer->Shutdown();
|
||||
peer->HostId = -1;
|
||||
Peers.Remove(peer);
|
||||
|
||||
Delete(peer);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
#include "Types.h"
|
||||
|
||||
/// <summary>
|
||||
/// Low-level network service. Provides network peer management functionality.
|
||||
/// </summary>
|
||||
API_CLASS(Namespace="FlaxEngine.Networking", Static) class FLAXENGINE_API NetworkManager
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_NO_SPAWN(NetworkManager);
|
||||
public:
|
||||
/// <summary>
|
||||
/// Creates new peer using given configuration.
|
||||
/// </summary>
|
||||
/// <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.</remarks>
|
||||
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);
|
||||
|
||||
};
|
||||
@@ -9,6 +9,12 @@
|
||||
#include "Engine/Core/Math/Math.h"
|
||||
#include "Engine/Platform/CPUInfo.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
Array<NetworkPeer*, HeapAllocation> Peers;
|
||||
uint32_t LastHostId = 0;
|
||||
}
|
||||
|
||||
void NetworkPeer::Initialize(const NetworkConfig& config)
|
||||
{
|
||||
Config = config;
|
||||
@@ -156,3 +162,31 @@ bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const Net
|
||||
RecycleMessage(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
NetworkPeer* NetworkPeer::CreatePeer(const NetworkConfig& config)
|
||||
{
|
||||
// Validate the address for listen/connect
|
||||
NetworkEndPoint endPoint = {};
|
||||
const bool isValidEndPoint = NetworkBase::CreateEndPoint(config.Address, String("7777"), NetworkIPVersion::IPv4, endPoint, false);
|
||||
ASSERT(config.Address == String("any") || isValidEndPoint);
|
||||
|
||||
// Alloc new host
|
||||
Peers.Add(New<NetworkPeer>());
|
||||
NetworkPeer* host = Peers.Last();
|
||||
host->HostId = LastHostId++;
|
||||
|
||||
// Initialize the host
|
||||
host->Initialize(config);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
void NetworkPeer::ShutdownPeer(NetworkPeer* peer)
|
||||
{
|
||||
ASSERT(peer->IsValid());
|
||||
peer->Shutdown();
|
||||
peer->HostId = -1;
|
||||
Peers.Remove(peer);
|
||||
|
||||
Delete(peer);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
/// </summary>
|
||||
API_CLASS(sealed, NoSpawn, Namespace = "FlaxEngine.Networking") class FLAXENGINE_API NetworkPeer final : public PersistentScriptingObject
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_NO_SPAWN(NetworkHost);
|
||||
DECLARE_SCRIPTING_TYPE_NO_SPAWN(NetworkPeer);
|
||||
friend class NetworkManager;
|
||||
public:
|
||||
int HostId = -1;
|
||||
@@ -150,6 +150,23 @@ public:
|
||||
/// </remarks>
|
||||
API_FUNCTION()
|
||||
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, Array<NetworkConnection, HeapAllocation> targets);
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Creates new peer using given configuration.
|
||||
/// </summary>
|
||||
/// <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.</remarks>
|
||||
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);
|
||||
|
||||
public:
|
||||
bool IsValid() const
|
||||
|
||||
Reference in New Issue
Block a user