Skip to content

Commit

Permalink
show specific error message in TCP accept/send/receive logs (#4128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Apr 29, 2021
1 parent fa6d356 commit f97aa86
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/network/socket_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,38 @@ class TcpSocket {
inline TcpSocket Accept() {
SOCKET newfd = accept(sockfd_, NULL, NULL);
if (newfd == INVALID_SOCKET) {
Log::Fatal("Socket accept error, code: %d", GetLastError());
int err_code = GetLastError();
#if defined(_WIN32)
Log::Fatal("Socket accept error (code: %d)", err_code);
#else
Log::Fatal("Socket accept error, %s (code: %d)", std::strerror(err_code), err_code);
#endif
}
return TcpSocket(newfd);
}

inline int Send(const char *buf_, int len, int flag = 0) {
int cur_cnt = send(sockfd_, buf_, len, flag);
if (cur_cnt == SOCKET_ERROR) {
Log::Fatal("Socket send error, code: %d", GetLastError());
int err_code = GetLastError();
#if defined(_WIN32)
Log::Fatal("Socket send error (code: %d)", err_code);
#else
Log::Fatal("Socket send error, %s (code: %d)", std::strerror(err_code), err_code);
#endif
}
return cur_cnt;
}

inline int Recv(char *buf_, int len, int flags = 0) {
int cur_cnt = recv(sockfd_, buf_ , len , flags);
if (cur_cnt == SOCKET_ERROR) {
Log::Fatal("Socket recv error, code: %d", GetLastError());
int err_code = GetLastError();
#if defined(_WIN32)
Log::Fatal("Socket recv error (code: %d)", err_code);
#else
Log::Fatal("Socket recv error, %s (code: %d)", std::strerror(err_code), err_code);
#endif
}
return cur_cnt;
}
Expand Down

0 comments on commit f97aa86

Please sign in to comment.