8000 Merge pull request #324 from Stormhand/master · chegewara/esp32-snippets@3ec8ea4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ec8ea4

Browse files
authored
Merge pull request nkolban#324 from Stormhand/master
Add timeout related to issue nkolban#322
2 parents 8a55390 + 2fbca3c commit 3ec8ea4

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

cpp_utils/HttpServer.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static const char* LOG_TAG = "HttpServer";
2828
HttpServer::HttpServer() {
2929
m_fileBufferSize = 4*1024; // Default size of the file buffer.
3030
m_portNumber = 80; // The default port number.
31+
m_clientTimeout = 5; // The default timeout 5 seconds.
3132
m_rootPath = ""; // The default path.
3233
m_useSSL = false; // Default SSL is no.
3334
setDirectoryListing(false); // Default directory listing is disabled.
@@ -159,6 +160,7 @@ class HttpServerTask: public Task {
159160
//Memory::checkIntegrity();
160161
try {
161162
clientSocket = m_pHttpServer->m_socket.accept(); // Block waiting for a new external client connection.
163+
clientSocket.setTimeout(m_pHttpServer->getClientTimeout());
162164
}
163165
catch(std::exception &e) {
164166
ESP_LOGE("HttpServerTask", "Caught an exception waiting for new client!");
@@ -329,6 +331,22 @@ void HttpServer::listDirectory(std::string path, HttpResponse& response) {
329331
response.close();
330332
} // listDirectory
331333

334+
/**
335+
* @brief Set different socket timeout for new connections.
336+
* @param [in] use Set to true to enable directory listing.
337+
*/
338+
void HttpServer::setClientTimeout(uint32_t timeout) {
339+
m_clientTimeout = timeout;
340+
}
341+
342+
/**
343+
* @brief Get current socket's timeout for new connections.
344+
* @param [in] use Set to true to enable directory listing.
345+
*/
346+
uint32_t HttpServer::getClientTimeout() {
347+
return m_clientTimeout;
348+
}
349+
332350
/**
333351
* @brief Set whether or not we will list directories.
334352
* @param [in] use Set to true to enable directory listing.

cpp_utils/HttpServer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class HttpServer {
8181
void setDirectoryListing(bool use); // Should we list the content of directories?
8282
void setFileBufferSize(size_t fileBufferSize); // Set the size of the file buffer
8383
void setRootPath(std::string path); // Set the root of the file system path.
84+
void setClientTimeout(uint32_t timeout); // Set client's socket timeout
85+
uint32_t getClientTimeout(); // Get client's socket timeout
8486
void start(uint16_t portNumber, bool useSSL=false);
8587
void stop(); // Stop a previously started server.
8688

@@ -95,6 +97,7 @@ class HttpServer {
9597
std::string m_rootPath; // Root path into the file system.
9698
Socket m_socket;
9799
bool m_useSSL; // Is this server listening on an HTTPS port?
100+
uint32_t m_clientTimeout; // Default Timeout
98101
}; // HttpServer
99102

100103
#endif /* COMPONENTS_CPP_UTILS_HTTPSERVER_H_ */

cpp_utils/Socket.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ Socket Socket::accept() {
6060
struct sockaddr addr;
6161
getBind(&addr);
6262
ESP_LOGD(LOG_TAG, ">> accept: Accepting on %s; sockFd: %d, using SSL: %d", addressToString(&addr).c_str(), m_sock, getSSL());
63-
64-
int clientSockFD = ::lwip_accept_r(m_sock, nullptr, nullptr);
63+
struct sockaddr_in client_addr;
64+
socklen_t sin_size;
65+
int clientSockFD = ::lwip_accept_r(m_sock, (struct sockaddr *)&client_addr, &sin_size);
66+
printf("------> new connection client %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
6567
if (clientSockFD == -1) {
6668
SocketException se(errno);
6769
ESP_LOGE(LOG_TAG, "accept(): %s, m_sock=%d", strerror(errno), m_sock);
@@ -272,6 +274,30 @@ bool Socket::operator <(const Socket& other) const {
272274
return m_sock < other.m_sock;
273275
}
274276

277+
int Socket::setSocketOption(int option, char* value, size_t len)
278+
{
279+
int res = setsockopt(m_sock, SOL_SOCKET, option, value, len);
280+
if(res < 0) {
281+
ESP_LOGE(LOG_TAG, "%X : %d", option, errno);
282+
}
283+
return res;
284+
}
285+
286+
/**
287+
* @brief Socket timeout.
288+
* @param [in] seconds to wait.
289+
*/
290+
int Socket::setTimeout(uint32_t seconds)
291+
{
292+
struct timeval tv;
293+
tv.tv_sec = seconds;
294+
tv.tv_usec = 0;
295+
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
296+
return -1;
297+
}
298+
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
299+
}
300+
275301

276302
std::string Socket::readToDelim(std::string delim) {
277303
std::string ret;

cpp_utils/Socket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class Socket {
6868
int connect(struct in_addr address, uint16_t port);
6969
int connect(char* address, uint16_t port);
7070
int createSocket(bool isDatagram = false);
71+
int setSocketOption(int option, char* value, size_t len);
72+
int setTimeout(uint32_t seconds);
7173
void getBind(struct sockaddr* pAddr);
7274
int getFD() const;
7375
bool getSSL() const;

0 commit comments

Comments
 (0)
0