Refactor Network api, add more dcos, expose it to scripting
This commit is contained in:
@@ -4,50 +4,65 @@
|
||||
|
||||
#include "Engine/Core/Types/BaseTypes.h"
|
||||
#include "Engine/Core/Types/String.h"
|
||||
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/Network.h\"");
|
||||
|
||||
#define SOCKGROUP_ITEMSIZE 16
|
||||
|
||||
enum class FLAXENGINE_API NetworkProtocol
|
||||
/// <summary>
|
||||
/// Network connection protocol type.
|
||||
/// </summary>
|
||||
API_ENUM() enum class NetworkProtocol
|
||||
{
|
||||
/// <summary>Not specified.</summary>
|
||||
Undefined,
|
||||
/// <summary>User Datagram Protocol.</summary>
|
||||
Udp,
|
||||
/// <summary>Transmission Control Protocol.</summary>
|
||||
Tcp
|
||||
Tcp,
|
||||
};
|
||||
|
||||
enum class FLAXENGINE_API NetworkIPVersion
|
||||
/// <summary>
|
||||
/// IP version type.
|
||||
/// </summary>
|
||||
API_ENUM() enum class NetworkIPVersion
|
||||
{
|
||||
/// <summary>Not specified.</summary>
|
||||
Undefined,
|
||||
/// <summary>Internet Protocol version 4.</summary>
|
||||
IPv4,
|
||||
/// <summary>Internet Protocol version 6.</summary>
|
||||
IPv6
|
||||
IPv6,
|
||||
};
|
||||
|
||||
struct FLAXENGINE_API NetworkSocket
|
||||
/// <summary>
|
||||
/// Network socket.
|
||||
/// </summary>
|
||||
API_STRUCT() struct FLAXENGINE_API NetworkSocket
|
||||
{
|
||||
NetworkProtocol Protocol = NetworkProtocol::Undefined;
|
||||
NetworkIPVersion IPVersion = NetworkIPVersion::Undefined;
|
||||
byte Data[8] = {};
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkSocketGroup)
|
||||
|
||||
/// <summary>Socket protocol type.</summary>
|
||||
API_FIELD() NetworkProtocol Protocol = NetworkProtocol::Undefined;
|
||||
/// <summary>Socket address IP version.</summary>
|
||||
API_FIELD() NetworkIPVersion IPVersion = NetworkIPVersion::Undefined;
|
||||
API_FIELD(Private, NoArray) byte Data[8] = {};
|
||||
};
|
||||
|
||||
struct FLAXENGINE_API NetworkAddress
|
||||
/// <summary>
|
||||
/// Network end-point.
|
||||
/// </summary>
|
||||
API_STRUCT() struct FLAXENGINE_API NetworkEndPoint
|
||||
{
|
||||
String Address;
|
||||
String Port;
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkSocketGroup)
|
||||
|
||||
/// <summary>End-point IP version.</summary>
|
||||
API_FIELD(ReadOnly) NetworkIPVersion IPVersion = NetworkIPVersion::Undefined;
|
||||
API_FIELD(Private, NoArray) byte Data[28] = {};
|
||||
};
|
||||
|
||||
struct FLAXENGINE_API NetworkEndPoint
|
||||
{
|
||||
NetworkIPVersion IPVersion = NetworkIPVersion::Undefined;
|
||||
byte Data[28] = {};
|
||||
};
|
||||
|
||||
enum class FLAXENGINE_API NetworkSocketOption
|
||||
/// <summary>
|
||||
/// Network socket options.
|
||||
/// </summary>
|
||||
API_ENUM() enum class NetworkSocketOption
|
||||
{
|
||||
/// <summary>Enables debugging info recording.</summary>
|
||||
Debug,
|
||||
@@ -81,29 +96,53 @@ enum class FLAXENGINE_API NetworkSocketOption
|
||||
IPv6Only,
|
||||
/// <summary>Retrieve the current path MTU, the socket must be connected UDP/TCP.</summary>
|
||||
Mtu,
|
||||
// <summary>Socket type, DGRAM, STREAM ..</summary>
|
||||
Type
|
||||
/// <summary>Socket type, DGRAM, STREAM ..</summary>
|
||||
Type,
|
||||
};
|
||||
|
||||
struct FLAXENGINE_API NetworkSocketState
|
||||
/// <summary>
|
||||
/// Network socket state.
|
||||
/// </summary>
|
||||
API_ENUM() enum class NetworkSocketState
|
||||
{
|
||||
bool Error = false;
|
||||
bool Invalid = false;
|
||||
bool Disconnected = false;
|
||||
bool Readable = false;
|
||||
bool Writeable = false;
|
||||
/// <summary>Nothing.</summary>
|
||||
None = 0,
|
||||
/// <summary>Socket error.</summary>
|
||||
Error = 1 << 0,
|
||||
/// <summary>Invalid request.</summary>
|
||||
Invalid = 1 << 1,
|
||||
/// <summary>Socket disconnected.</summary>
|
||||
Disconnected = 1 << 2,
|
||||
/// <summary>Socket is readable.</summary>
|
||||
Readable = 1 << 3,
|
||||
/// <summary>Socket is writable.</summary>
|
||||
Writeable = 1 << 4,
|
||||
};
|
||||
|
||||
struct FLAXENGINE_API NetworkSocketGroup
|
||||
DECLARE_ENUM_OPERATORS(NetworkSocketState);
|
||||
|
||||
/// <summary>
|
||||
/// Network sockets group.
|
||||
/// </summary>
|
||||
API_STRUCT() struct FLAXENGINE_API NetworkSocketGroup
|
||||
{
|
||||
uint32 Count = 0;
|
||||
uint32 Capacity = 0;
|
||||
byte *Data;
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkSocketGroup)
|
||||
|
||||
/// <summary>Group size.</summary>
|
||||
API_FIELD(ReadOnly) uint32 Count = 0;
|
||||
/// <summary>Group capacity.</summary>
|
||||
API_FIELD(ReadOnly) uint32 Capacity = 0;
|
||||
API_FIELD(Private) byte* Data = nullptr;
|
||||
};
|
||||
|
||||
class FLAXENGINE_API NetworkBase
|
||||
/// <summary>
|
||||
/// Low-level networking implementation interface with Berkeley sockets.
|
||||
/// </summary>
|
||||
API_CLASS(Static, Name="Network") class FLAXENGINE_API NetworkBase
|
||||
{
|
||||
public:
|
||||
static struct FLAXENGINE_API ScriptingTypeInitializer TypeInitializer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new native socket.
|
||||
/// </summary>
|
||||
@@ -111,14 +150,14 @@ public:
|
||||
/// <param name="proto">The protocol.</param>
|
||||
/// <param name="ipv">The ip version.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool CreateSocket(NetworkSocket& socket, NetworkProtocol proto, NetworkIPVersion ipv);
|
||||
API_FUNCTION() static bool CreateSocket(API_PARAM(Ref) NetworkSocket& socket, NetworkProtocol proto, NetworkIPVersion ipv);
|
||||
|
||||
/// <summary>
|
||||
/// Closes native socket.
|
||||
/// </summary>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool DestroySocket(NetworkSocket& socket);
|
||||
API_FUNCTION() static bool DestroySocket(API_PARAM(Ref) NetworkSocket& socket);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the specified socket option.
|
||||
@@ -127,16 +166,7 @@ public:
|
||||
/// <param name="option">The option.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool SetSocketOption(NetworkSocket& socket, NetworkSocketOption option, bool value);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the specified socket option.
|
||||
/// </summary>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="option">The option.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool SetSocketOption(NetworkSocket& socket, NetworkSocketOption option, int32 value);
|
||||
API_FUNCTION() static bool SetSocketOption(API_PARAM(Ref) NetworkSocket& socket, NetworkSocketOption option, int32 value);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the specified socket option.
|
||||
@@ -145,16 +175,7 @@ public:
|
||||
/// <param name="option">The option.</param>
|
||||
/// <param name="value">The returned value.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool GetSocketOption(NetworkSocket& socket, NetworkSocketOption option, bool* value);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the specified socket option.
|
||||
/// </summary>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="option">The option.</param>
|
||||
/// <param name="value">The returned value.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool GetSocketOption(NetworkSocket& socket, NetworkSocketOption option, int32* value);
|
||||
API_FUNCTION() static bool GetSocketOption(API_PARAM(Ref) NetworkSocket& socket, NetworkSocketOption option, API_PARAM(Out) int32& value);
|
||||
|
||||
/// <summary>
|
||||
/// Connects a socket to the specified end point.
|
||||
@@ -162,7 +183,7 @@ public:
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="endPoint">The end point.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool ConnectSocket(NetworkSocket& socket, NetworkEndPoint& endPoint);
|
||||
API_FUNCTION() static bool ConnectSocket(API_PARAM(Ref) NetworkSocket& socket, API_PARAM(Ref) NetworkEndPoint& endPoint);
|
||||
|
||||
/// <summary>
|
||||
/// Binds a socket to the specified end point.
|
||||
@@ -170,7 +191,7 @@ public:
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="endPoint">The end point.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool BindSocket(NetworkSocket& socket, NetworkEndPoint& endPoint);
|
||||
API_FUNCTION() static bool BindSocket(API_PARAM(Ref) NetworkSocket& socket, API_PARAM(Ref) NetworkEndPoint& endPoint);
|
||||
|
||||
/// <summary>
|
||||
/// Listens for incoming connection.
|
||||
@@ -178,7 +199,7 @@ public:
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="queueSize">Pending connection queue size.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool Listen(NetworkSocket& socket, uint16 queueSize);
|
||||
API_FUNCTION() static bool Listen(API_PARAM(Ref) NetworkSocket& socket, uint16 queueSize);
|
||||
|
||||
/// <summary>
|
||||
/// Accepts a pending connection.
|
||||
@@ -187,21 +208,21 @@ public:
|
||||
/// <param name="newSocket">The newly connected socket.</param>
|
||||
/// <param name="newEndPoint">The end point of the new socket.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool Accept(NetworkSocket& serverSocket, NetworkSocket& newSocket, NetworkEndPoint& newEndPoint);
|
||||
API_FUNCTION() static bool Accept(API_PARAM(Ref) NetworkSocket& serverSocket, API_PARAM(Ref) NetworkSocket& newSocket, API_PARAM(Out) NetworkEndPoint& newEndPoint);
|
||||
|
||||
/// <summary>
|
||||
/// Checks for socket readability.
|
||||
/// </summary>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <returns>Returns true when data is available. Otherwise false.</returns>
|
||||
static bool IsReadable(NetworkSocket& socket);
|
||||
API_FUNCTION() static bool IsReadable(API_PARAM(Ref) NetworkSocket& socket);
|
||||
|
||||
/// <summary>
|
||||
/// Checks for socket writeability.
|
||||
/// </summary>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <returns>Returns true when data can be written. Otherwise false.</returns>
|
||||
static bool IsWritable(NetworkSocket& socket);
|
||||
API_FUNCTION() static bool IsWritable(API_PARAM(Ref) NetworkSocket& socket);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a socket group. It allocate memory based on the desired capacity.
|
||||
@@ -209,21 +230,21 @@ public:
|
||||
/// <param name="capacity">The group capacity (fixed).</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool CreateSocketGroup(uint32 capacity, NetworkSocketGroup& group);
|
||||
API_FUNCTION() static bool CreateSocketGroup(uint32 capacity, API_PARAM(Out) NetworkSocketGroup& group);
|
||||
|
||||
/// <summary>
|
||||
/// Destroy the socket group, and free the allocated memory.
|
||||
/// </summary>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <returns>Returns true if the group is already destroyed, otherwise false.</returns>
|
||||
static bool DestroySocketGroup(NetworkSocketGroup& group);
|
||||
|
||||
API_FUNCTION() static bool DestroySocketGroup(API_PARAM(Ref) NetworkSocketGroup& group);
|
||||
|
||||
/// <summary>
|
||||
/// Updates sockets states.
|
||||
/// </summary>
|
||||
/// <param name="group">The sockets group.</param>
|
||||
/// <returns>Returns -1 on error, The number of elements where states are nonzero, otherwise 0.</returns>
|
||||
static int32 Poll(NetworkSocketGroup& group);
|
||||
API_FUNCTION() static int32 Poll(API_PARAM(Ref) NetworkSocketGroup& group);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves socket state.
|
||||
@@ -232,7 +253,7 @@ public:
|
||||
/// <param name="index">The socket index in group.</param>
|
||||
/// <param name="state">The returned state.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool GetSocketState(NetworkSocketGroup& group, uint32 index, NetworkSocketState& state);
|
||||
API_FUNCTION() static bool GetSocketState(API_PARAM(Ref) NetworkSocketGroup& group, uint32 index, API_PARAM(Out) NetworkSocketState& state);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a socket to a group.
|
||||
@@ -240,24 +261,23 @@ public:
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <returns>Returns the socket index in group or -1 on error.</returns>
|
||||
static int32 AddSocketToGroup(NetworkSocketGroup& group, NetworkSocket& socket);
|
||||
API_FUNCTION() static int32 AddSocketToGroup(API_PARAM(Ref) NetworkSocketGroup& group, API_PARAM(Ref) NetworkSocket& socket);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a socket by index.
|
||||
/// Some data like socket IPVersion might be undefined.
|
||||
/// Gets a socket by index. Some data like socket IPVersion might be undefined.
|
||||
/// </summary>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="index">The index.</param>
|
||||
/// <param name="socket">The returned socket.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool GetSocketFromGroup(NetworkSocketGroup& group, uint32 index, NetworkSocket* socket);
|
||||
API_FUNCTION() static bool GetSocketFromGroup(API_PARAM(Ref) NetworkSocketGroup& group, uint32 index, API_PARAM(Out) NetworkSocket* socket);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the socket at the specified index.
|
||||
/// </summary>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="index">The index.</param>
|
||||
static void RemoveSocketFromGroup(NetworkSocketGroup& group, uint32 index);
|
||||
API_FUNCTION() static void RemoveSocketFromGroup(API_PARAM(Ref) NetworkSocketGroup& group, uint32 index);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the socket if present.
|
||||
@@ -265,13 +285,13 @@ public:
|
||||
/// <param name="group">The group.</param>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <returns>Returns true if the socket is not found, otherwise false.</returns>
|
||||
static bool RemoveSocketFromGroup(NetworkSocketGroup& group, NetworkSocket& socket);
|
||||
|
||||
API_FUNCTION() static bool RemoveSocketFromGroup(API_PARAM(Ref) NetworkSocketGroup& group, API_PARAM(Ref) NetworkSocket& socket);
|
||||
|
||||
/// <summary>
|
||||
/// Clears the socket group.
|
||||
/// </summary>
|
||||
/// <param name="group">The group.</param>
|
||||
static void ClearGroup(NetworkSocketGroup& group);
|
||||
API_FUNCTION() static void ClearGroup(API_PARAM(Ref) NetworkSocketGroup& group);
|
||||
|
||||
/// <summary>
|
||||
/// Writes data to the socket.
|
||||
@@ -279,9 +299,9 @@ public:
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="data">The data to write.</param>
|
||||
/// <param name="length">The length of data.</param>
|
||||
/// <param name="endPoint">If protocol is UDP , the destination end point. Otherwise nullptr.</param>
|
||||
/// <param name="endPoint">If protocol is UDP, the destination end point. Otherwise nullptr.</param>
|
||||
/// <returns>Returns -1 on error, otherwise bytes written.</returns>
|
||||
static int32 WriteSocket(NetworkSocket socket, byte* data, uint32 length, NetworkEndPoint* endPoint = nullptr);
|
||||
API_FUNCTION() static int32 WriteSocket(NetworkSocket socket, byte* data, uint32 length, NetworkEndPoint* endPoint = nullptr);
|
||||
|
||||
/// <summary>
|
||||
/// Reads data on the socket.
|
||||
@@ -291,22 +311,23 @@ public:
|
||||
/// <param name="bufferSize">Size of the buffer.</param>
|
||||
/// <param name="endPoint">If UDP, the end point from where data is coming. Otherwise nullptr.</param>
|
||||
/// <returns>Returns -1 on error, otherwise bytes read.</returns>
|
||||
static int32 ReadSocket(NetworkSocket socket, byte* buffer, uint32 bufferSize, NetworkEndPoint* endPoint = nullptr);
|
||||
API_FUNCTION() static int32 ReadSocket(NetworkSocket socket, byte* buffer, uint32 bufferSize, NetworkEndPoint* endPoint = nullptr);
|
||||
|
||||
/// <summary>
|
||||
/// Creates an end point.
|
||||
/// </summary>
|
||||
/// <param name="address">The address.</param>
|
||||
/// <param name="address">The network address.</param>
|
||||
/// <param name="port">The network port.</param>
|
||||
/// <param name="ipv">The ip version.</param>
|
||||
/// <param name="endPoint">The created end point.</param>
|
||||
/// <param name="bindable">True if the end point will be connected or binded.</param>
|
||||
/// <returns>Returns true on error, otherwise false.</returns>
|
||||
static bool CreateEndPoint(NetworkAddress& address, NetworkIPVersion ipv, NetworkEndPoint& endPoint, bool bindable = true);
|
||||
API_FUNCTION() static bool CreateEndPoint(const String& address, const String& port, NetworkIPVersion ipv, API_PARAM(Out) NetworkEndPoint& endPoint, bool bindable = true);
|
||||
|
||||
/// <summary>
|
||||
/// Remaps an ipv4 end point to an ipv6 one.
|
||||
/// </summary>
|
||||
/// <param name="endPoint">The ipv4 end point.</param>
|
||||
/// <returns>The ipv6 end point.</returns>
|
||||
static NetworkEndPoint RemapEndPointToIPv6(NetworkEndPoint& endPoint);
|
||||
API_FUNCTION() static NetworkEndPoint RemapEndPointToIPv6(API_PARAM(Ref) NetworkEndPoint& endPoint);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user