From 8d0da1d9d6dd86f60618c62bc01b833524a247ee Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Tue, 3 Apr 2012 19:06:32 +0200 Subject: [PATCH] Replaced char* arguments with void* for raw data --- examples/voip/Server.cpp | 2 +- include/SFML/Network/Packet.hpp | 14 +++++++------- include/SFML/Network/TcpSocket.hpp | 4 ++-- include/SFML/Network/UdpSocket.hpp | 4 ++-- include/SFML/System/InputStream.hpp | 4 ++-- src/SFML/Network/Packet.cpp | 26 +++++++++++++------------- src/SFML/Network/TcpSocket.cpp | 10 +++++----- src/SFML/Network/UdpSocket.cpp | 10 +++++----- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/examples/voip/Server.cpp b/examples/voip/Server.cpp index fb318d363..29227fab3 100644 --- a/examples/voip/Server.cpp +++ b/examples/voip/Server.cpp @@ -126,7 +126,7 @@ private : if (id == audioData) { // Extract audio samples from the packet, and append it to our samples buffer - const sf::Int16* samples = reinterpret_cast(packet.getData() + 1); + const sf::Int16* samples = reinterpret_cast(static_cast(packet.getData()) + 1); std::size_t sampleCount = (packet.getDataSize() - 1) / sizeof(sf::Int16); // Don't forget that the other thread can access the sample array at any time diff --git a/include/SFML/Network/Packet.hpp b/include/SFML/Network/Packet.hpp index 0ef52cd4a..c8684414d 100644 --- a/include/SFML/Network/Packet.hpp +++ b/include/SFML/Network/Packet.hpp @@ -99,7 +99,7 @@ public : /// \see getDataSize /// //////////////////////////////////////////////////////////// - const char* getData() const; + const void* getData() const; //////////////////////////////////////////////////////////// /// \brief Get the size of the data contained in the packet @@ -250,7 +250,7 @@ private : /// \see onReceive /// //////////////////////////////////////////////////////////// - virtual const char* onSend(std::size_t& size); + virtual const void* onSend(std::size_t& size); //////////////////////////////////////////////////////////// /// \brief Called after the packet is received over the network @@ -269,7 +269,7 @@ private : /// \see onSend /// //////////////////////////////////////////////////////////// - virtual void onReceive(const char* data, std::size_t size); + virtual void onReceive(const void* data, std::size_t size); //////////////////////////////////////////////////////////// // Member data @@ -377,18 +377,18 @@ private : /// \code /// class ZipPacket : public sf::Packet /// { -/// virtual const char* onSend(std::size_t& size) +/// virtual const void* onSend(std::size_t& size) /// { -/// const char* srcData = getData(); +/// const void* srcData = getData(); /// std::size_t srcSize = getDataSize(); /// /// return MySuperZipFunction(srcData, srcSize, &size); /// } /// -/// virtual void onReceive(const char* data, std::size_t size) +/// virtual void onReceive(const void* data, std::size_t size) /// { /// std::size_t dstSize; -/// const char* dstData = MySuperUnzipFunction(data, size, &dstSize); +/// const void* dstData = MySuperUnzipFunction(data, size, &dstSize); /// /// append(dstData, dstSize); /// } diff --git a/include/SFML/Network/TcpSocket.hpp b/include/SFML/Network/TcpSocket.hpp index d7f3b8713..cd908f4fb 100644 --- a/include/SFML/Network/TcpSocket.hpp +++ b/include/SFML/Network/TcpSocket.hpp @@ -134,7 +134,7 @@ public : /// \see receive /// //////////////////////////////////////////////////////////// - Status send(const char* data, std::size_t size); + Status send(const void* data, std::size_t size); //////////////////////////////////////////////////////////// /// \brief Receive raw data from the remote peer @@ -152,7 +152,7 @@ public : /// \see send /// //////////////////////////////////////////////////////////// - Status receive(char* data, std::size_t size, std::size_t& received); + Status receive(void* data, std::size_t size, std::size_t& received); //////////////////////////////////////////////////////////// /// \brief Send a formatted packet of data to the remote peer diff --git a/include/SFML/Network/UdpSocket.hpp b/include/SFML/Network/UdpSocket.hpp index 8bee1c454..58b2eaddb 100644 --- a/include/SFML/Network/UdpSocket.hpp +++ b/include/SFML/Network/UdpSocket.hpp @@ -120,7 +120,7 @@ public : /// \see receive /// //////////////////////////////////////////////////////////// - Status send(const char* data, std::size_t size, const IpAddress& remoteAddress, unsigned short remotePort); + Status send(const void* data, std::size_t size, const IpAddress& remoteAddress, unsigned short remotePort); //////////////////////////////////////////////////////////// /// \brief Receive raw data from a remote peer @@ -143,7 +143,7 @@ public : /// \see send /// //////////////////////////////////////////////////////////// - Status receive(char* data, std::size_t size, std::size_t& received, IpAddress& remoteAddress, unsigned short& remotePort); + Status receive(void* data, std::size_t size, std::size_t& received, IpAddress& remoteAddress, unsigned short& remotePort); //////////////////////////////////////////////////////////// /// \brief Send a formatted packet of data to a remote peer diff --git a/include/SFML/System/InputStream.hpp b/include/SFML/System/InputStream.hpp index 2eeab1153..f33aceeb4 100644 --- a/include/SFML/System/InputStream.hpp +++ b/include/SFML/System/InputStream.hpp @@ -56,7 +56,7 @@ public : /// \return The number of bytes actually read /// //////////////////////////////////////////////////////////// - virtual Int64 read(char* data, Int64 size) = 0; + virtual Int64 read(void* data, Int64 size) = 0; //////////////////////////////////////////////////////////// /// \brief Change the current reading position @@ -117,7 +117,7 @@ public : /// /// bool open(std::string filename); /// -/// Int64 read(char* data, Int64 size); +/// Int64 read(void* data, Int64 size); /// /// Int64 seek(Int64 position); /// diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index 595d7fa0a..ebb6eb2de 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -71,7 +71,7 @@ void Packet::clear() //////////////////////////////////////////////////////////// -const char* Packet::getData() const +const void* Packet::getData() const { return !m_data.empty() ? &m_data[0] : NULL; } @@ -114,7 +114,7 @@ Packet& Packet::operator >>(Int8& data) { if (checkSize(sizeof(data))) { - data = *reinterpret_cast(getData() + m_readPos); + data = *reinterpret_cast(&m_data[m_readPos]); m_readPos += sizeof(data); } @@ -127,7 +127,7 @@ Packet& Packet::operator >>(Uint8& data) { if (checkSize(sizeof(data))) { - data = *reinterpret_cast(getData() + m_readPos); + data = *reinterpret_cast(&m_data[m_readPos]); m_readPos += sizeof(data); } @@ -140,7 +140,7 @@ Packet& Packet::operator >>(Int16& data) { if (checkSize(sizeof(data))) { - data = ntohs(*reinterpret_cast(getData() + m_readPos)); + data = ntohs(*reinterpret_cast(&m_data[m_readPos])); m_readPos += sizeof(data); } @@ -153,7 +153,7 @@ Packet& Packet::operator >>(Uint16& data) { if (checkSize(sizeof(data))) { - data = ntohs(*reinterpret_cast(getData() + m_readPos)); + data = ntohs(*reinterpret_cast(&m_data[m_readPos])); m_readPos += sizeof(data); } @@ -166,7 +166,7 @@ Packet& Packet::operator >>(Int32& data) { if (checkSize(sizeof(data))) { - data = ntohl(*reinterpret_cast(getData() + m_readPos)); + data = ntohl(*reinterpret_cast(&m_data[m_readPos])); m_readPos += sizeof(data); } @@ -179,7 +179,7 @@ Packet& Packet::operator >>(Uint32& data) { if (checkSize(sizeof(data))) { - data = ntohl(*reinterpret_cast(getData() + m_readPos)); + data = ntohl(*reinterpret_cast(&m_data[m_readPos])); m_readPos += sizeof(data); } @@ -192,7 +192,7 @@ Packet& Packet::operator >>(float& data) { if (checkSize(sizeof(data))) { - data = *reinterpret_cast(getData() + m_readPos); + data = *reinterpret_cast(&m_data[m_readPos]); m_readPos += sizeof(data); } @@ -205,7 +205,7 @@ Packet& Packet::operator >>(double& data) { if (checkSize(sizeof(data))) { - data = *reinterpret_cast(getData() + m_readPos); + data = *reinterpret_cast(&m_data[m_readPos]); m_readPos += sizeof(data); } @@ -223,7 +223,7 @@ Packet& Packet::operator >>(char* data) if ((length > 0) && checkSize(length)) { // Then extract characters - std::memcpy(data, getData() + m_readPos, length); + std::memcpy(data, &m_data[m_readPos], length); data[length] = '\0'; // Update reading position @@ -245,7 +245,7 @@ Packet& Packet::operator >>(std::string& data) if ((length > 0) && checkSize(length)) { // Then extract characters - data.assign(getData() + m_readPos, length); + data.assign(&m_data[m_readPos], length); // Update reading position m_readPos += length; @@ -496,7 +496,7 @@ bool Packet::checkSize(std::size_t size) //////////////////////////////////////////////////////////// -const char* Packet::onSend(std::size_t& size) +const void* Packet::onSend(std::size_t& size) { size = getDataSize(); return getData(); @@ -504,7 +504,7 @@ const char* Packet::onSend(std::size_t& size) //////////////////////////////////////////////////////////// -void Packet::onReceive(const char* data, std::size_t size) +void Packet::onReceive(const void* data, std::size_t size) { append(data, size); } diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index be2a0869d..0a49d309d 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -206,7 +206,7 @@ void TcpSocket::disconnect() //////////////////////////////////////////////////////////// -Socket::Status TcpSocket::send(const char* data, std::size_t size) +Socket::Status TcpSocket::send(const void* data, std::size_t size) { // Check the parameters if (!data || (size == 0)) @@ -221,7 +221,7 @@ Socket::Status TcpSocket::send(const char* data, std::size_t size) for (int length = 0; length < sizeToSend; length += sent) { // Send a chunk of data - sent = ::send(getHandle(), data + length, sizeToSend - length, 0); + sent = ::send(getHandle(), static_cast(data) + length, sizeToSend - length, 0); // Check for errors if (sent < 0) @@ -233,7 +233,7 @@ Socket::Status TcpSocket::send(const char* data, std::size_t size) //////////////////////////////////////////////////////////// -Socket::Status TcpSocket::receive(char* data, std::size_t size, std::size_t& received) +Socket::Status TcpSocket::receive(void* data, std::size_t size, std::size_t& received) { // First clear the variables to fill received = 0; @@ -246,7 +246,7 @@ Socket::Status TcpSocket::receive(char* data, std::size_t size, std::size_t& rec } // Receive a chunk of bytes - int sizeReceived = recv(getHandle(), data, static_cast(size), 0); + int sizeReceived = recv(getHandle(), static_cast(data), static_cast(size), 0); // Check the number of bytes received if (sizeReceived > 0) @@ -274,7 +274,7 @@ Socket::Status TcpSocket::send(Packet& packet) // Get the data to send from the packet std::size_t size = 0; - const char* data = packet.onSend(size); + const void* data = packet.onSend(size); // First send the packet size Uint32 packetSize = htonl(static_cast(size)); diff --git a/src/SFML/Network/UdpSocket.cpp b/src/SFML/Network/UdpSocket.cpp index f2ba8d3b3..7b1bf5bbc 100644 --- a/src/SFML/Network/UdpSocket.cpp +++ b/src/SFML/Network/UdpSocket.cpp @@ -90,7 +90,7 @@ void UdpSocket::unbind() //////////////////////////////////////////////////////////// -Socket::Status UdpSocket::send(const char* data, std::size_t size, const IpAddress& remoteAddress, unsigned short remotePort) +Socket::Status UdpSocket::send(const void* data, std::size_t size, const IpAddress& remoteAddress, unsigned short remotePort) { // Create the internal socket if it doesn't exist create(); @@ -107,7 +107,7 @@ Socket::Status UdpSocket::send(const char* data, std::size_t size, const IpAddre sockaddr_in address = priv::SocketImpl::createAddress(remoteAddress.toInteger(), remotePort); // Send the data (unlike TCP, all the data is always sent in one call) - int sent = sendto(getHandle(), data, static_cast(size), 0, reinterpret_cast(&address), sizeof(address)); + int sent = sendto(getHandle(), static_cast(data), static_cast(size), 0, reinterpret_cast(&address), sizeof(address)); // Check for errors if (sent < 0) @@ -118,7 +118,7 @@ Socket::Status UdpSocket::send(const char* data, std::size_t size, const IpAddre //////////////////////////////////////////////////////////// -Socket::Status UdpSocket::receive(char* data, std::size_t size, std::size_t& received, IpAddress& remoteAddress, unsigned short& remotePort) +Socket::Status UdpSocket::receive(void* data, std::size_t size, std::size_t& received, IpAddress& remoteAddress, unsigned short& remotePort) { // First clear the variables to fill received = 0; @@ -137,7 +137,7 @@ Socket::Status UdpSocket::receive(char* data, std::size_t size, std::size_t& rec // Receive a chunk of bytes priv::SocketImpl::AddrLength addressSize = sizeof(address); - int sizeReceived = recvfrom(getHandle(), data, static_cast(size), 0, reinterpret_cast(&address), &addressSize); + int sizeReceived = recvfrom(getHandle(), static_cast(data), static_cast(size), 0, reinterpret_cast(&address), &addressSize); // Check for errors if (sizeReceived < 0) @@ -165,7 +165,7 @@ Socket::Status UdpSocket::send(Packet& packet, const IpAddress& remoteAddress, u // Get the data to send from the packet std::size_t size = 0; - const char* data = packet.onSend(size); + const void* data = packet.onSend(size); // Send it return send(data, size, remoteAddress, remotePort);