Skip to content

Commit 08ca1d8

Browse files
committed
refine live http flow
1 parent 70fa959 commit 08ca1d8

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

examples/LiveNetworkBaseline/LiveNetworkBaseline.ino

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ unsigned long g_nextMqttAttemptAtMs = 0;
5353
unsigned long g_currentMqttRetryMs = kMqttRetryBaseMs;
5454
bool g_lastWiFiConnected = false;
5555

56+
void closeHttpClient() {
57+
#if defined(ARDUINO_ARCH_ESP8266)
58+
g_httpClient.abort();
59+
#else
60+
g_httpClient.stop();
61+
#endif
62+
}
63+
5664
unsigned long applyBackoffWithJitter(unsigned long nowMs,
5765
unsigned long baseDelayMs,
5866
unsigned long* currentDelayMs,
@@ -142,12 +150,12 @@ bool sendHttpNow(const char* body, size_t length) {
142150
return false;
143151
}
144152

145-
g_httpClient.stop();
153+
closeHttpClient();
146154
g_httpClient.setTimeout(kHttpTimeoutMs);
147155
const bool connected = g_httpAddressValid ? g_httpClient.connect(g_httpAddress, kHttpPort)
148156
: g_httpClient.connect(kHttpHost, kHttpPort);
149157
if (!connected) {
150-
g_httpClient.stop();
158+
closeHttpClient();
151159
return false;
152160
}
153161

@@ -172,7 +180,7 @@ bool sendHttpNow(const char* body, size_t length) {
172180
if (statusLine.length() > 0) {
173181
const bool ok =
174182
statusLine.startsWith("HTTP/1.1 2") || statusLine.startsWith("HTTP/1.0 2");
175-
g_httpClient.stop();
183+
closeHttpClient();
176184
return ok;
177185
}
178186
continue;
@@ -188,7 +196,7 @@ bool sendHttpNow(const char* body, size_t length) {
188196
#endif
189197
}
190198

191-
g_httpClient.stop();
199+
closeHttpClient();
192200
return false;
193201
}
194202

examples/LiveNetworkNode/LiveNetworkNode.ino

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,27 @@ void disconnectWiFi() {
112112
g_mqttClient.disconnect();
113113
}
114114

115-
ZeroHttpPump::StepResult httpConnectStep(const ZeroHttpPump::Request&, void*) {
115+
void closeHttpClient() {
116+
#if defined(ARDUINO_ARCH_ESP8266)
117+
g_httpClient.abort();
118+
#else
116119
g_httpClient.stop();
120+
#endif
121+
}
122+
123+
ZeroHttpPump::StepResult httpConnectStep(const ZeroHttpPump::Request&, void*) {
124+
closeHttpClient();
117125
g_httpClient.setTimeout(kHttpIoTimeoutMs);
118126
const bool connected = g_httpAddressValid ? g_httpClient.connect(g_httpAddress, kHttpPort)
119127
: g_httpClient.connect(kHttpHost, kHttpPort);
120128
if (!connected) {
129+
closeHttpClient();
121130
return ZeroHttpPump::kStepFailed;
122131
}
123132
g_httpRequestPrepared = false;
124133
g_httpResponseSawStatus = false;
125134
g_httpResponseSuccess = false;
126-
g_httpReadDeadlineMs = millis() + kHttpIoTimeoutMs;
135+
g_httpReadDeadlineMs = 0;
127136
g_httpStatusLineLength = 0;
128137
return ZeroHttpPump::kStepComplete;
129138
}
@@ -145,15 +154,19 @@ ZeroHttpPump::StepResult httpWriteStep(const ZeroHttpPump::Request& request, voi
145154
g_httpClient.print("\r\n\r\n");
146155
g_httpClient.write(reinterpret_cast<const uint8_t*>(request.body), request.bodyLength);
147156
g_httpRequestPrepared = true;
157+
g_httpReadDeadlineMs = millis() + kHttpIoTimeoutMs;
148158
}
149159

150160
return ZeroHttpPump::kStepComplete;
151161
}
152162

153163
ZeroHttpPump::StepResult httpReadStep(const ZeroHttpPump::Request&, void*) {
154164
if (!g_httpClient.connected() && !g_httpClient.available()) {
155-
return g_httpResponseSawStatus && g_httpResponseSuccess ? ZeroHttpPump::kStepComplete
156-
: ZeroHttpPump::kStepFailed;
165+
if (g_httpResponseSawStatus && g_httpResponseSuccess) {
166+
return ZeroHttpPump::kStepComplete;
167+
}
168+
closeHttpClient();
169+
return ZeroHttpPump::kStepFailed;
157170
}
158171

159172
while (g_httpClient.available()) {
@@ -170,7 +183,11 @@ ZeroHttpPump::StepResult httpReadStep(const ZeroHttpPump::Request&, void*) {
170183
(strncmp(g_httpStatusLine, "HTTP/1.1 2", 10) == 0) ||
171184
(strncmp(g_httpStatusLine, "HTTP/1.0 2", 10) == 0);
172185
g_httpStatusLineLength = 0;
173-
return g_httpResponseSuccess ? ZeroHttpPump::kStepComplete : ZeroHttpPump::kStepFailed;
186+
if (g_httpResponseSuccess) {
187+
return ZeroHttpPump::kStepComplete;
188+
}
189+
closeHttpClient();
190+
return ZeroHttpPump::kStepFailed;
174191
}
175192
}
176193
g_httpStatusLineLength = 0;
@@ -182,14 +199,15 @@ ZeroHttpPump::StepResult httpReadStep(const ZeroHttpPump::Request&, void*) {
182199
}
183200

184201
if (millis() > g_httpReadDeadlineMs) {
202+
closeHttpClient();
185203
return ZeroHttpPump::kStepFailed;
186204
}
187205

188206
return ZeroHttpPump::kStepPending;
189207
}
190208

191209
ZeroHttpPump::StepResult httpCloseStep(const ZeroHttpPump::Request&, void*) {
192-
g_httpClient.stop();
210+
closeHttpClient();
193211
g_httpRequestPrepared = false;
194212
g_httpResponseSawStatus = false;
195213
g_httpResponseSuccess = false;

0 commit comments

Comments
 (0)