Add Windows cross-platform support alongside Linux

Replace POSIX-specific socket and process APIs with portable
abstractions so the project builds on both Windows and Linux.

- Add include/network/net_platform.hpp: Winsock2/POSIX socket
  abstraction (socket types, non-blocking, error handling,
  WSAStartup lifecycle)
- Add include/platform/process.hpp: CreateProcess/fork+exec
  abstraction for spawning ffplay subprocesses
- Update network module (tcp_socket, world_socket) to use
  portable socket helpers instead of raw POSIX calls
- Update audio module (music_manager, footstep_manager,
  activity_sound_manager) to use portable process helpers
  instead of fork/exec/kill/waitpid
- Replace hardcoded /tmp/ paths with std::filesystem::temp_directory_path()
- Link ws2_32 and SDL2main on Windows in CMakeLists.txt
This commit is contained in:
Kelsi 2026-02-03 22:24:17 -08:00
parent dd126c6e4b
commit 6bf3fa4ed4
14 changed files with 416 additions and 186 deletions

View file

@ -1,18 +1,14 @@
#include "network/tcp_socket.hpp"
#include "network/packet.hpp"
#include "network/net_platform.hpp"
#include "core/logger.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <cstring>
namespace wowee {
namespace network {
TCPSocket::TCPSocket() = default;
TCPSocket::TCPSocket() {
net::ensureInit();
}
TCPSocket::~TCPSocket() {
disconnect();
@ -23,21 +19,20 @@ bool TCPSocket::connect(const std::string& host, uint16_t port) {
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
if (sockfd == INVALID_SOCK) {
LOG_ERROR("Failed to create socket");
return false;
}
// Set non-blocking
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
net::setNonBlocking(sockfd);
// Resolve host
struct hostent* server = gethostbyname(host.c_str());
if (server == nullptr) {
LOG_ERROR("Failed to resolve host: ", host);
close(sockfd);
sockfd = -1;
net::closeSocket(sockfd);
sockfd = INVALID_SOCK;
return false;
}
@ -49,11 +44,14 @@ bool TCPSocket::connect(const std::string& host, uint16_t port) {
serverAddr.sin_port = htons(port);
int result = ::connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if (result < 0 && errno != EINPROGRESS) {
LOG_ERROR("Failed to connect: ", strerror(errno));
close(sockfd);
sockfd = -1;
return false;
if (result < 0) {
int err = net::lastError();
if (!net::isInProgress(err)) {
LOG_ERROR("Failed to connect: ", net::errorString(err));
net::closeSocket(sockfd);
sockfd = INVALID_SOCK;
return false;
}
}
connected = true;
@ -62,9 +60,9 @@ bool TCPSocket::connect(const std::string& host, uint16_t port) {
}
void TCPSocket::disconnect() {
if (sockfd >= 0) {
close(sockfd);
sockfd = -1;
if (sockfd != INVALID_SOCK) {
net::closeSocket(sockfd);
sockfd = INVALID_SOCK;
}
connected = false;
receiveBuffer.clear();
@ -87,9 +85,9 @@ void TCPSocket::send(const Packet& packet) {
" size=", sendData.size(), " bytes");
// Send complete packet
ssize_t sent = ::send(sockfd, sendData.data(), sendData.size(), 0);
ssize_t sent = net::portableSend(sockfd, sendData.data(), sendData.size());
if (sent < 0) {
LOG_ERROR("Send failed: ", strerror(errno));
LOG_ERROR("Send failed: ", net::errorString(net::lastError()));
} else if (static_cast<size_t>(sent) != sendData.size()) {
LOG_WARNING("Partial send: ", sent, " of ", sendData.size(), " bytes");
}
@ -100,7 +98,7 @@ void TCPSocket::update() {
// Receive data into buffer
uint8_t buffer[4096];
ssize_t received = recv(sockfd, buffer, sizeof(buffer), 0);
ssize_t received = net::portableRecv(sockfd, buffer, sizeof(buffer));
if (received > 0) {
LOG_DEBUG("Received ", received, " bytes from server");
@ -113,9 +111,12 @@ void TCPSocket::update() {
LOG_INFO("Connection closed by server");
disconnect();
}
else if (errno != EAGAIN && errno != EWOULDBLOCK) {
LOG_ERROR("Receive failed: ", strerror(errno));
disconnect();
else {
int err = net::lastError();
if (!net::isWouldBlock(err)) {
LOG_ERROR("Receive failed: ", net::errorString(err));
disconnect();
}
}
}