From 81bb322fd26cc5c99197efa945d9b6db90a351cf Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Perrier Date: Thu, 18 Feb 2021 22:41:05 +0100 Subject: [PATCH] Refactor NetworkSocketOption to support user defined size. --- Source/Engine/Platform/Base/NetworkBase.cpp | 10 ++++++++ Source/Engine/Platform/Base/NetworkBase.h | 19 +++++++++++++-- Source/Engine/Platform/Win32/Win32Network.cpp | 23 +++++++++++++++++++ Source/Engine/Platform/Win32/Win32Network.h | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Platform/Base/NetworkBase.cpp b/Source/Engine/Platform/Base/NetworkBase.cpp index 757b550f1..4060bfee6 100644 --- a/Source/Engine/Platform/Base/NetworkBase.cpp +++ b/Source/Engine/Platform/Base/NetworkBase.cpp @@ -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; diff --git a/Source/Engine/Platform/Base/NetworkBase.h b/Source/Engine/Platform/Base/NetworkBase.h index 066983de8..e5038835a 100644 --- a/Source/Engine/Platform/Base/NetworkBase.h +++ b/Source/Engine/Platform/Base/NetworkBase.h @@ -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 true when data can be written. Otherwise false. static bool IsWriteable(NetworkSocket& socket); + /// + /// Creates a socket group. It allocate memory based on the desired capacity. + /// + /// The group capacity (fixed). + /// The group. + /// Returns true on error, otherwise false. + static bool CreateSocketGroup(uint32 capacity, NetworkSocketGroup& group); + + /// + /// Destroy the socket group, and free the allocated memory. + /// + /// The group. + /// Returns true if the group is already destroyed, otherwise false. + static bool DestroySocketGroup(NetworkSocketGroup& group); + /// /// Updates sockets states. /// diff --git a/Source/Engine/Platform/Win32/Win32Network.cpp b/Source/Engine/Platform/Win32/Win32Network.cpp index b046eaa78..dd94901b3 100644 --- a/Source/Engine/Platform/Win32/Win32Network.cpp +++ b/Source/Engine/Platform/Win32/Win32Network.cpp @@ -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); diff --git a/Source/Engine/Platform/Win32/Win32Network.h b/Source/Engine/Platform/Win32/Win32Network.h index 060cbde32..b11ccf520 100644 --- a/Source/Engine/Platform/Win32/Win32Network.h +++ b/Source/Engine/Platform/Win32/Win32Network.h @@ -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);