mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-22 23:30:14 +00:00
Socket fixes (fixes Windows-only connection failure): - WorldSocket::connect() now waits for non-blocking connect to complete with select() before returning, preventing WSAENOTCONN on the first recv() call on Windows (Linux handles this implicitly but Windows requires writability poll after non-blocking connect) - Add net::isConnectionClosed() helper: treats WSAENOTCONN/WSAECONNRESET/ WSAESHUTDOWN/WSAECONNABORTED as graceful peer-close rather than recv errors - Apply isConnectionClosed() in both WorldSocket and TCPSocket recv loops UI: - Add renderBossFrames(): displays boss unit health bars in top-right corner when SMSG_UPDATE_INSTANCE_ENCOUNTER_UNIT has active slots; supports click-to-target and color-coded health bars (red→orange→yellow as HP drops)
138 lines
3.2 KiB
C++
138 lines
3.2 KiB
C++
#pragma once
|
|
|
|
// Cross-platform socket abstractions for Windows (Winsock2) and POSIX.
|
|
|
|
#ifdef _WIN32
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#endif
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
using socket_t = SOCKET;
|
|
#ifndef __MINGW32__
|
|
using ssize_t = int; // recv/send return int on MSVC
|
|
#endif
|
|
|
|
inline constexpr socket_t INVALID_SOCK = INVALID_SOCKET;
|
|
|
|
#else
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <cerrno>
|
|
|
|
using socket_t = int;
|
|
|
|
inline constexpr socket_t INVALID_SOCK = -1;
|
|
|
|
#endif
|
|
|
|
#include <cstring>
|
|
|
|
namespace wowee {
|
|
namespace net {
|
|
|
|
// ---- Winsock lifecycle (no-op on Linux) ----
|
|
|
|
#ifdef _WIN32
|
|
struct WinsockInit {
|
|
WinsockInit() {
|
|
WSADATA wsa;
|
|
WSAStartup(MAKEWORD(2, 2), &wsa);
|
|
}
|
|
~WinsockInit() { WSACleanup(); }
|
|
};
|
|
// Call once at program start (e.g. as a static in Application).
|
|
inline void ensureInit() {
|
|
static WinsockInit instance;
|
|
}
|
|
#else
|
|
inline void ensureInit() {}
|
|
#endif
|
|
|
|
// ---- Portable helpers ----
|
|
|
|
inline void closeSocket(socket_t s) {
|
|
#ifdef _WIN32
|
|
closesocket(s);
|
|
#else
|
|
close(s);
|
|
#endif
|
|
}
|
|
|
|
inline bool setNonBlocking(socket_t s) {
|
|
#ifdef _WIN32
|
|
u_long mode = 1;
|
|
return ioctlsocket(s, FIONBIO, &mode) == 0;
|
|
#else
|
|
int flags = fcntl(s, F_GETFL, 0);
|
|
return fcntl(s, F_SETFL, flags | O_NONBLOCK) != -1;
|
|
#endif
|
|
}
|
|
|
|
inline int lastError() {
|
|
#ifdef _WIN32
|
|
return WSAGetLastError();
|
|
#else
|
|
return errno;
|
|
#endif
|
|
}
|
|
|
|
inline bool isWouldBlock(int err) {
|
|
#ifdef _WIN32
|
|
return err == WSAEWOULDBLOCK;
|
|
#else
|
|
return err == EAGAIN || err == EWOULDBLOCK;
|
|
#endif
|
|
}
|
|
|
|
// Returns true for errors that mean the peer closed the connection cleanly.
|
|
// On Windows, WSAENOTCONN / WSAECONNRESET / WSAESHUTDOWN can be returned by
|
|
// recv() when the server closes the connection, rather than returning 0.
|
|
inline bool isConnectionClosed(int err) {
|
|
#ifdef _WIN32
|
|
return err == WSAENOTCONN || // socket not connected (server closed)
|
|
err == WSAECONNRESET || // connection reset by peer
|
|
err == WSAESHUTDOWN || // socket shut down
|
|
err == WSAECONNABORTED; // connection aborted
|
|
#else
|
|
return err == ENOTCONN || err == ECONNRESET;
|
|
#endif
|
|
}
|
|
|
|
inline bool isInProgress(int err) {
|
|
#ifdef _WIN32
|
|
return err == WSAEWOULDBLOCK || err == WSAEALREADY;
|
|
#else
|
|
return err == EINPROGRESS;
|
|
#endif
|
|
}
|
|
|
|
inline const char* errorString(int err) {
|
|
#ifdef _WIN32
|
|
// Simple thread-local buffer for FormatMessage
|
|
thread_local char buf[256];
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
nullptr, err, 0, buf, sizeof(buf), nullptr);
|
|
return buf;
|
|
#else
|
|
return strerror(err);
|
|
#endif
|
|
}
|
|
|
|
// Portable send — Windows recv/send take char*, not void*.
|
|
inline ssize_t portableSend(socket_t s, const uint8_t* data, size_t len) {
|
|
return ::send(s, reinterpret_cast<const char*>(data), static_cast<int>(len), 0);
|
|
}
|
|
|
|
inline ssize_t portableRecv(socket_t s, uint8_t* buf, size_t len) {
|
|
return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
|
|
}
|
|
|
|
} // namespace net
|
|
} // namespace wowee
|