Skip to content

Commit c9130b9

Browse files
mobrembskiMichał Obrembski
authored and
Michał Obrembski
committed
Added setConnectionTimeout to EthernetClient for Linux
1 parent c326e38 commit c9130b9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

hal/architecture/Linux/drivers/core/EthernetClient.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
#include <errno.h>
3434
#include "log.h"
3535

36-
EthernetClient::EthernetClient() : _sock(-1)
36+
EthernetClient::EthernetClient() : _sock(-1), socketTimeout(1000)
3737
{
3838
}
3939

40-
EthernetClient::EthernetClient(int sock) : _sock(sock)
40+
EthernetClient::EthernetClient(int sock) : _sock(sock), socketTimeout(1000)
4141
{
4242
}
4343

@@ -89,6 +89,13 @@ int EthernetClient::connect(const char* host, uint16_t port)
8989
continue;
9090
}
9191

92+
// Sets the socket timeout
93+
struct timeval timeout;
94+
timeout.tv_sec = 0;
95+
timeout.tv_usec = socketTimeout * 1000000;
96+
setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
97+
setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
98+
9299
break;
93100
}
94101

hal/architecture/Linux/drivers/core/EthernetClient.h

+9
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,21 @@ class EthernetClient : public Client
200200
{
201201
return !this->operator==(rhs);
202202
};
203+
/**
204+
* @brief Set socket timeout.
205+
*
206+
*/
207+
void setConnectionTimeout(uint16_t timeoutInMilis)
208+
{
209+
socketTimeout = timeoutInMilis;
210+
};
203211

204212
friend class EthernetServer;
205213

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

211220
#endif

0 commit comments

Comments
 (0)