Skip to content

Commit 667816e

Browse files
Jeroen88d-a-v
Jeroen88
authored andcommitted
BearSSL Max Fragment Length Negotation and Node.js server (#5929)
* Minor bug fixes in Maximum Fragment Length Negotation example, mainly giving background processes some time in fetch() * Minor layout changes to pass travis tests * Use PolledTimeout for timeout
1 parent f950d53 commit 667816e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

libraries/ESP8266WiFi/examples/BearSSL_MaxFragmentLength/BearSSL_MaxFragmentLength.ino

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Released to the public domain
66

77
#include <ESP8266WiFi.h>
8+
#include <PolledTimeout.h>
89

910
#ifndef STASSID
1011
#define STASSID "your-ssid"
@@ -17,17 +18,22 @@ const char *pass = STAPSK;
1718
void fetch(BearSSL::WiFiClientSecure *client) {
1819
client->write("GET / HTTP/1.0\r\nHost: tls.mbed.org\r\nUser-Agent: ESP8266\r\n\r\n");
1920
client->flush();
20-
uint32_t to = millis() + 5000;
21+
using oneShot = esp8266::polledTimeout::oneShot;
22+
oneShot timeout(5000);
2123
do {
2224
char tmp[32];
23-
memset(tmp, 0, 32);
2425
int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1);
2526
yield();
2627
if (rlen < 0) {
2728
break;
2829
}
30+
if (rlen == 0) {
31+
delay(10); // Give background processes some time
32+
continue;
33+
}
34+
tmp[rlen] = '\0';
2935
Serial.print(tmp);
30-
} while (millis() < to);
36+
} while (!timeout);
3137
client->stop();
3238
Serial.printf("\n-------\n");
3339
}
@@ -73,11 +79,11 @@ int fetchMaxFragmentLength() {
7379

7480
BearSSL::WiFiClientSecure client;
7581
client.setInsecure();
76-
bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024);
82+
bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 512);
7783
Serial.printf("\nConnecting to https://tls.mbed.org\n");
7884
Serial.printf("MFLN supported: %s\n", mfln ? "yes" : "no");
7985
if (mfln) {
80-
client.setBufferSizes(1024, 1024);
86+
client.setBufferSizes(512, 512);
8187
}
8288
client.connect("tls.mbed.org", 443);
8389
if (client.connected()) {
@@ -125,6 +131,6 @@ void loop() {
125131
yield();
126132

127133
Serial.printf("\n\n");
128-
Serial.printf("Default SSL: %d bytes used\n", a);
129-
Serial.printf("1024 byte MFLN SSL: %d bytes used\n", b);
134+
Serial.printf("Default SSL: %d bytes used\n", a);
135+
Serial.printf("512 byte MFLN SSL: %d bytes used\n", b);
130136
}

0 commit comments

Comments
 (0)