Add soft failing to NetworkPeer creation and INetworkDriver initialization

This commit is contained in:
Wojtek Figat
2021-10-18 11:50:03 +02:00
parent 8bdf417b17
commit 3838870b16
5 changed files with 70 additions and 31 deletions

View File

@@ -55,7 +55,7 @@ ENetDriver::ENetDriver(const SpawnParams& params)
{
}
void ENetDriver::Initialize(NetworkPeer* host, const NetworkConfig& config)
bool ENetDriver::Initialize(NetworkPeer* host, const NetworkConfig& config)
{
_networkHost = host;
_config = config;
@@ -64,9 +64,11 @@ void ENetDriver::Initialize(NetworkPeer* host, const NetworkConfig& config)
if (enet_initialize() != 0)
{
LOG(Error, "Failed to initialize ENet driver!");
return true;
}
LOG(Info, "Initialized ENet driver!");
LOG(Info, "Initialized ENet driver");
return false;
}
void ENetDriver::Dispose()

View File

@@ -20,7 +20,7 @@ public:
// [INetworkDriver]
String DriverName() override { return String("ENetDriver"); }
void Initialize(NetworkPeer* host, const NetworkConfig& config) override;
bool Initialize(NetworkPeer* host, const NetworkConfig& config) override;
void Dispose() override;
bool Listen() override;
bool Connect() override;

View File

@@ -32,7 +32,8 @@ public:
/// </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>
API_FUNCTION() virtual void Initialize(NetworkPeer* host, const NetworkConfig& config) = 0;
/// <returns>True if failed to initialize network driver, false otherwise.</returns>
API_FUNCTION() virtual bool Initialize(NetworkPeer* host, const NetworkConfig& config) = 0;
/// <summary>
/// Disposes this driver making it no longer usable.

View File

@@ -13,20 +13,43 @@ namespace
uint32 LastHostId = 0;
}
void NetworkPeer::Initialize(const NetworkConfig& config)
bool NetworkPeer::Initialize(const NetworkConfig& config)
{
Config = config;
if (NetworkDriver)
return true;
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);
ASSERT(Config.MessageSize > 32); // TODO: Adjust this, not sure what the lowest limit should be.
ASSERT(Config.MessagePoolSize > 128);
if (Config.NetworkDriver == nullptr)
{
LOG(Error, "Missing NetworkDriver");
return true;
}
if (Config.ConnectionsLimit <= 0)
{
LOG(Error, "Invalid ConnectionsLimit");
return true;
}
if (Config.MessageSize <= 32) // TODO: Adjust this, not sure what the lowest limit should be.
{
LOG(Error, "Invalid MessageSize");
return true;
}
if (Config.MessagePoolSize <= 128)
{
LOG(Error, "Invalid MessagePoolSize");
return true;
}
NetworkDriver = ToInterface<INetworkDriver>(Config.NetworkDriver);
if (!NetworkDriver)
{
LOG(Error, "NetworkDriver doesn't implement INetworkDriver interface");
return true;
}
// TODO: Dynamic message pool allocation
// Setup messages
@@ -38,11 +61,14 @@ void NetworkPeer::Initialize(const NetworkConfig& config)
MessagePool.Push(messageId);
// Setup network driver
NetworkDriver = ToInterface<INetworkDriver>(Config.NetworkDriver);
ASSERT(NetworkDriver);
NetworkDriver->Initialize(this, Config);
if (NetworkDriver->Initialize(this, Config))
{
LOG(Error, "Failed to initialize NetworkDriver");
return true;
}
LOG(Info, "NetworkManager initialized using driver = {0}", NetworkDriver->DriverName());
return false;
}
void NetworkPeer::Shutdown()
@@ -57,7 +83,7 @@ void NetworkPeer::Shutdown()
void NetworkPeer::CreateMessageBuffers()
{
ASSERT(MessageBuffer == nullptr);
const uint32 pageSize = Platform::GetCPUInfo().PageSize;
// Calculate total size in bytes
@@ -73,7 +99,7 @@ void NetworkPeer::CreateMessageBuffers()
void NetworkPeer::DisposeMessageBuffers()
{
ASSERT(MessageBuffer != nullptr);
Platform::FreePages(MessageBuffer);
MessageBuffer = nullptr;
}
@@ -121,7 +147,7 @@ void NetworkPeer::RecycleMessage(const NetworkMessage& message)
#ifdef BUILD_DEBUG
ASSERT(MessagePool.Contains(message.MessageId) == false);
#endif
// Return the message id
MessagePool.Push(message.MessageId);
}
@@ -140,7 +166,7 @@ void NetworkPeer::AbortSendMessage(const NetworkMessage& message)
bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const NetworkMessage& message)
{
ASSERT(message.IsValid());
NetworkDriver->SendMessage(channelType, message);
RecycleMessage(message);
@@ -150,7 +176,7 @@ bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const Net
bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const NetworkMessage& message, const NetworkConnection& target)
{
ASSERT(message.IsValid());
NetworkDriver->SendMessage(channelType, message, target);
RecycleMessage(message);
@@ -160,7 +186,7 @@ bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const Net
bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const NetworkMessage& message, const Array<NetworkConnection>& targets)
{
ASSERT(message.IsValid());
NetworkDriver->SendMessage(channelType, message, targets);
RecycleMessage(message);
@@ -170,18 +196,28 @@ bool NetworkPeer::EndSendMessage(const NetworkChannelType channelType, const Net
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);
if (config.Address != TEXT("any"))
{
NetworkEndPoint endPoint;
if (Network::CreateEndPoint(config.Address, String::Empty, NetworkIPVersion::IPv4, endPoint, false))
{
LOG(Error, "Invalid end point.");
return nullptr;
}
}
// Alloc new host
Peers.Add(New<NetworkPeer>());
NetworkPeer* host = Peers.Last();
NetworkPeer* host = New<NetworkPeer>();
host->HostId = LastHostId++;
// Initialize the host
host->Initialize(config);
if (host->Initialize(config))
{
Delete(host);
return nullptr;
}
Peers.Add(host);
return host;
}
@@ -191,6 +227,6 @@ void NetworkPeer::ShutdownPeer(NetworkPeer* peer)
peer->Shutdown();
peer->HostId = -1;
Peers.Remove(peer);
Delete(peer);
}

View File

@@ -151,7 +151,7 @@ public:
/// </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>
/// <remarks>Peer should be destroyed using <see cref="ShutdownPeer"/> once it is no longer in use. Returns null if failed to create a peer (eg. config is invalid).</remarks>
API_FUNCTION()
static NetworkPeer* CreatePeer(const NetworkConfig& config);
@@ -189,7 +189,7 @@ public:
private:
void Initialize(const NetworkConfig& config);
bool Initialize(const NetworkConfig& config);
void Shutdown();
void CreateMessageBuffers();
void DisposeMessageBuffers();