Fix SFML deprecations and fix SFML warnings

Fix SFML deprecations and fix SFML warnings
This commit is contained in:
Andy Vandijck 2025-05-06 10:29:02 +02:00
parent 116af5c6a8
commit aaf4e7c18c
4 changed files with 15 additions and 42 deletions

View File

@ -65,9 +65,10 @@ nonstd::optional<IpAddress> IpAddress::resolve(std::string address)
return Any;
// Try to convert the address as a byte representation ("xxx.xxx.xxx.xxx")
const std::uint32_t ip = inet_addr(address.data());
if (ip != INADDR_NONE)
return IpAddress(ntohl(ip));
std::uint32_t ipaddr = 0;
inet_pton(AF_INET, address.data(), &ipaddr);
if (ipaddr != INADDR_NONE)
return IpAddress(ntohl(ipaddr));
// Not a valid address, try to convert it as a host name
addrinfo hints{}; // Zero-initialize
@ -106,10 +107,11 @@ IpAddress::IpAddress(std::uint32_t address) : m_address(address)
////////////////////////////////////////////////////////////
std::string IpAddress::toString() const
{
char address_str[INET_ADDRSTRLEN];
in_addr address{};
address.s_addr = htonl(m_address);
return inet_ntoa(address);
inet_ntop(AF_INET, &address, address_str, INET_ADDRSTRLEN);
return address_str;
}

View File

@ -42,7 +42,6 @@
#pragma warning(disable : 4127) // "conditional expression is constant" generated by the FD_SET macro
#endif
namespace
{
// Low-level send/receive flags (OS-dependent)
@ -246,14 +245,11 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size, std::size_t&
int result = 0;
for (sent = 0; sent < size; sent += static_cast<std::size_t>(result))
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
// Send a chunk of data
result = static_cast<int>(::send(getNativeHandle(),
static_cast<const char*>(data) + sent,
static_cast<SocketImpl::Size>(size - sent),
flags));
#pragma GCC diagnostic pop
// Check for errors
if (result < 0)
@ -283,14 +279,9 @@ Socket::Status TcpSocket::receive(void* data, std::size_t size, std::size_t& rec
err() << "Cannot receive data from the network (the destination buffer is invalid)" << std::endl;
return Status::Error;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
// Receive a chunk of bytes
const int sizeReceived = static_cast<int>(
recv(getNativeHandle(), static_cast<char*>(data), static_cast<SocketImpl::Size>(size), flags));
#pragma GCC diagnostic pop
// Check the number of bytes received
if (sizeReceived > 0)
{
@ -329,28 +320,15 @@ Socket::Status TcpSocket::send(Packet& packet)
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(m_blockToSendBuffer.data(), &packetSize, sizeof(packetSize));
#pragma GCC diagnostic pop
if (size > 0)
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
// being used.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
// Send the data block
std::size_t sent = 0;
const Status status = send(m_blockToSendBuffer.data() + packet.m_sendPos,
static_cast<SocketImpl::Size>(m_blockToSendBuffer.size() - packet.m_sendPos),
sent);
#pragma GCC diagnostic pop
#pragma GCC diagnostic pop
// In the case of a partial send, record the location to resume from
if (status == Status::Partial)
{

View File

@ -114,9 +114,6 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, IpAddress rem
// Build the target address
sockaddr_in address = SocketImpl::createAddress(remoteAddress.toInteger(), remotePort);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
// Send the data (unlike TCP, all the data is always sent in one call)
const int sent = static_cast<int>(
sendto(getNativeHandle(),
@ -125,7 +122,6 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, IpAddress rem
0,
reinterpret_cast<sockaddr*>(&address),
sizeof(address)));
#pragma GCC diagnostic pop
// Check for errors
if (sent < 0)
@ -157,8 +153,6 @@ Socket::Status UdpSocket::receive(void* data,
// Data that will be filled with the other computer's address
sockaddr_in address = SocketImpl::createAddress(INADDR_ANY, 0);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
// Receive a chunk of bytes
SocketImpl::AddrLength addressSize = sizeof(address);
const int sizeReceived = static_cast<int>(
@ -168,7 +162,6 @@ Socket::Status UdpSocket::receive(void* data,
0,
reinterpret_cast<sockaddr*>(&address),
&addressSize));
#pragma GCC diagnostic pop
// Check for errors
if (sizeReceived < 0)

View File

@ -47,14 +47,14 @@ namespace sf
template <typename IntegerType>
[[nodiscard]] constexpr IntegerType toInteger(std::array<unsigned char,8> bytes)
{
return ((IntegerType)(bytes[0] << 0)+
(IntegerType)(bytes[1] << 8)+
(IntegerType)(bytes[2] << 16)+
(IntegerType)(bytes[3] << 24)+
(IntegerType)(bytes[4] << 32)+
(IntegerType)(bytes[5] << 40)+
(IntegerType)(bytes[6] << 48)+
(IntegerType)(bytes[7] << 56));
return (((IntegerType)bytes[0] << 0)+
((IntegerType)bytes[1] << 8)+
((IntegerType)bytes[2] << 16)+
((IntegerType)bytes[3] << 24)+
((IntegerType)bytes[4] << 32)+
((IntegerType)bytes[5] << 40)+
((IntegerType)bytes[6] << 48)+
((IntegerType)bytes[7] << 56));
}
[[nodiscard]] SFML_SYSTEM_API std::FILE* openFile(const ghc::filesystem::path& filename, std::string mode);