Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meaningful Win32 socket error code to cnex #446

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading