-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
ESPLwIPClient::setTimeout conflict fix with Stream::setTimeout #6676
Changes from 12 commits
5564372
12699eb
c1183e4
2f49062
e6e77ff
b1193c2
6420e3a
bee59c1
b986041
675484c
77830cf
7d86e1d
0e1d13a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,8 @@ WiFiClientSecure::WiFiClientSecure(int sock) | |
{ | ||
_connected = false; | ||
_timeout = 30000; // Same default as ssl_client | ||
_lastReadTimeout = 0; | ||
_lastWriteTimeout = 0; | ||
|
||
sslclient = new sslclient_context; | ||
ssl_init(sslclient); | ||
|
@@ -94,6 +96,8 @@ void WiFiClientSecure::stop() | |
sslclient->socket = -1; | ||
_connected = false; | ||
_peek = -1; | ||
_lastReadTimeout = 0; | ||
_lastWriteTimeout = 0; | ||
} | ||
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key); | ||
} | ||
|
@@ -199,6 +203,16 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) | |
if (!_connected) { | ||
return 0; | ||
} | ||
if(_lastWriteTimeout != _timeout){ | ||
struct timeval timeout_tv; | ||
timeout_tv.tv_sec = _timeout / 1000; | ||
timeout_tv.tv_usec = (_timeout % 1000) * 1000; | ||
if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cosmethic change, but I suggest to use sizeof(timeout_tv) instead of sizeof(struct timeval) |
||
{ | ||
_lastWriteTimeout = _timeout; | ||
} | ||
} | ||
|
||
int res = send_ssl_data(sslclient, buf, size); | ||
if (res < 0) { | ||
stop(); | ||
|
@@ -209,6 +223,18 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) | |
|
||
int WiFiClientSecure::read(uint8_t *buf, size_t size) | ||
{ | ||
if(_lastReadTimeout != _timeout){ | ||
if(fd() >= 0){ | ||
struct timeval timeout_tv; | ||
timeout_tv.tv_sec = _timeout / 1000; | ||
timeout_tv.tv_usec = (_timeout % 1000) * 1000; | ||
if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cosmethic change, but I suggest to use sizeof(timeout_tv) instead of sizeof(struct timeval) |
||
{ | ||
_lastReadTimeout = _timeout; | ||
} | ||
} | ||
} | ||
|
||
int peeked = 0; | ||
int avail = available(); | ||
if ((!buf && size) || avail <= 0) { | ||
|
@@ -396,4 +422,3 @@ int WiFiClientSecure::fd() const | |
{ | ||
return sslclient->socket; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cosmethic change, but I suggest to use
sizeof(timeout_tv)
instead ofsizeof(struct timeval)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree, as it is good practice to actually use the struct/class for the
sizeof()
instead of the object.This way you are less likely to end up with the value
4
(or8
when compiling for a 64-bit platform) as result of yoursizeof()
call when working with pointers instead of reference.Thus this will reduce the chance of programming errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, when it comes to pointers... but imagine you change the type of
timeout_tv
somewhere earlier and forget to replace allsizeof (struct timeval)
occurences later in the code. Maybe a better thing is to declare a const variable/define/constexpr timeval_size just after thetimeout_tv
declaration and then use this variable later in the code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, or maybe there is a compiler flag which can warn when
sizeof()
is given a pointer as that's the actual problem.