From ab3b730d217c5d26216962e32b5083d9a44ea777 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 27 Jan 2017 14:51:01 +0100 Subject: [PATCH] Network/TcpClient|UdpSocket: Add SendMultiple method To efficiently merge multiples buffers into a reduced number of network packets --- include/Nazara/Network/AbstractSocket.hpp | 4 +- include/Nazara/Network/NetBuffer.hpp | 21 ++++++++++ include/Nazara/Network/TcpClient.hpp | 2 + include/Nazara/Network/UdpSocket.hpp | 8 ++-- src/Nazara/Network/Posix/SocketImpl.cpp | 51 +++++++++++++++++++++++ src/Nazara/Network/Posix/SocketImpl.hpp | 1 + src/Nazara/Network/TcpClient.cpp | 39 +++++++++++++++++ src/Nazara/Network/UdpSocket.cpp | 25 +++++++++++ src/Nazara/Network/Win32/SocketImpl.cpp | 51 ++++++++++++++++++++++- src/Nazara/Network/Win32/SocketImpl.hpp | 10 +++-- 10 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 include/Nazara/Network/NetBuffer.hpp diff --git a/include/Nazara/Network/AbstractSocket.hpp b/include/Nazara/Network/AbstractSocket.hpp index 8a2e55e25..0b33c6a8f 100644 --- a/include/Nazara/Network/AbstractSocket.hpp +++ b/include/Nazara/Network/AbstractSocket.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Network module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -68,4 +68,4 @@ namespace Nz #include -#endif // NAZARA_ABSTRACTSOCKET_HPP \ No newline at end of file +#endif // NAZARA_ABSTRACTSOCKET_HPP diff --git a/include/Nazara/Network/NetBuffer.hpp b/include/Nazara/Network/NetBuffer.hpp new file mode 100644 index 000000000..0c05c53b2 --- /dev/null +++ b/include/Nazara/Network/NetBuffer.hpp @@ -0,0 +1,21 @@ +// Copyright (C) 2017 Jérôme Leclercq +// This file is part of the "Nazara Engine - Network module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_NETBUFFER_HPP +#define NAZARA_NETBUFFER_HPP + +#include + +namespace Nz +{ + struct NetBuffer + { + void* data; + std::size_t dataLength; + }; +} + +#endif // NAZARA_NETBUFFER_HPP diff --git a/include/Nazara/Network/TcpClient.hpp b/include/Nazara/Network/TcpClient.hpp index dc41d8eb5..a3b229bda 100644 --- a/include/Nazara/Network/TcpClient.hpp +++ b/include/Nazara/Network/TcpClient.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace Nz { @@ -49,6 +50,7 @@ namespace Nz bool ReceivePacket(NetPacket* packet); bool Send(const void* buffer, std::size_t size, std::size_t* sent); + bool SendMultiple(const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent); bool SendPacket(const NetPacket& packet); bool SetCursorPos(UInt64 offset) override; diff --git a/include/Nazara/Network/UdpSocket.hpp b/include/Nazara/Network/UdpSocket.hpp index 3c4abdfe7..04ca41db0 100644 --- a/include/Nazara/Network/UdpSocket.hpp +++ b/include/Nazara/Network/UdpSocket.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Network module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Nz { @@ -29,7 +30,7 @@ namespace Nz inline bool Create(NetProtocol protocol); void EnableBroadcasting(bool broadcasting); - + inline IpAddress GetBoundAddress() const; inline UInt16 GetBoundPort() const; inline SocketState GetState() const; @@ -42,6 +43,7 @@ namespace Nz bool ReceivePacket(NetPacket* packet, IpAddress* from); bool Send(const IpAddress& to, const void* buffer, std::size_t size, std::size_t* sent); + bool SendMultiple(const IpAddress& to, const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent); bool SendPacket(const IpAddress& to, const NetPacket& packet); private: @@ -55,4 +57,4 @@ namespace Nz #include -#endif // NAZARA_UDPSOCKET_HPP \ No newline at end of file +#endif // NAZARA_UDPSOCKET_HPP diff --git a/src/Nazara/Network/Posix/SocketImpl.cpp b/src/Nazara/Network/Posix/SocketImpl.cpp index 7d28e6785..93fe74d4d 100644 --- a/src/Nazara/Network/Posix/SocketImpl.cpp +++ b/src/Nazara/Network/Posix/SocketImpl.cpp @@ -577,6 +577,57 @@ 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"); + + StackAllocation memory = NazaraStackAllocation(bufferCount * sizeof(iovec)); + iovec* sysBuffers = static_cast(memory.GetPtr()); + for (std::size_t i = 0; i < bufferCount; ++i) + { + sysBuffers[i].iov_base = buffers[i].data; + sysBuffers[i].iov_len = buffers[i].dataLength; + } + + struct msghdr header; + std::memset(&header, 0, sizeof(header); + + IpAddressImpl::SockAddrBuffer nameBuffer; + header.msg_namelen = IpAddressImpl::ToSockAddr(to, nameBuffer.data()); + header.msg_name = nameBuffer.data(); + msgHdr.msg_iov = sysBuffers; + msgHdr.msg_iovlen = static_cast(bufferCount); + + int sentLength = sendmsg (socket, &msgHdr, MSG_NOSIGNAL); + if (byteSent == SOCKET_ERROR) + { + int errorCode = GetLastErrorCode(); + switch (errorCode) + { + case EWOULDBLOCK: + byteSent = 0; + break; + + default: + { + if (error) + *error = TranslateErrnoToResolveError(errorCode); + + return false; //< Error + } + } + } + + if (sent) + *sent = static_cast(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"); diff --git a/src/Nazara/Network/Posix/SocketImpl.hpp b/src/Nazara/Network/Posix/SocketImpl.hpp index a543d8880..e90d4cc74 100644 --- a/src/Nazara/Network/Posix/SocketImpl.hpp +++ b/src/Nazara/Network/Posix/SocketImpl.hpp @@ -64,6 +64,7 @@ namespace Nz static bool ReceiveFrom(SocketHandle handle, void* buffer, int length, IpAddress* from, int* read, SocketError* error); static bool Send(SocketHandle handle, const void* buffer, int length, int* sent, SocketError* error); + static bool SendMultiple(SocketHandle handle, const NetBuffer* buffers, std::size_t bufferCount, const IpAddress& to, int* sent, SocketError* error); static bool SendTo(SocketHandle handle, const void* buffer, int length, const IpAddress& to, int* sent, SocketError* error); static bool SetBlocking(SocketHandle handle, bool blocking, SocketError* error = nullptr); diff --git a/src/Nazara/Network/TcpClient.cpp b/src/Nazara/Network/TcpClient.cpp index b5fd845ee..54144e8fd 100644 --- a/src/Nazara/Network/TcpClient.cpp +++ b/src/Nazara/Network/TcpClient.cpp @@ -353,6 +353,45 @@ namespace Nz return true; } + /*! + * \brief Sends multiple buffers at once + * \return true If data were sent + * + * \param buffers A pointer to an array of NetBuffer containing buffers and size data + * \param size Number of NetBuffer to send + * \param sent Optional argument to get the number of bytes sent + */ + bool TcpClient::SendMultiple(const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent) + { + NazaraAssert(buffers && bufferCount > 0, "Invalid buffer"); + + int byteSent; + if (!SocketImpl::SendMultiple(m_handle, buffers, bufferCount, m_peerAddress, &byteSent, &m_lastError)) + { + switch (m_lastError) + { + case SocketError_ConnectionClosed: + case SocketError_ConnectionRefused: + UpdateState(SocketState_NotConnected); + break; + + default: + break; + } + + if (sent) + *sent = byteSent; + + return false; + } + + if (sent) + *sent = byteSent; + + UpdateState(SocketState_Connected); + return true; + } + /*! * \brief Sends the packet available * \return true If packet sent diff --git a/src/Nazara/Network/UdpSocket.cpp b/src/Nazara/Network/UdpSocket.cpp index 679922513..c13dee343 100644 --- a/src/Nazara/Network/UdpSocket.cpp +++ b/src/Nazara/Network/UdpSocket.cpp @@ -179,6 +179,31 @@ namespace Nz return true; } + /*! + * \brief Sends multiple buffers as one datagram + * \return true If data were sent + * + * \param to Destination IpAddress (must match socket protocol) + * \param buffers A pointer to an array of NetBuffer containing buffers and size data + * \param size Number of NetBuffer to send + * \param sent Optional argument to get the number of bytes sent + */ + bool UdpSocket::SendMultiple(const IpAddress& to, const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent) + { + NazaraAssert(to.IsValid(), "Invalid ip address"); + NazaraAssert(to.GetProtocol() == m_protocol, "IP Address has a different protocol than the socket"); + NazaraAssert(buffers && bufferCount > 0, "Invalid buffer"); + + int byteSent; + if (!SocketImpl::SendMultiple(m_handle, buffers, bufferCount, to, &byteSent, &m_lastError)) + return false; + + if (sent) + *sent = byteSent; + + return true; + } + /*! * \brief Sends the packet available * \return true If packet sent diff --git a/src/Nazara/Network/Win32/SocketImpl.cpp b/src/Nazara/Network/Win32/SocketImpl.cpp index 74550fe37..3a5c285e9 100644 --- a/src/Nazara/Network/Win32/SocketImpl.cpp +++ b/src/Nazara/Network/Win32/SocketImpl.cpp @@ -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(memory.GetPtr()); + for (std::size_t i = 0; i < bufferCount; ++i) + { + winBuffers[i].buf = static_cast(buffers[i].data); + winBuffers[i].len = static_cast(buffers[i].dataLength); + } + + DWORD byteSent; + if (WSASendTo(handle, winBuffers, static_cast(bufferCount), &byteSent, 0, reinterpret_cast(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(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(size); if (setsockopt(handle, SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&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(size); if (setsockopt(handle, SOL_SOCKET, SO_SNDBUF, reinterpret_cast(&option), sizeof(option)) == SOCKET_ERROR) { if (error) diff --git a/src/Nazara/Network/Win32/SocketImpl.hpp b/src/Nazara/Network/Win32/SocketImpl.hpp index 5af2b2534..edcc7ce09 100644 --- a/src/Nazara/Network/Win32/SocketImpl.hpp +++ b/src/Nazara/Network/Win32/SocketImpl.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2017 Jérôme Leclercq +// Copyright (C) 2017 Jérôme Leclercq // This file is part of the "Nazara Engine - Network module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -7,9 +7,10 @@ #ifndef NAZARA_SOCKETIMPL_HPP #define NAZARA_SOCKETIMPL_HPP -#include #include #include +#include +#include #include #define NAZARA_NETWORK_POLL_SUPPORT NAZARA_CORE_WINDOWS_NT6 @@ -34,7 +35,7 @@ namespace Nz static SocketState Bind(SocketHandle handle, const IpAddress& address, SocketError* error); static SocketHandle Create(NetProtocol protocol, SocketType type, SocketError* error); - + static void ClearErrorCode(SocketHandle handle); static void Close(SocketHandle handle); @@ -65,6 +66,7 @@ namespace Nz static bool ReceiveFrom(SocketHandle handle, void* buffer, int length, IpAddress* from, int* read, SocketError* error); static bool Send(SocketHandle handle, const void* buffer, int length, int* sent, SocketError* error); + static bool SendMultiple(SocketHandle handle, const NetBuffer* buffers, std::size_t bufferCount, const IpAddress& to, int* sent, SocketError* error); static bool SendTo(SocketHandle handle, const void* buffer, int length, const IpAddress& to, int* sent, SocketError* error); static bool SetBlocking(SocketHandle handle, bool blocking, SocketError* error = nullptr); @@ -87,4 +89,4 @@ namespace Nz }; } -#endif // NAZARA_SOCKETIMPL_HPP \ No newline at end of file +#endif // NAZARA_SOCKETIMPL_HPP