Skip to content

Commit

Permalink
Proper look for connection.
Browse files Browse the repository at this point in the history
See discussion here: esp8266/Arduino#5257
  • Loading branch information
TD-er committed Oct 19, 2018
1 parent d0cf6de commit 1daed05
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/_C003.ino
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool do_process_c003_delay_queue(int controller_number, const C003_queue_element
client.print(" \n");

unsigned long timer = millis() + 200;
while (!client.available() && !timeOutReached(timer))
while (!client_available(client) && !timeOutReached(timer))
delay(1);

timer = millis() + 1000;
Expand All @@ -102,7 +102,7 @@ bool do_process_c003_delay_queue(int controller_number, const C003_queue_element
addLog(LOG_LEVEL_DEBUG, log);
client.println(SecuritySettings.ControllerPassword[element.controller_idx]);
delay(100);
while (client.available())
while (client_available(client))
client.read();

strcpy_P(log, PSTR("TELNT: Sending cmd"));
Expand Down
4 changes: 2 additions & 2 deletions src/_C012.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ boolean Blynk_get(const String& command, byte controllerIndex, float *data )
boolean success = !ControllerSettings.MustCheckReply;
if (ControllerSettings.MustCheckReply || data) {
unsigned long timer = millis() + 200;
while (!client.available() && !timeOutReached(timer))
yield();
while (!client_available(client) && !timeOutReached(timer))
delay(1);

char log[80] = {0};

Expand Down
51 changes: 30 additions & 21 deletions src/_CPlugin_Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,26 +402,30 @@ bool safeReadStringUntil(Stream &input, String &str, char terminator, unsigned i

do {
//read character
c = input.read();
if (c >= 0) {
//found terminator, we're ok
if (c == terminator) {
return(true);
}
//found character, add to string
else{
str += char(c);
//string at max size?
if (str.length() >= maxSize) {
addLog(LOG_LEVEL_ERROR, F("Not enough bufferspace to read all input data!"));
return(false);
}
}
}
// We must run the backgroundtasks every now and then.
if (timeOutReached(backgroundtasks_timer)) {
backgroundtasks_timer += 10;
backgroundtasks();
if (input.available()) {
c = input.read();
if (c >= 0) {
//found terminator, we're ok
if (c == terminator) {
return(true);
}
//found character, add to string
else{
str += char(c);
//string at max size?
if (str.length() >= maxSize) {
addLog(LOG_LEVEL_ERROR, F("Not enough bufferspace to read all input data!"));
return(false);
}
}
}
// We must run the backgroundtasks every now and then.
if (timeOutReached(backgroundtasks_timer)) {
backgroundtasks_timer += 10;
backgroundtasks();
} else {
yield();
}
} else {
yield();
}
Expand Down Expand Up @@ -621,6 +625,11 @@ bool try_connect_host(int controller_number, WiFiClient& client, ControllerSetti
// See: https://github.com/esp8266/Arduino/pull/5113
// https://github.com/esp8266/Arduino/pull/1829
bool client_available(WiFiClient& client) {
#ifdef ESP32
yield();
#else
esp_yield(); // Could be called from events
#endif
return client.available() || client.connected();
}

Expand All @@ -631,7 +640,7 @@ bool send_via_http(const String& logIdentifier, WiFiClient& client, const String

if (must_check_reply) {
unsigned long timer = millis() + 200;
while (!client.available()) {
while (!client_available(client)) {
if (timeOutReached(timer)) return false;
delay(1);
}
Expand Down

0 comments on commit 1daed05

Please sign in to comment.