Network/TcpClient|UdpSocket: Add SendMultiple method

To efficiently merge multiples buffers into a reduced number of network
packets
This commit is contained in:
Lynix
2017-01-27 14:51:01 +01:00
parent 1d6f22cd8a
commit ab3b730d21
10 changed files with 201 additions and 11 deletions

View File

@@ -608,6 +608,53 @@ namespace Nz
return true;
}
bool SocketImpl::SendMultiple(SocketHandle handle, const NetBuffer* buffers, std::size_t bufferCount, const IpAddress& to, int* sent, SocketError* error)
{
NazaraAssert(handle != InvalidHandle, "Invalid handle");
NazaraAssert(buffers && bufferCount > 0, "Invalid buffers");
IpAddressImpl::SockAddrBuffer nameBuffer;
int bufferLength = IpAddressImpl::ToSockAddr(to, nameBuffer.data());
StackAllocation memory = NazaraStackAllocation(bufferCount * sizeof(WSABUF));
WSABUF* winBuffers = static_cast<WSABUF*>(memory.GetPtr());
for (std::size_t i = 0; i < bufferCount; ++i)
{
winBuffers[i].buf = static_cast<CHAR*>(buffers[i].data);
winBuffers[i].len = static_cast<ULONG>(buffers[i].dataLength);
}
DWORD byteSent;
if (WSASendTo(handle, winBuffers, static_cast<DWORD>(bufferCount), &byteSent, 0, reinterpret_cast<const sockaddr*>(nameBuffer.data()), bufferLength, nullptr, nullptr) == SOCKET_ERROR)
{
int errorCode = WSAGetLastError();
switch (errorCode)
{
case WSAEWOULDBLOCK:
{
byteSent = 0;
break;
}
default:
{
if (error)
*error = TranslateWSAErrorToSocketError(errorCode);
return false; //< Error
}
}
}
if (sent)
*sent = static_cast<int>(byteSent);
if (error)
*error = SocketError_NoError;
return true;
}
bool SocketImpl::SendTo(SocketHandle handle, const void* buffer, int length, const IpAddress& to, int* sent, SocketError* error)
{
NazaraAssert(handle != InvalidHandle, "Invalid handle");
@@ -719,7 +766,7 @@ namespace Nz
{
NazaraAssert(handle != InvalidHandle, "Invalid handle");
DWORD option = size;
DWORD option = static_cast<DWORD>(size);
if (setsockopt(handle, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<const char*>(&option), sizeof(option)) == SOCKET_ERROR)
{
if (error)
@@ -738,7 +785,7 @@ namespace Nz
{
NazaraAssert(handle != InvalidHandle, "Invalid handle");
DWORD option = size;
DWORD option = static_cast<DWORD>(size);
if (setsockopt(handle, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char*>(&option), sizeof(option)) == SOCKET_ERROR)
{
if (error)