Skip to content

Commit

Permalink
code instrumented
Browse files Browse the repository at this point in the history
  • Loading branch information
Foo committed Mar 31, 2024
1 parent 9551334 commit 2bb4b54
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/src/SocketFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include <fcntl.h>
#endif

#include <iostream>

namespace MinimalSocket {
namespace {
#ifdef _WIN32
#define REBIND_OPTION SO_REUSEADDR
#else
#elif defined(__unix__) || defined(__APPLE__)
#define REBIND_OPTION SO_REUSEPORT
#endif
} // namespace
Expand Down Expand Up @@ -148,31 +150,40 @@ void turnToNonBlocking(SocketID socket_id) {
#elif defined(__unix__) || defined(__APPLE__)
// https://jameshfisher.com/2017/04/05/set_socket_nonblocking/
int flags = ::fcntl(socket_id, F_GETFL);
std::cout << "=====> 0" << std::endl;
if (::fcntl(socket_id, F_SETFL, flags | O_NONBLOCK) == -1) {
std::cout << "=====> 1" << std::endl;
throw Error{"Unable to set up the non blocking mode"};
}
#endif
std::cout << "=====> 2" << std::endl;
}

int isTimeoutErrorCode(int code) {
return
#ifdef _WIN32
code == WSAETIMEDOUT || code == WSAEWOULDBLOCK;
#else
#elif defined(__unix__)
code == EAGAIN || code == EWOULDBLOCK;
#elif defined(__APPLE__)
code == EAGAIN;
#endif
}

bool checkResult(int value, int errorValue, const std::string &error_msg,
bool canBeTimedOut) {
if (value != errorValue) {
std::cout << "=====> 3" << std::endl;
return false;
}
std::cout << "=====> 4" << std::endl;
SocketError maybe_error(error_msg);
if (canBeTimedOut && isTimeoutErrorCode(maybe_error.getErrorCode())) {
std::cout << "=====> 5" << std::endl;
// just out of time: tolerable
return true;
}
std::cout << "=====> 6" << std::endl;
throw maybe_error;
}
} // namespace MinimalSocket

0 comments on commit 2bb4b54

Please sign in to comment.