Skip to content

Commit

Permalink
Added setConnectionTimeout to EthernetClient for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
mobrembski authored and Michał Obrembski committed Nov 20, 2020
1 parent 6b5f47c commit 7ed332d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions hal/architecture/Linux/drivers/core/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
#include <errno.h>
#include "log.h"

EthernetClient::EthernetClient() : _sock(-1)
EthernetClient::EthernetClient() : _sock(-1), socketTimeout(1000)
{
}

EthernetClient::EthernetClient(int sock) : _sock(sock)
EthernetClient::EthernetClient(int sock) : _sock(sock), socketTimeout(1000)
{
}

Expand Down Expand Up @@ -89,6 +89,13 @@ int EthernetClient::connect(const char* host, uint16_t port)
continue;
}

// Sets the socket timeout
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = socketTimeout * 1000000;
setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));

break;
}

Expand Down
9 changes: 9 additions & 0 deletions hal/architecture/Linux/drivers/core/EthernetClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,21 @@ class EthernetClient : public Client
{
return !this->operator==(rhs);
};
/**
* @brief Set socket timeout.
*
*/
void setConnectionTimeout(uint16_t timeoutInMilis)
{
socketTimeout = timeoutInMilis;
};

friend class EthernetServer;

private:
int _sock; //!< @brief Network socket file descriptor.
IPAddress _srcip; //!< @brief Local ip to bind to.
uint16_t socketTimeout; //!< @brief Socket timeout in miliseconds.
};

#endif

0 comments on commit 7ed332d

Please sign in to comment.