|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov |
| 3 | + |
| 4 | +// |
| 5 | +// Shows how to use request continuation to pause a request for a long processing task, and be able to resume it later. |
| 6 | +// |
| 7 | + |
| 8 | +#include <Arduino.h> |
| 9 | +#ifdef ESP32 |
| 10 | +#include <AsyncTCP.h> |
| 11 | +#include <WiFi.h> |
| 12 | +#elif defined(ESP8266) |
| 13 | +#include <ESP8266WiFi.h> |
| 14 | +#include <ESPAsyncTCP.h> |
| 15 | +#elif defined(TARGET_RP2040) |
| 16 | +#include <WebServer.h> |
| 17 | +#include <WiFi.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +#include <ESPAsyncWebServer.h> |
| 21 | + |
| 22 | +#include <list> |
| 23 | +#include <memory> |
| 24 | +#include <mutex> |
| 25 | + |
| 26 | +static AsyncWebServer server(80); |
| 27 | + |
| 28 | +// =============================================================== |
| 29 | +// The code below is used to simulate some long running operations |
| 30 | +// =============================================================== |
| 31 | + |
| 32 | +typedef struct { |
| 33 | + size_t id; |
| 34 | + AsyncWebServerRequestPtr requestPtr; |
| 35 | + uint8_t data; |
| 36 | +} LongRunningOperation; |
| 37 | + |
| 38 | +static std::list<std::unique_ptr<LongRunningOperation>> longRunningOperations; |
| 39 | +static size_t longRunningOperationsCount = 0; |
| 40 | +#ifdef ESP32 |
| 41 | +static std::mutex longRunningOperationsMutex; |
| 42 | +#endif |
| 43 | + |
| 44 | +static void startLongRunningOperation(AsyncWebServerRequestPtr &&requestPtr) { |
| 45 | +#ifdef ESP32 |
| 46 | + std::lock_guard<std::mutex> lock(longRunningOperationsMutex); |
| 47 | +#endif |
| 48 | + |
| 49 | + // LongRunningOperation *op = new LongRunningOperation(); |
| 50 | + std::unique_ptr<LongRunningOperation> op(new LongRunningOperation()); |
| 51 | + op->id = ++longRunningOperationsCount; |
| 52 | + op->data = 10; |
| 53 | + |
| 54 | + // you need to hold the AsyncWebServerRequestPtr returned by pause(); |
| 55 | + // This object is authorized to leave the scope of the request handler. |
| 56 | + op->requestPtr = std::move(requestPtr); |
| 57 | + |
| 58 | + Serial.printf("[%u] Start long running operation for %" PRIu8 " seconds...\n", op->id, op->data); |
| 59 | + longRunningOperations.push_back(std::move(op)); |
| 60 | +} |
| 61 | + |
| 62 | +static bool processLongRunningOperation(LongRunningOperation *op) { |
| 63 | + // request was deleted ? |
| 64 | + if (op->requestPtr.expired()) { |
| 65 | + Serial.printf("[%u] Request was deleted - stopping long running operation\n", op->id); |
| 66 | + return true; // operation finished |
| 67 | + } |
| 68 | + |
| 69 | + // processing the operation |
| 70 | + Serial.printf("[%u] Long running operation processing... %" PRIu8 " seconds left\n", op->id, op->data); |
| 71 | + |
| 72 | + // check if we have finished ? |
| 73 | + op->data--; |
| 74 | + if (op->data) { |
| 75 | + // not finished yet |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + // Try to get access to the request pointer if it is still exist. |
| 80 | + // If there has been a disconnection during that time, the pointer won't be valid anymore |
| 81 | + if (auto request = op->requestPtr.lock()) { |
| 82 | + Serial.printf("[%u] Long running operation finished! Sending back response...\n", op->id); |
| 83 | + request->send(200, "text/plain", String(op->id) + " "); |
| 84 | + |
| 85 | + } else { |
| 86 | + Serial.printf("[%u] Long running operation finished, but request was deleted!\n", op->id); |
| 87 | + } |
| 88 | + |
| 89 | + return true; // operation finished |
| 90 | +} |
| 91 | + |
| 92 | +/// ========================================================== |
| 93 | + |
| 94 | +void setup() { |
| 95 | + Serial.begin(115200); |
| 96 | + |
| 97 | +#ifndef CONFIG_IDF_TARGET_ESP32H2 |
| 98 | + WiFi.mode(WIFI_AP); |
| 99 | + WiFi.softAP("esp-captive"); |
| 100 | +#endif |
| 101 | + |
| 102 | + // Add a middleware to see how pausing a request affects the middleware chain |
| 103 | + server.addMiddleware([](AsyncWebServerRequest *request, ArMiddlewareNext next) { |
| 104 | + Serial.printf("Middleware chain start\n"); |
| 105 | + |
| 106 | + // continue to the next middleware, and at the end the request handler |
| 107 | + next(); |
| 108 | + |
| 109 | + // we can check the request pause state after the handler was executed |
| 110 | + if (request->isPaused()) { |
| 111 | + Serial.printf("Request was paused!\n"); |
| 112 | + } |
| 113 | + |
| 114 | + Serial.printf("Middleware chain ends\n"); |
| 115 | + }); |
| 116 | + |
| 117 | + // HOW TO RUN THIS EXAMPLE: |
| 118 | + // |
| 119 | + // 1. Open several terminals to trigger some requests concurrently that will be paused with: |
| 120 | + // > time curl -v http://192.168.4.1/ |
| 121 | + // |
| 122 | + // 2. Look at the output of the Serial console to see how the middleware chain is executed |
| 123 | + // and to see the long running operations being processed and resume the requests. |
| 124 | + // |
| 125 | + // 3. You can try close your curl command to cancel the request and check that the request is deleted. |
| 126 | + // Note: in case the network is disconnected, the request will be deleted. |
| 127 | + // |
| 128 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { |
| 129 | + // Print a message in case the request is disconnected (network disconnection, client close, etc.) |
| 130 | + request->onDisconnect([]() { |
| 131 | + Serial.printf("Request was disconnected!\n"); |
| 132 | + }); |
| 133 | + |
| 134 | + // Instruct ESPAsyncWebServer to pause the request and get a AsyncWebServerRequestPtr to be able to access the request later. |
| 135 | + // The AsyncWebServerRequestPtr is the ONLY object authorized to leave the scope of the request handler. |
| 136 | + // The Middleware chain will continue to run until the end after this handler exit, but the request will be paused and will not |
| 137 | + // be sent to the client until send() is called later. |
| 138 | + Serial.printf("Pausing request...\n"); |
| 139 | + AsyncWebServerRequestPtr requestPtr = request->pause(); |
| 140 | + |
| 141 | + // start our long operation... |
| 142 | + startLongRunningOperation(std::move(requestPtr)); |
| 143 | + }); |
| 144 | + |
| 145 | + server.begin(); |
| 146 | +} |
| 147 | + |
| 148 | +static uint32_t lastTime = 0; |
| 149 | + |
| 150 | +void loop() { |
| 151 | + if (millis() - lastTime >= 1000) { |
| 152 | + |
| 153 | +#ifdef ESP32 |
| 154 | + Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); |
| 155 | + std::lock_guard<std::mutex> lock(longRunningOperationsMutex); |
| 156 | +#endif |
| 157 | + |
| 158 | + // process all long running operations |
| 159 | + longRunningOperations.remove_if([](const std::unique_ptr<LongRunningOperation> &op) { |
| 160 | + return processLongRunningOperation(op.get()); |
| 161 | + }); |
| 162 | + |
| 163 | + lastTime = millis(); |
| 164 | + } |
| 165 | +} |
0 commit comments