diff --git a/luprex/core/cpp/driver-common.cpp b/luprex/core/cpp/driver-common.cpp index 801ce63e..4ca10a19 100644 --- a/luprex/core/cpp/driver-common.cpp +++ b/luprex/core/cpp/driver-common.cpp @@ -380,12 +380,14 @@ public: struct pollfd &pfd = pollvec[index++]; pfd.fd = p.second; pfd.events = POLLIN; + pfd.revents = 0; } for (const ChanInfo &chan : chans_) { struct pollfd &pfd = pollvec[index++]; assert(chan.socket != INVALID_SOCKET); pfd.fd = chan.socket; - pfd.events = POLLERR; + pfd.events = 0; + pfd.revents = 0; if (chan.ready_now) mstimeout = 0; if (chan.just_released) mstimeout = 0; if (chan.ready_on_pollin) pfd.events |= POLLIN; @@ -413,7 +415,7 @@ public: struct pollfd &pfd = pollvec[index++]; bool pollin = ((pfd.revents & POLLIN) != 0); bool pollout = ((pfd.revents & POLLOUT) != 0); - bool pollerr = ((pfd.revents & POLLERR) != 0); + bool pollerr = ((pfd.revents & (POLLERR | POLLHUP)) != 0); if (chan.ready_now || pollerr || chan.just_released || (chan.ready_on_pollin && pollin) || (chan.ready_on_pollout && pollout) || @@ -433,6 +435,7 @@ public: } void drive(DrivenEngine *de, int argc, char *argv[]) { + socket_init(); SSL_load_error_strings(); ERR_load_crypto_strings(); enable_tty_raw(); @@ -476,6 +479,7 @@ public: SSL_CTX_free(ssl_ctx_with_root_certs_); SSL_CTX_free(ssl_ctx_with_server_certs_); DrivenEngine::set(nullptr); + socket_uninit(); } }; diff --git a/luprex/core/cpp/driver-mingw.cpp b/luprex/core/cpp/driver-mingw.cpp index c39a62e0..ff4ca8c5 100644 --- a/luprex/core/cpp/driver-mingw.cpp +++ b/luprex/core/cpp/driver-mingw.cpp @@ -35,6 +35,12 @@ static void enable_tty_raw() { // Do nothing on windows. } +static std::string winsock_error_string(int errcode) { + std::ostringstream oss; + oss << "error " << errcode; + return oss.str(); +} + static PADDRINFOA find_good_addr(PADDRINFOA addrinfo) { for (PADDRINFOA addr = addrinfo; addr != nullptr; addr = addr->ai_next) { if (addr->ai_family == AF_INET) { @@ -117,6 +123,7 @@ SOCKET listen_on_port(int port, std::string &err) { goto error; } set_nonblocking(sock); + std::cerr << "listening socket is " << sock << std::endl; return sock; error: @@ -128,6 +135,7 @@ static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) { SOCKET chsock = accept(listen_socket, nullptr, nullptr); if (chsock != INVALID_SOCKET) { set_nonblocking(chsock); + std::cerr << "accepted socket is " << chsock << std::endl; return chsock; } else { int errcode = WSAGetLastError(); @@ -182,13 +190,25 @@ static int socket_close(SOCKET socket) { static int socket_poll(PollVector &pollvec, int mstimeout, std::string &err) { int status = WSAPoll(&pollvec[0], pollvec.size(), mstimeout); if (status < 0) { - WSAGetLastError(); - err = "poll failed"; + err = winsock_error_string(WSAGetLastError()); return -1; } return status; } +static void socket_init() { + WSADATA data; + int errcode = WSAStartup(2, &data); + if (errcode != 0) { + fprintf(stderr, "Winsock didn't initalize, error %d", errcode); + exit(1); + } +} + +static void socket_uninit() { + // Nothing needed. +} + static int console_write(const char *bytes, int nbytes) { if (nbytes == 0) return 0; HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);