Skip to content

Allow setting TCP timeout #1312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ HTTPClient::HTTPClient() {
_returnCode = 0;
_size = -1;
_canReuse = false;
_tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;

}

Expand Down Expand Up @@ -252,6 +253,17 @@ void HTTPClient::setAuthorization(const char * auth) {
}
}

/**
* set the timeout for the TCP connection
* @param timeout unsigned int
*/
void HTTPClient::setTimeout(uint16_t timeout) {
_tcpTimeout = timeout;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may add:

if(connected()) {
 _tcp->setTimeout(timeout);
}

to allow changing timeout when the connection is already there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, thank you for that. I've added in your check.

if(connected()) {
_tcp->setTimeout(timeout);
}
}

/**
* send a GET request
* @return http code
Expand Down Expand Up @@ -673,7 +685,7 @@ bool HTTPClient::connect(void) {
}

// set Timeout for readBytesUntil and readStringUntil
_tcp->setTimeout(HTTPCLIENT_TCP_TIMEOUT);
_tcp->setTimeout(_tcpTimeout);

#ifdef ESP8266
_tcp->setNoDelay(true);
Expand Down
5 changes: 3 additions & 2 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define DEBUG_HTTPCLIENT(...)
#endif

#define HTTPCLIENT_TCP_TIMEOUT (1000)
#define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (1000)

/// HTTP client errors
#define HTTPC_ERROR_CONNECTION_REFUSED (-1)
Expand Down Expand Up @@ -127,6 +127,7 @@ class HTTPClient {
void setUserAgent(const char * userAgent);
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
void setTimeout(uint16_t timeout);

/// request handling
int GET();
Expand Down Expand Up @@ -170,7 +171,7 @@ class HTTPClient {
String _host;
uint16_t _port;
bool _reuse;

uint16_t _tcpTimeout;

String _url;
bool _https;
Expand Down