Skip to content

Commit

Permalink
Add Win32 socket error code to cnex
Browse files Browse the repository at this point in the history
  • Loading branch information
gitlarryf authored and ghewgill committed Feb 28, 2024
1 parent bf5bb08 commit 22d634b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 21 additions & 3 deletions exec/cnex/lib/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ static SOCKET *check_socket(TExecutor *exec, Object *o)
return o->ptr;
}

static const char *socket_error_message(int err)
{
#ifdef _WIN32
static char msgbuf[256];
DWORD r = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf,
sizeof(msgbuf),
NULL);
if (r == 0) {
snprintf(msgbuf, sizeof(msgbuf), "(No Error Text) - %d", err);
}
return msgbuf;
#endif
return strerror(err);
}


#ifdef _WIN32
static void initWinsock()
Expand Down Expand Up @@ -251,15 +270,14 @@ void net_socket_recv(TExecutor *exec)
}
int r = recv(s, (char*)buf, n, 0);
if (r < 0) {
perror("recv");
int err = errno;
int err = socket_error();
Cell *ret = cell_createArrayCell(2);
Cell *t = cell_arrayIndexForWrite(ret, 0);
t->type = cNumber;
t->number = number_from_uint32(CHOICE_RecvResult_error);
t = cell_arrayIndexForWrite(ret, 1);
t->type = cString;
t->string = string_createCString(strerror(err));
t->string = string_createCString(socket_error_message(err));
push(exec->stack, ret);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions exec/cnex/lib/socketx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma comment(lib, "ws2_32.lib") // Include the wsock32 (version 2) library, automatically on Windows builds.
typedef int socklen_t;
#pragma warning(disable: 4127) // incompatible with FD_SET()
#define socket_error() WSAGetLastError()

#else

Expand All @@ -19,6 +20,7 @@ typedef int socklen_t;

#define closesocket(x) close(x)
#define INVALID_SOCKET -1
#define socket_error() errno

typedef int SOCKET;

Expand Down

0 comments on commit 22d634b

Please sign in to comment.