Further progress toward working linux version

This commit is contained in:
2021-12-07 17:28:53 -05:00
parent 608274a295
commit 147c7f2df4
4 changed files with 167 additions and 173 deletions

View File

@@ -31,6 +31,12 @@ public:
return (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);
}
void set_nonblocking(SOCKET sock) {
u_long mode = 1; // 1 to enable non-blocking socket
int status = ioctlsocket(sock, FIONBIO, &mode);
assert(status == 0);
}
static PADDRINFOA find_good_addr(PADDRINFOA addrinfo) {
for (PADDRINFOA addr = addrinfo; addr != nullptr; addr = addr->ai_next) {
if (addr->ai_family == AF_INET) {
@@ -40,12 +46,6 @@ public:
return nullptr;
}
void set_nonblocking(SOCKET sock) {
u_long mode = 1; // 1 to enable non-blocking socket
int status = ioctlsocket(sock, FIONBIO, &mode);
assert(status == 0);
}
SOCKET open_connection(const std::string &target, std::string &err) {
PADDRINFOA addrs = nullptr;
PADDRINFOA goodaddr = nullptr;
@@ -57,7 +57,6 @@ public:
int status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
while (status == WSATRY_AGAIN) {
status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
goto error;
}
if (status == WSAHOST_NOT_FOUND) {
err = "host not found";
@@ -218,7 +217,7 @@ public:
}
void handle_clock() {
int now = get_now() - basetime_;
int64_t now = get_now() - basetime_;
driven_->drv_set_clock(double(now) / 10000000.0);
}
@@ -231,6 +230,33 @@ public:
short_sleep_ = true;
}
void accept_connections(int port, SOCKET sock) {
while (true) {
SOCKET chsock = accept(sock, nullptr, nullptr);
if (chsock != INVALID_SOCKET) {
set_nonblocking(chsock);
int chid = driven_->drv_notify_accept(port);
socket_[chid] = chsock;
connected_[chid] = true;
short_sleep_ = true;
continue;
}
int errcode = WSAGetLastError();
if (errcode == WSAEWOULDBLOCK) {
return;
}
if (errcode == WSAECONNRESET) {
// The remote disconnected before we had a chance to accept.
// Just pretend it never happened.
continue;
}
// If a listening port fails in a non-transient way,
// we don't really have any good way of handling
// that.
assert(false);
}
}
bool calc_select_sets(fd_set &rfds, fd_set &wfds, fd_set &efds) const {
FD_ZERO(&rfds);
FD_ZERO(&wfds);
@@ -254,32 +280,6 @@ public:
return any;
}
void accept_connections(int port, SOCKET sock) {
while (true) {
SOCKET chsock = accept(sock, nullptr, nullptr);
if (chsock != INVALID_SOCKET) {
int chid = driven_->drv_notify_accept(port);
socket_[chid] = chsock;
connected_[chid] = true;
short_sleep_ = true;
continue;
}
int errcode = WSAGetLastError();
if (errcode == WSAEWOULDBLOCK) {
return;
}
if (errcode == WSAECONNRESET) {
// The remote disconnected before we had a chance to accept.
// Just pretend it never happened.
continue;
}
// If a listening port fails in a non-transient way,
// we don't really have any good way of handling
// that.
assert(false);
}
}
void handle_socket_input_output(int mstimeout) {
fd_set rfds, wfds, efds;
int nbytes; const char *bytes;