Unverified Commit f80fc9bd authored by mszarma's avatar mszarma Committed by GitHub
Browse files

Use timeval to set up SO_RCVTIMEO (#1734)



Refactor code to use timeval struct
for setting up the SO_RCVTIMEO variable
timeout value for sockets in case of Linux.
Changed SetTimeout API timeout input to seconds.
Co-authored-by: default avatarChao Ma <mctt90@gmail.com>
parent 4ddd477f
......@@ -190,7 +190,7 @@ bool SocketReceiver::Wait(const char* addr, int num_sender) {
}
// Initialize socket and socket-thread
server_socket_ = new TCPSocket();
server_socket_->SetTimeout(kTimeOut * 60 * 1000); // millsec
server_socket_->SetTimeout(kTimeOut); // seconds
// Bind socket
if (server_socket_->Bind(ip.c_str(), port) == false) {
LOG(FATAL) << "Cannot bind to " << ip << ":" << port;
......
......@@ -21,7 +21,7 @@ namespace dgl {
namespace network {
static constexpr int kMaxTryCount = 1024; // maximal connection: 1024
static constexpr int kTimeOut = 10; // 10 minutes for socket timeout
static constexpr int kTimeOut = 10 * 60; // 10 minutes (in seconds) for socket timeout
static constexpr int kMaxConnection = 1024; // maximal connection: 1024
/*!
......
......@@ -137,8 +137,17 @@ bool TCPSocket::SetBlocking(bool flag) {
#endif // _WIN32
void TCPSocket::SetTimeout(int timeout) {
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<char*>(&timeout), sizeof(timeout));
#ifdef _WIN32
timeout = timeout * 1000; // WIN API accepts millsec
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<char*>(&timeout), sizeof(timeout));
#else // !_WIN32
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO,
&tv, sizeof(tv));
#endif // _WIN32
}
bool TCPSocket::ShutDown(int ways) {
......
......@@ -79,7 +79,7 @@ class TCPSocket {
/*!
* \brief Set timeout for socket
* \param timeout millsec timeout
* \param timeout seconds timeout
*/
void SetTimeout(int timeout);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment