Hello. So I want to make sure this isn't me doing something stupid or previously mentioned before I open an issue on it.
The following code will run on Linux perfectly, but on Windows (using the nuwen distribution of MinGW) the code compiles but crashes when trying to deserialise the string from the code. Changing it to a sf::String works, so that's a potential workaround. std::wstring will crash too, despite the documentation listing it as supported.
#include <SFML/Network.hpp>
#include <cstdint>
#include <iostream>
#include <stdexcept>
inline std::uint16_t getFreePort()
{
sf::TcpListener listener;
listener.listen(sf::TcpListener::AnyPort);
return listener.getLocalPort();
}
int main(int nArgs, char** ppcArgs)
{
typedef std::string PacketData;
auto generatePacketData = []() { return PacketData("Hello_World"); };
auto printPacketData = [](std::ostream& stream, PacketData data) { stream << data; };
const auto port = getFreePort();
const std::string ip = "127.0.0.1";
std::cout << "Listener listens" << std::endl;
sf::TcpListener listener;
if ( listener.listen(port) != sf::Socket::Status::Done )
throw std::runtime_error("Could not listen on port " + std::to_string(port));
listener.setBlocking(false);
std::cout << "Client Socket Connects" << std::endl;
sf::TcpSocket clientSocket;
if ( clientSocket.connect(sf::IpAddress(ip), port) != sf::Socket::Status::Done )
throw std::runtime_error("Could not connect to listener on port " + std::to_string(port));
clientSocket.setBlocking(false);
std::cout << "Waiting for networking overhead" << std::endl;
sf::sleep(sf::seconds(1));
std::cout << "Server accepts" << std::endl;
sf::TcpSocket serverSocket;
if ( listener.accept(serverSocket) != sf::Socket::Status::Done )
throw std::runtime_error("Could not accept listener on port " + std::to_string(port));
serverSocket.setBlocking(false);
const PacketData inData = generatePacketData();
std::cout << "Client sends packet" << std::endl;
sf::Packet sendPacket;
sendPacket << inData;
clientSocket.send(sendPacket);
std::cout << "Server receives packet" << std::endl;
sf::Packet receivePacket;
const auto status = serverSocket.receive(receivePacket);
PacketData outData;
switch(status)
{
case sf::Socket::Status::Done:
std::cout << "Server received packet" << std::endl;
receivePacket >> outData;
std::cout << "Server clears packet" << std::endl;
receivePacket.clear();
break;
default:
throw std::runtime_error("Did not receive packet");
}
std::cout << "Received packet: ";
printPacketData(std::cout, outData);
std::cout << std::endl;
return 0;
}
Am I missing something obvious, is std::string/std::wstring not supported with sf::Packet and it crashes or just works because of some overloading nonsense, or is it a known (or unknown) bug in sf::Packet?
Whilst I can use the sf::String solution, a compiler error would of been be nicer than my roundtrip tests of my SFML wrappers crashing (not failing, just straight up crashing) and me having to break out the debugger because of this unintuitive gotcha in SFML's packets.