Add XML comments
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
#include "Engine/Core/Collections/Dictionary.h"
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
|
||||
/// <summary>
|
||||
/// Low-level network transport interface implementation based on ENet library.
|
||||
/// </summary>
|
||||
API_CLASS(Namespace="FlaxEngine.Networking", Sealed) class FLAXENGINE_API ENetDriver : public INetworkDriver
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(ENetDriver);
|
||||
|
||||
@@ -1,26 +1,98 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
#include "Types.h"
|
||||
|
||||
/// <summary>
|
||||
/// Basic interface for the low-level network transport/driver.
|
||||
/// </summary>
|
||||
API_INTERFACE(Namespace="FlaxEngine.Networking") class FLAXENGINE_API INetworkDriver
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(INetworkDriver);
|
||||
public:
|
||||
/// <summary>
|
||||
/// Initializes the instance of this network driver using given configuration.
|
||||
/// </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>
|
||||
virtual void Initialize(NetworkPeer* host, const NetworkConfig& config) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Disposes this driver making it no longer usable.
|
||||
/// Reserved for resource deallocation etc.
|
||||
/// </summary>
|
||||
virtual void Dispose() = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Starts listening for incoming connections.
|
||||
/// Once this is called, this driver becomes a server.
|
||||
/// </summary>
|
||||
/// <returns>True when succeeded.</returns>
|
||||
virtual bool Listen() = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Starts connection handshake with the end point specified in the <seealso cref="NetworkConfig"/> structure.
|
||||
/// Once this is called, this driver becomes a client.
|
||||
/// </summary>
|
||||
/// <returns>True when succeeded.</returns>
|
||||
virtual bool Connect() = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects from the server.
|
||||
/// </summary>
|
||||
/// <remarks>Can be used only by the client!</remarks>
|
||||
virtual void Disconnect() = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects given connection from the server.
|
||||
/// </summary>
|
||||
/// <remarks>Can be used only by the server!</remarks>
|
||||
virtual void Disconnect(const NetworkConnection& connection) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to pop an network event from the queue.
|
||||
/// </summary>
|
||||
/// <param name="eventPtr">The pointer to event structure.</param>
|
||||
/// <returns>True when succeeded and the event can be processed.</returns>
|
||||
virtual bool PopEvent(NetworkEvent* eventPtr) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Sends given message over specified channel to the server.
|
||||
/// </summary>
|
||||
/// <param name="channelType">The channel to send the message over.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <remarks>Can be used only by the client!</remarks>
|
||||
/// <remarks>
|
||||
/// Do not recycle the message after calling this.
|
||||
/// This function automatically recycles the message.
|
||||
virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Sends given message over specified channel to the given client connection (target).
|
||||
/// </summary>
|
||||
/// <param name="channelType">The channel to send the message over.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="target">The client connection to send the message to.</param>
|
||||
/// <remarks>Can be used only by the server!</remarks>
|
||||
/// <remarks>
|
||||
/// Do not recycle the message after calling this.
|
||||
/// This function automatically recycles the message.
|
||||
/// </remarks>
|
||||
virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, NetworkConnection target) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Sends given message over specified channel to the given client connection (target).
|
||||
/// </summary>
|
||||
/// <param name="channelType">The channel to send the message over.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="targets">The connections list to send the message to.</param>
|
||||
/// <remarks>Can be used only by the server!</remarks>
|
||||
/// <remarks>
|
||||
/// Do not recycle the message after calling this.
|
||||
/// This function automatically recycles the message.
|
||||
/// </remarks>
|
||||
virtual void SendMessage(NetworkChannelType channelType, const NetworkMessage& message, Array<NetworkConnection, HeapAllocation> targets) = 0;
|
||||
|
||||
// TODO: Stats API
|
||||
|
||||
@@ -1,16 +1,40 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
|
||||
/// <summary>
|
||||
/// The low-level network channel type for message sending.
|
||||
/// </summary>
|
||||
API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkChannelType
|
||||
{
|
||||
/// <summary>
|
||||
/// Invalid channel type.
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Unreliable channel type.
|
||||
/// Messages can be lost or arrive out-of-order.
|
||||
/// </summary>
|
||||
Unreliable,
|
||||
|
||||
/// <summary>
|
||||
/// Unreliable-ordered channel type.
|
||||
/// Messages can be lost but always arrive in order.
|
||||
/// </summary>
|
||||
UnreliableOrdered,
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reliable channel type.
|
||||
/// Messages won't be lost but may arrive out-of-order.
|
||||
/// </summary>
|
||||
Reliable,
|
||||
|
||||
/// <summary>
|
||||
/// Reliable-ordered channel type.
|
||||
/// Messages won't be lost and always arrive in order.
|
||||
/// </summary>
|
||||
ReliableOrdered
|
||||
};
|
||||
|
||||
@@ -1,32 +1,51 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Platform/Network.h"
|
||||
|
||||
API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkTransportType
|
||||
/// <summary>
|
||||
/// Network driver implementations enum.
|
||||
/// </summary>
|
||||
API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkDriverType
|
||||
{
|
||||
/// <summary>
|
||||
/// Invalid network driver implementation.
|
||||
/// </summary>
|
||||
Undefined = 0,
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ENet library based network driver implementation.
|
||||
/// </summary>
|
||||
ENet
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Low-level network configuration structure. Provides settings for the network driver and all internal components.
|
||||
/// </summary>
|
||||
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.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
NetworkTransportType NetworkDriverType = NetworkTransportType::ENet;
|
||||
// TODO: Expose INetworkDriver as a ref not enum, when C++/C# interfaces are done.
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// The upper limit on how many peers can join when we're listening.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
uint16 ConnectionsLimit = 32;
|
||||
|
||||
/// <summary>
|
||||
/// Address used to connect to or listen at.
|
||||
/// Set it to "any" when you want to listen at all available addresses.
|
||||
/// </summary>
|
||||
/// <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");
|
||||
@@ -37,9 +56,20 @@ public:
|
||||
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; // MTU
|
||||
uint16 MessageSize = 1500;
|
||||
|
||||
/// <summary>
|
||||
/// The amount of pooled messages that can be used at once (receiving and sending!).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Creating more messages than this limit will result in a crash!
|
||||
/// This should be tweaked manually to fit the needs (adjusting this value will increase/decrease memory usage)!
|
||||
/// </remarks>
|
||||
API_FIELD()
|
||||
uint16 MessagePoolSize = 2048; // (RX and TX)
|
||||
uint16 MessagePoolSize = 2048;
|
||||
};
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
|
||||
/// <summary>
|
||||
/// Network connection structure - used to identify connected peers when we're listening.
|
||||
/// </summary>
|
||||
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkConnection
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkConnection);
|
||||
public:
|
||||
API_FIELD();
|
||||
/// <summary>
|
||||
/// The identifier of the connection.
|
||||
/// </summary>
|
||||
/// <remarks>Used by network driver implementations.</remarks>
|
||||
API_FIELD()
|
||||
uint32 ConnectionId;
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
@@ -6,26 +6,60 @@
|
||||
#include "NetworkMessage.h"
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
|
||||
/// <summary>
|
||||
/// Network event type enum contains all possible events that can be returned by PopEvent function.
|
||||
/// </summary>
|
||||
API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkEventType
|
||||
{
|
||||
/// <summary>
|
||||
/// Invalid network event type.
|
||||
/// </summary>
|
||||
Undefined = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Event "connected" - client connected to our server or we've connected to the server.
|
||||
/// </summary>
|
||||
Connected,
|
||||
|
||||
/// <summary>
|
||||
/// Event "disconnected" - client disconnected from our server or we've been kicked from the server.
|
||||
/// </summary>
|
||||
Disconnected,
|
||||
|
||||
/// <summary>
|
||||
/// Event "disconnected" - client got a timeout from our server or we've list the connection to the server.
|
||||
/// </summary>
|
||||
Timeout,
|
||||
|
||||
/// <summary>
|
||||
/// Event "message" - message received from some client or the server.
|
||||
/// </summary>
|
||||
Message
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Network event structure that wraps all data needed to identify and process it.
|
||||
/// </summary>
|
||||
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkEvent
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkEvent);
|
||||
public:
|
||||
/// <summary>
|
||||
/// The type of the received event.
|
||||
/// </summary>
|
||||
API_FIELD();
|
||||
NetworkEventType EventType;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The message when this event is an "message" event - not valid in any other cases.
|
||||
/// </summary>
|
||||
API_FIELD();
|
||||
NetworkMessage Message;
|
||||
|
||||
/// <summary>
|
||||
/// The connected of the client that has sent message, connected, disconnected or got a timeout.
|
||||
/// </summary>
|
||||
/// <remarks>Only valid when event has been received on server-peer.</remarks>
|
||||
API_FIELD();
|
||||
NetworkConnection Sender;
|
||||
};
|
||||
|
||||
@@ -1,17 +1,30 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// 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);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "NetworkMessage.h"
|
||||
|
||||
@@ -15,3 +15,8 @@ void NetworkMessage::ReadBytes(uint8* bytes, const int numBytes)
|
||||
Platform::MemoryCopy(bytes, Buffer + Position, numBytes);
|
||||
Position += numBytes;
|
||||
}
|
||||
|
||||
bool NetworkMessage::IsValid() const
|
||||
{
|
||||
return Buffer != nullptr && BufferSize > 0;
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ namespace FlaxEngine.Networking
|
||||
/// Writes raw bytes into the message.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes that will be written.</param>
|
||||
/// <param name="length">The amount of bytes to write from the <see cref="bytes"/> pointer.</param>
|
||||
/// <param name="length">The amount of bytes to write from the bytes pointer.</param>
|
||||
public void WriteBytes(byte* bytes, int length)
|
||||
{
|
||||
Assert.IsTrue(Position + length <= BufferSize, $"Could not write data of length {length} into message with id={MessageId}! Current write position={Position}");
|
||||
Utils.MemoryCopy(new IntPtr(bytes), new IntPtr(Buffer + Position), length);
|
||||
Utils.MemoryCopy(new IntPtr(bytes), new IntPtr(Buffer + Position), (ulong)length);
|
||||
Position += (uint)length;
|
||||
Length = Position;
|
||||
}
|
||||
@@ -26,13 +26,13 @@ namespace FlaxEngine.Networking
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// The buffer pointer that will be used to store the bytes.
|
||||
/// Should be of the same length as <see cref="length"/> or longer.
|
||||
/// Should be of the same length as length or longer.
|
||||
/// </param>
|
||||
/// <param name="length">The minimal amount of bytes that the <see cref="buffer"/> contains.</param>
|
||||
/// <param name="length">The minimal amount of bytes that the buffer contains.</param>
|
||||
public void ReadBytes(byte* buffer, int length)
|
||||
{
|
||||
Assert.IsTrue(Position + length <= Length, $"Could not read data of length {length} from message with id={MessageId} and size of {Length}B! Current read position={Position}");
|
||||
Utils.MemoryCopy(new IntPtr(Buffer + Position), new IntPtr(buffer), length);
|
||||
Utils.MemoryCopy(new IntPtr(Buffer + Position), new IntPtr(buffer), (ulong)length);
|
||||
Position += (uint)length;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace FlaxEngine.Networking
|
||||
/// Writes raw bytes into the message.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes that will be written.</param>
|
||||
/// <param name="length">The amount of bytes to write from the <see cref="bytes"/> array.</param>
|
||||
/// <param name="length">The amount of bytes to write from the bytes array.</param>
|
||||
public void WriteBytes(byte[] bytes, int length)
|
||||
{
|
||||
fixed (byte* bytesPtr = bytes)
|
||||
@@ -54,9 +54,9 @@ namespace FlaxEngine.Networking
|
||||
/// </summary>
|
||||
/// <param name="buffer">
|
||||
/// The buffer that will be used to store the bytes.
|
||||
/// Should be of the same length as <see cref="length"/> or longer.
|
||||
/// Should be of the same length as length or longer.
|
||||
/// </param>
|
||||
/// <param name="length">The minimal amount of bytes that the <see cref="buffer"/> contains.</param>
|
||||
/// <param name="length">The minimal amount of bytes that the buffer contains.</param>
|
||||
public void ReadBytes(byte[] buffer, int length)
|
||||
{
|
||||
fixed (byte* bufferPtr = buffer)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
@@ -9,28 +9,52 @@
|
||||
#include "Engine/Core/Types/String.h"
|
||||
#include "Engine/Scripting/ScriptingType.h"
|
||||
|
||||
/// <summary>
|
||||
/// Network message structure. Provides raw data writing and reading to the message buffer.
|
||||
/// </summary>
|
||||
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkMessage
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkMessage);
|
||||
public:
|
||||
/// <summary>
|
||||
/// The raw message buffer.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
uint8* Buffer = nullptr;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The unique, internal message identifier.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
uint32 MessageId = 0;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the buffer that this message has.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
uint32 BufferSize = 0;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The length in bytes of this message.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
uint32 Length = 0;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The position in bytes in buffer where the next read/write will occur.
|
||||
/// </summary>
|
||||
API_FIELD()
|
||||
uint32 Position = 0;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Initializes default values of the <seealso cref="NetworkMessage"/> structure.
|
||||
/// </summary>
|
||||
NetworkMessage() = default;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes values of the <seealso cref="NetworkMessage"/> structure.
|
||||
/// </summary>
|
||||
NetworkMessage(uint8* buffer, uint32 messageId, uint32 bufferSize, uint32 length, uint32 position) :
|
||||
Buffer(buffer), MessageId(messageId), BufferSize(bufferSize), Length(length), Position(position)
|
||||
{ }
|
||||
@@ -38,7 +62,21 @@ public:
|
||||
~NetworkMessage() = default;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Writes raw bytes into the message.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes that will be written.</param>
|
||||
/// <param name="numBytes">The amount of bytes to write from the bytes pointer.</param>
|
||||
FORCE_INLINE void WriteBytes(uint8* bytes, int numBytes);
|
||||
|
||||
/// <summary>
|
||||
/// Reads raw bytes from the message into the given byte array.
|
||||
/// </summary>
|
||||
/// <param name="bytes">
|
||||
/// The buffer pointer that will be used to store the bytes.
|
||||
/// Should be of the same length as length or longer.
|
||||
/// </param>
|
||||
/// <param name="numBytes">The minimal amount of bytes that the buffer contains.</param>
|
||||
FORCE_INLINE void ReadBytes(uint8* bytes, int numBytes);
|
||||
|
||||
#define DECL_READWRITE(type, name) \
|
||||
@@ -59,17 +97,26 @@ public:
|
||||
DECL_READWRITE(bool, Boolean)
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Writes data of type <see cref="Vector2"/> into the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE void WriteVector2(const Vector2& value)
|
||||
{
|
||||
WriteSingle(value.X);
|
||||
WriteSingle(value.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads and returns data of type <see cref="Vector2"/> from the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE Vector2 ReadVector2()
|
||||
{
|
||||
return Vector2(ReadSingle(), ReadSingle());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes data of type <see cref="Vector3"/> into the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE void WriteVector3(const Vector3& value)
|
||||
{
|
||||
WriteSingle(value.X);
|
||||
@@ -77,11 +124,17 @@ public:
|
||||
WriteSingle(value.Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads and returns data of type <see cref="Vector3"/> from the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE Vector3 ReadVector3()
|
||||
{
|
||||
return Vector3(ReadSingle(), ReadSingle(), ReadSingle());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes data of type <see cref="Vector4"/> into the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE void WriteVector4(const Vector4& value)
|
||||
{
|
||||
WriteSingle(value.X);
|
||||
@@ -90,11 +143,17 @@ public:
|
||||
WriteSingle(value.W);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads and returns data of type <see cref="Vector4"/> from the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE Vector4 ReadVector4()
|
||||
{
|
||||
return Vector4(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes data of type <see cref="Quaternion"/> into the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE void WriteQuaternion(const Quaternion& value)
|
||||
{
|
||||
WriteSingle(value.X);
|
||||
@@ -103,6 +162,9 @@ public:
|
||||
WriteSingle(value.W);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads and returns data of type <see cref="Quaternion"/> from the message.
|
||||
/// </summary>
|
||||
FORCE_INLINE Quaternion ReadQuaternion()
|
||||
{
|
||||
return Quaternion(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
|
||||
@@ -125,8 +187,8 @@ public:
|
||||
}
|
||||
|
||||
public:
|
||||
bool IsValid() const
|
||||
{
|
||||
return Buffer != nullptr && BufferSize > 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns true if the message is valid for reading or writing.
|
||||
/// </summary>
|
||||
bool IsValid() const;
|
||||
};
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "NetworkPeer.h"
|
||||
#include "NetworkEvent.h"
|
||||
|
||||
|
||||
#include "Drivers/ENetDriver.h"
|
||||
|
||||
#include "Engine/Core/Log.h"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
@@ -9,7 +9,10 @@
|
||||
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
|
||||
API_CLASS(sealed, NoSpawn, Namespace="FlaxEngine.Networking") class FLAXENGINE_API NetworkPeer final : public PersistentScriptingObject
|
||||
/// <summary>
|
||||
/// Low-level network peer class. Provides server-client communication functions, message processing and sending.
|
||||
/// </summary>
|
||||
API_CLASS(sealed, NoSpawn, Namespace = "FlaxEngine.Networking") class FLAXENGINE_API NetworkPeer final : public PersistentScriptingObject
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_NO_SPAWN(NetworkHost);
|
||||
friend class NetworkManager;
|
||||
@@ -22,6 +25,9 @@ public:
|
||||
Array<uint32, HeapAllocation> MessagePool;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NetworkPeer"/> class.
|
||||
/// </summary>
|
||||
NetworkPeer() : PersistentScriptingObject(SpawnParams(Guid::New(), TypeInitializer))
|
||||
{
|
||||
}
|
||||
@@ -35,39 +41,112 @@ private:
|
||||
void DisposeMessageBuffers();
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Starts listening for incoming connections.
|
||||
/// Once this is called, this peer becomes a server.
|
||||
/// </summary>
|
||||
/// <returns>True when succeeded.</returns>
|
||||
API_FUNCTION()
|
||||
bool Listen();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Starts connection handshake with the end point specified in the <seealso cref="NetworkConfig"/> structure.
|
||||
/// Once this is called, this peer becomes a client.
|
||||
/// </summary>
|
||||
/// <returns>True when succeeded.</returns>
|
||||
API_FUNCTION()
|
||||
bool Connect();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects from the server.
|
||||
/// </summary>
|
||||
/// <remarks>Can be used only by the client!</remarks>
|
||||
API_FUNCTION()
|
||||
void Disconnect();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects given connection from the server.
|
||||
/// </summary>
|
||||
/// <remarks>Can be used only by the server!</remarks>
|
||||
API_FUNCTION()
|
||||
void Disconnect(const NetworkConnection& connection);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Tries to pop an network event from the queue.
|
||||
/// </summary>
|
||||
/// <param name="eventRef">The reference to event structure.</param>
|
||||
/// <returns>True when succeeded and the event can be processed.</returns>
|
||||
API_FUNCTION()
|
||||
bool PopEvent(API_PARAM(out) NetworkEvent& eventRef);
|
||||
|
||||
/// <summary>
|
||||
/// Acquires new message from the pool.
|
||||
/// Cannot acquire more messages than the limit specified in the <seealso cref="NetworkConfig"/> structure.
|
||||
/// </summary>
|
||||
/// <returns>The acquired message.</returns>
|
||||
/// <remarks>Make sure to recycle the message to this peer once it is no longer needed!</remarks>
|
||||
API_FUNCTION()
|
||||
NetworkMessage CreateMessage();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns given message to the pool.
|
||||
/// </summary>
|
||||
/// <remarks>Make sure that this message belongs to the peer and has not been recycled already (debug build checks for this)!</remarks>
|
||||
API_FUNCTION()
|
||||
void RecycleMessage(const NetworkMessage& message);
|
||||
|
||||
/// <summary>
|
||||
/// Acquires new message from the pool and setups it for sending.
|
||||
/// </summary>
|
||||
/// <returns>The acquired message.</returns>
|
||||
API_FUNCTION()
|
||||
NetworkMessage BeginSendMessage();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Aborts given message send. This effectively deinitializes the message and returns it to the pool.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
API_FUNCTION()
|
||||
void AbortSendMessage(const NetworkMessage& message);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends given message over specified channel to the server.
|
||||
/// </summary>
|
||||
/// <param name="channelType">The channel to send the message over.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <remarks>Can be used only by the client!</remarks>
|
||||
/// <remarks>
|
||||
/// Do not recycle the message after calling this.
|
||||
/// This function automatically recycles the message.
|
||||
/// </remarks>
|
||||
API_FUNCTION()
|
||||
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends given message over specified channel to the given client connection (target).
|
||||
/// </summary>
|
||||
/// <param name="channelType">The channel to send the message over.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="target">The client connection to send the message to.</param>
|
||||
/// <remarks>Can be used only by the server!</remarks>
|
||||
/// <remarks>
|
||||
/// Do not recycle the message after calling this.
|
||||
/// This function automatically recycles the message.
|
||||
/// </remarks>
|
||||
API_FUNCTION()
|
||||
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, const NetworkConnection& target);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends given message over specified channel to the given client connection (target).
|
||||
/// </summary>
|
||||
/// <param name="channelType">The channel to send the message over.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="targets">The connections list to send the message to.</param>
|
||||
/// <remarks>Can be used only by the server!</remarks>
|
||||
/// <remarks>
|
||||
/// Do not recycle the message after calling this.
|
||||
/// This function automatically recycles the message.
|
||||
/// </remarks>
|
||||
API_FUNCTION()
|
||||
bool EndSendMessage(NetworkChannelType channelType, const NetworkMessage& message, Array<NetworkConnection, HeapAllocation> targets);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user