Skip to content

Commit 7d463ae

Browse files
authored
fix(windows): start Winsock at the first socket; 0.3.5 never did, every request failed (#26)
Socket::platform_init() existed and was never called. On Windows the first connect then failed with WSANOTINITIALISED, reported as status 0 'Connection failed'. Socket::ensure_platform_started() runs once per process at the top of connect (before socket() and getaddrinfo); Socket::platform_started() reports it; tests/test_winsock_start.cpp is the first socket of its process and checks both. Co-authored-by: Sunrisepeak <Sunrisepeak@users.noreply.github.com>
1 parent a312f63 commit 7d463ae

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎src/socket.cppm‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import :platform;
3030

3131
namespace mcpplibs::tinyhttps {
3232

33+
inline std::atomic<bool> platform_started_{ false };
34+
3335
#ifdef TINYHTTPS_WINSOCK
3436
using SocketHandle = SOCKET;
3537
constexpr SocketHandle INVALID_SOCKET_FD = INVALID_SOCKET;
@@ -117,6 +119,9 @@ public:
117119
}
118120

119121
bool connect(const char* host, int port, int timeoutMs) {
122+
// Winsock first: a socket or getaddrinfo before WSAStartup fails with
123+
// WSANOTINITIALISED, which reaches the caller as a bare "Connection failed".
124+
ensure_platform_started();
120125
// Close existing connection if any
121126
if (is_valid()) {
122127
close();
@@ -372,6 +377,22 @@ public:
372377
#endif
373378
}
374379

380+
// Winsock must be started before the first socket or getaddrinfo, and a
381+
// library cannot assume its user did it: 0.3.5 never called platform_init
382+
// itself, and on Windows every request then ended in "Connection failed"
383+
// (status 0, nothing saying why). Every path that opens a socket calls
384+
// this first; it runs once per process, and platform_started says whether.
385+
static void ensure_platform_started() {
386+
static const bool once = [] {
387+
platform_init();
388+
platform_started_.store(true);
389+
return true;
390+
}();
391+
(void)once;
392+
}
393+
394+
static bool platform_started() { return platform_started_.load(); }
395+
375396
static void platform_cleanup() {
376397
#ifdef TINYHTTPS_WINSOCK
377398
WSACleanup();

‎tests/test_winsock_start.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Winsock must be started before the first socket or getaddrinfo, and the
2+
// library starts it itself: 0.3.5 never did, and on Windows every request then
3+
// ended in "Connection failed" with no reason. This program is the first
4+
// socket of its process, so platform_started() is false until connect runs.
5+
#include <gtest/gtest.h>
6+
7+
import mcpplibs.tinyhttps;
8+
import std;
9+
10+
TEST(Winsock, StartedByTheFirstConnect) {
11+
EXPECT_FALSE(mcpplibs::tinyhttps::Socket::platform_started());
12+
mcpplibs::tinyhttps::Socket socket;
13+
// Nothing listens on port 1 of the loopback: the connection is refused
14+
// quickly, which is a started Winsock answering, not an unstarted one.
15+
socket.connect("127.0.0.1", 1, 500);
16+
EXPECT_TRUE(mcpplibs::tinyhttps::Socket::platform_started());
17+
}

0 commit comments

Comments
 (0)