Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced char* arguments with void* for raw data

  • Loading branch information...
commit 8d0da1d9d6dd86f60618c62bc01b833524a247ee 1 parent 69f387d
@LaurentGomila authored
View
2  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<const sf::Int16*>(packet.getData() + 1);
+ const sf::Int16* samples = reinterpret_cast<const sf::Int16*>(static_cast<const char*>(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
View
14 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);
/// }
View
4 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
View
4 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
View
4 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);
///
View
26 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<const Int8*>(getData() + m_readPos);
+ data = *reinterpret_cast<const Int8*>(&m_data[m_readPos]);
m_readPos += sizeof(data);
}
@@ -127,7 +127,7 @@ Packet& Packet::operator >>(Uint8& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const Uint8*>(getData() + m_readPos);
+ data = *reinterpret_cast<const Uint8*>(&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<const Int16*>(getData() + m_readPos));
+ data = ntohs(*reinterpret_cast<const Int16*>(&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<const Uint16*>(getData() + m_readPos));
+ data = ntohs(*reinterpret_cast<const Uint16*>(&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<const Int32*>(getData() + m_readPos));
+ data = ntohl(*reinterpret_cast<const Int32*>(&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<const Uint32*>(getData() + m_readPos));
+ data = ntohl(*reinterpret_cast<const Uint32*>(&m_data[m_readPos]));
m_readPos += sizeof(data);
}
@@ -192,7 +192,7 @@ Packet& Packet::operator >>(float& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const float*>(getData() + m_readPos);
+ data = *reinterpret_cast<const float*>(&m_data[m_readPos]);
m_readPos += sizeof(data);
}
@@ -205,7 +205,7 @@ Packet& Packet::operator >>(double& data)
{
if (checkSize(sizeof(data)))
{
- data = *reinterpret_cast<const double*>(getData() + m_readPos);
+ data = *reinterpret_cast<const double*>(&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);
}
View
10 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<const char*>(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<int>(size), 0);
+ int sizeReceived = recv(getHandle(), static_cast<char*>(data), static_cast<int>(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<Uint32>(size));
View
10 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<int>(size), 0, reinterpret_cast<sockaddr*>(&address), sizeof(address));
+ int sent = sendto(getHandle(), static_cast<const char*>(data), static_cast<int>(size), 0, reinterpret_cast<sockaddr*>(&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<int>(size), 0, reinterpret_cast<sockaddr*>(&address), &addressSize);
+ int sizeReceived = recvfrom(getHandle(), static_cast<char*>(data), static_cast<int>(size), 0, reinterpret_cast<sockaddr*>(&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);

2 comments on commit 8d0da1d

@retep998

Just because you can, does not mean you should.
In these functions, the type of the data has no effect on the behavior of the functions, so there's no need for templates.
Besides, using templates would just cause the compiler to emit a whole bunch of copies of this stuff for each template parameter you use, thereby slowing down compilation, and also bloating the resulting binary if your compiler isn't good enough at optimizing.
By using void*, you don't even need to cast anything.
Granted it would be nice to have a templated function that could take an arbitrary type and use sizeof and such to read and write that object, although it won't be able to deal with stuff such as deep copies and pointer members, so it's best to simply let the user decide how to turn the object into the appropriate void* data.

@LaurentGomila

There's typed stuff where it makes sense (in this case, sf::Packet).

Please sign in to comment.
Something went wrong with that request. Please try again.