Skip to content

Commit

Permalink
Make ezsocket connection timeout use preference
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Jun 29, 2018
1 parent a4b7cb6 commit aee72cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/ezsockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool SetSocketBlocking(int fd, bool blocking)
#endif
}

static Preference<unsigned int> timeoutSeconds("ConnectionSecondsTimeout", 5);
static Preference<unsigned int> timeoutMiliseconds("ConnectionMilisecondsTimeout", 7000);

// Returns a timeval set to the given number of milliseconds.
inline timeval timevalFromMs(unsigned int ms)
Expand All @@ -81,6 +81,7 @@ EzSockets::EzSockets()
times = new timeval;
times->tv_sec = 0;
times->tv_usec = 0;
timeout = timevalFromMs(timeoutMiliseconds/4); // We try 4 different types so max timeout is 4*this
state = skDISCONNECTED;
}

Expand Down Expand Up @@ -121,9 +122,9 @@ bool EzSockets::create(int Protocol, int Type)
sock = socket(AF_INET, Type, Protocol);
if (sock > SOCKET_NONE) {
#if defined(WIN32)
int tv = timeoutSeconds * 1000;
int tv = timeoutMiliseconds;
#else
struct timeval tv;
struct timeval tv = timevalFromMs(timeoutMiliseconds);
tv.tv_sec = timeoutSeconds;
tv.tv_usec = 0;
#endif
Expand Down Expand Up @@ -220,13 +221,10 @@ bool EzSockets::CanConnect(const std::string& host, unsigned short port)
FD_ZERO(&readfds);
FD_SET(sock.sock, &readfds);
SetSocketBlocking(sock.sock, true);
timeval times;
times.tv_sec = 1;
times.tv_usec = 0;
auto write = select(sock.sock + 1, &readfds, nullptr, nullptr, &times);
auto write = select(sock.sock + 1, &readfds, nullptr, nullptr, &(sock.timeout));
FD_ZERO(&readfds);
FD_SET(sock.sock, &readfds);
auto read = select(sock.sock + 1, nullptr, &readfds, nullptr, &times);
auto read = select(sock.sock + 1, nullptr, &readfds, nullptr, &(sock.timeout));
bool select = read > -1 && write > -1;
if (!select) {
sock.close();
Expand Down Expand Up @@ -264,12 +262,7 @@ bool EzSockets::connect(const std::string& host, unsigned short port)
FD_ZERO(&readfds);
FD_SET(sock, &readfds);

timeval times;

times.tv_sec = 5;
times.tv_usec = 5000;

auto ret = select(sock + 1, nullptr, &readfds, nullptr, &times);
auto ret = select(sock + 1, nullptr, &readfds, nullptr, &timeout);

SetSocketBlocking(sock, true);

Expand Down
1 change: 1 addition & 0 deletions src/ezsockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class EzSockets
// Used for Select() command
fd_set *scks;
timeval *times;
timeval timeout; // Used for setsockopt and connect() select

// Buffers
};
Expand Down

0 comments on commit aee72cd

Please sign in to comment.