diff --git a/CMakeLists.txt b/CMakeLists.txt index a363132..d6ddfa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,17 +2,18 @@ cmake_minimum_required(VERSION 3.5.0) project(sharedgl VERSION 0.5.0 LANGUAGES C CXX) include_directories(${CMAKE_SOURCE_DIR}/inc) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -march=native") # set(CMAKE_C_COMPILER "clang") IF(WIN32) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz") +ELSE() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -march=native") ENDIF(WIN32) -file(GLOB GLOBBED_SERVER_SOURCES CONFIGURE_DEPENDS "src/server/*.c" "src/network/*.c" "src/client/spinlock.c") +file(GLOB GLOBBED_SERVER_SOURCES CONFIGURE_DEPENDS "src/server/*.c" "src/network/*.c") # client stuff IF(UNIX) diff --git a/src/network/net.c b/src/network/net.c index 380c5d8..015452d 100644 --- a/src/network/net.c +++ b/src/network/net.c @@ -245,6 +245,32 @@ char *net_init_client(struct net_context **ctx, char *hostname, int port) #ifdef _WIN32 if (WSAGetLastError() != WSAEWOULDBLOCK) return error_messages[ERR_FAILED_TO_CONNECT]; + + fd_set writefds; + FD_ZERO(&writefds); + FD_SET(nctx->tcp_socket, &writefds); + + // 5 second timeout + struct timeval timeout; + timeout.tv_sec = 5; + timeout.tv_usec = 0; + + int result = select(0, NULL, &writefds, NULL, &timeout); + if (result == SOCKET_ERROR || result == 0) { + // select error or timeout + return error_messages[ERR_FAILED_TO_CONNECT]; + } else { + int error = 0; + int error_size = sizeof(error); + if (getsockopt(nctx->tcp_socket, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) == 0) { + // failed to connect + if (error != 0) + return error_messages[ERR_FAILED_TO_CONNECT]; + } else { + // getsockopt failed + return error_messages[ERR_FAILED_TO_CONNECT]; + } + } #else if (errno != EINPROGRESS) return error_messages[ERR_FAILED_TO_CONNECT]; @@ -390,9 +416,8 @@ long net_recv_udp_timeout(struct net_context *ctx, void *__restrict __buf, size_ static bool was_operation_invalid(ssize_t n) { #ifdef _WIN32 - // WSAENOTCONN should be a temp fix, bc without it we're likely not connected yet int error = WSAGetLastError(); - return (n == SOCKET_ERROR && error != WSAEWOULDBLOCK && error != WSAEINPROGRESS && error != WSAENOTCONN); + return (n == SOCKET_ERROR && error != WSAEWOULDBLOCK && error != WSAEINPROGRESS); #else return (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK); #endif