Skip to content

Commit

Permalink
Use select for Winsock, fix CMakeLists
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaivel committed May 30, 2024
1 parent 1e8f3d2 commit 78c5a12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 27 additions & 2 deletions src/network/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 78c5a12

Please sign in to comment.