Refactor NetworkSocketOption to support user defined size.
This commit is contained in:
@@ -62,6 +62,16 @@ bool NetworkBase::IsWriteable(NetworkSocket& socket)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NetworkBase::CreateSocketGroup(uint32 capacity, NetworkSocketGroup& group)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NetworkBase::DestroySocketGroup(NetworkSocketGroup& group)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int32 NetworkBase::Poll(NetworkSocketGroup& group)
|
||||
{
|
||||
return -1;
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "Engine/Core/Types/String.h"
|
||||
API_INJECT_CPP_CODE("#include \"Engine/Platform/Network.h\"");
|
||||
|
||||
#define SOCKGROUP_MAXCOUNT 64
|
||||
#define SOCKGROUP_ITEMSIZE 16
|
||||
|
||||
enum class FLAXENGINE_API NetworkProtocol
|
||||
@@ -98,7 +97,8 @@ struct FLAXENGINE_API NetworkSocketState
|
||||
struct FLAXENGINE_API NetworkSocketGroup
|
||||
{
|
||||
uint32 Count = 0;
|
||||
byte Data[SOCKGROUP_MAXCOUNT * SOCKGROUP_ITEMSIZE] = {};
|
||||
uint32 Capacity = 0;
|
||||
byte *Data;
|
||||
};
|
||||
|
||||
class FLAXENGINE_API NetworkBase
|
||||
@@ -203,6 +203,21 @@ public:
|
||||
/// <returns>Returns true when data can be written. Otherwise false.</returns>
|
||||
static bool IsWriteable(NetworkSocket& socket);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a socket group. It allocate memory based on the desired capacity.
|
||||
/// </summary>
|
||||
/// <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);
|
||||
|
||||
/// <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);
|
||||
|
||||
/// <summary>
|
||||
/// Updates sockets states.
|
||||
/// </summary>
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
static_assert(sizeof NetworkSocket::Data >= sizeof SOCKET, "NetworkSocket::Data is not big enough to contains SOCKET !");
|
||||
static_assert(sizeof NetworkEndPoint::Data >= sizeof sockaddr_in6, "NetworkEndPoint::Data is not big enough to contains sockaddr_in6 !");
|
||||
static_assert(SOCKGROUP_ITEMSIZE >= sizeof(pollfd), "SOCKGROUP_ITEMSIZE macro is not big enough to contains pollfd !");
|
||||
|
||||
// @formatter:off
|
||||
static const IN6_ADDR v4MappedPrefix = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
@@ -322,6 +323,28 @@ bool Win32Network::IsWriteable(NetworkSocket& socket)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Win32Network::CreateSocketGroup(uint32 capacity, NetworkSocketGroup& group)
|
||||
{
|
||||
if (!(group.Data = (byte*)malloc(capacity * SOCKGROUP_ITEMSIZE)))
|
||||
{
|
||||
LOG(Error, "Unable to malloc NetworkSocketGroup::Data ! Size : {0}", capacity * SOCKGROUP_ITEMSIZE);
|
||||
return true;
|
||||
}
|
||||
group.Capacity = capacity;
|
||||
for(int i = 0; i < (int)group.Capacity; i++)
|
||||
((pollfd*)&group.Data[i * SOCKGROUP_ITEMSIZE])->fd = -1;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Win32Network::DestroySocketGroup(NetworkSocketGroup& group)
|
||||
{
|
||||
if (!group.Data)
|
||||
return true;
|
||||
free(group.Data);
|
||||
return false;
|
||||
}
|
||||
|
||||
int32 Win32Network::Poll(NetworkSocketGroup& group)
|
||||
{
|
||||
int32 pollret = WSAPoll((pollfd*)group.Data, group.Count, 0);
|
||||
|
||||
@@ -22,6 +22,8 @@ public:
|
||||
static bool Accept(NetworkSocket& serverSock, NetworkSocket& newSock, NetworkEndPoint& newEndPoint);
|
||||
static bool IsReadable(NetworkSocket& socket);
|
||||
static bool IsWriteable(NetworkSocket& socket);
|
||||
static bool CreateSocketGroup(uint32 capacity, NetworkSocketGroup& group);
|
||||
static bool DestroySocketGroup(NetworkSocketGroup& group);
|
||||
static int32 Poll(NetworkSocketGroup& group);
|
||||
static bool GetSocketState(NetworkSocketGroup& group, uint32 index, NetworkSocketState& state);
|
||||
static int32 AddSocketToGroup(NetworkSocketGroup& group, NetworkSocket& socket);
|
||||
|
||||
Reference in New Issue
Block a user