From e4c5af63b75743c7b1721548d53244dfc2198afb Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Thu, 23 Jun 2022 02:15:48 +0200 Subject: [PATCH] Replace 'TcpSocket::send' local buffer with data member --- include/SFML/Network/TcpSocket.hpp | 3 ++- src/SFML/Network/TcpSocket.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/SFML/Network/TcpSocket.hpp b/include/SFML/Network/TcpSocket.hpp index 7ffcfa46..afcdc63c 100644 --- a/include/SFML/Network/TcpSocket.hpp +++ b/include/SFML/Network/TcpSocket.hpp @@ -228,7 +228,8 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received + PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received + std::vector m_blockToSendBuffer; //!< Buffer used to prepare data being sent from the socket }; } // namespace sf diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index de2fed8e..f0414d96 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -324,15 +324,15 @@ Socket::Status TcpSocket::send(Packet& packet) Uint32 packetSize = htonl(static_cast(size)); // Allocate memory for the data block to send - std::vector blockToSend(sizeof(packetSize) + size); + m_blockToSendBuffer.resize(sizeof(packetSize) + size); // Copy the packet size and data into the block to send #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnull-dereference" // False positive. - std::memcpy(blockToSend.data(), &packetSize, sizeof(packetSize)); + std::memcpy(m_blockToSendBuffer.data(), &packetSize, sizeof(packetSize)); #pragma GCC diagnostic pop if (size > 0) - std::memcpy(blockToSend.data() + sizeof(packetSize), data, size); + std::memcpy(m_blockToSendBuffer.data() + sizeof(packetSize), data, size); // These warnings are ignored here for portability, as even on Windows the // signature of `send` might change depending on whether Win32 or MinGW is @@ -343,7 +343,7 @@ Socket::Status TcpSocket::send(Packet& packet) #pragma GCC diagnostic ignored "-Wsign-conversion" // Send the data block std::size_t sent; - Status status = send(blockToSend.data() + packet.m_sendPos, static_cast(blockToSend.size() - packet.m_sendPos), sent); + Status status = send(m_blockToSendBuffer.data() + packet.m_sendPos, static_cast(m_blockToSendBuffer.size() - packet.m_sendPos), sent); #pragma GCC diagnostic pop #pragma GCC diagnostic pop