WebServer.handleClient delay (#4350)

* If WebServer.handleClient is run in a tight loop, it will starve other processes.  So, if there is no connection, throw in a delay(1).  Fixes #4348

* Made a variable to control the delay behavior
This commit is contained in:
lbernstone 2020-10-01 06:42:23 -06:00 committed by GitHub
parent 2243081f85
commit 9e7b13e46d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -45,6 +45,7 @@ WebServer::WebServer(IPAddress addr, int port)
, _currentVersion(0) , _currentVersion(0)
, _currentStatus(HC_NONE) , _currentStatus(HC_NONE)
, _statusChange(0) , _statusChange(0)
, _nullDelay(true)
, _currentHandler(nullptr) , _currentHandler(nullptr)
, _firstHandler(nullptr) , _firstHandler(nullptr)
, _lastHandler(nullptr) , _lastHandler(nullptr)
@ -66,6 +67,7 @@ WebServer::WebServer(int port)
, _currentVersion(0) , _currentVersion(0)
, _currentStatus(HC_NONE) , _currentStatus(HC_NONE)
, _statusChange(0) , _statusChange(0)
, _nullDelay(true)
, _currentHandler(nullptr) , _currentHandler(nullptr)
, _firstHandler(nullptr) , _firstHandler(nullptr)
, _lastHandler(nullptr) , _lastHandler(nullptr)
@ -280,6 +282,9 @@ void WebServer::handleClient() {
if (_currentStatus == HC_NONE) { if (_currentStatus == HC_NONE) {
WiFiClient client = _server.available(); WiFiClient client = _server.available();
if (!client) { if (!client) {
if (_nullDelay) {
delay(1);
}
return; return;
} }
@ -370,6 +375,10 @@ void WebServer::setContentLength(const size_t contentLength) {
_contentLength = contentLength; _contentLength = contentLength;
} }
void WebServer::enableDelay(boolean value) {
_nullDelay = value;
}
void WebServer::enableCORS(boolean value) { void WebServer::enableCORS(boolean value) {
_corsEnabled = value; _corsEnabled = value;
} }

View File

@ -123,6 +123,7 @@ public:
void send_P(int code, PGM_P content_type, PGM_P content); void send_P(int code, PGM_P content_type, PGM_P content);
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength); void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
void enableDelay(boolean value);
void enableCORS(boolean value = true); void enableCORS(boolean value = true);
void enableCrossOrigin(boolean value = true); void enableCrossOrigin(boolean value = true);
@ -176,6 +177,7 @@ protected:
uint8_t _currentVersion; uint8_t _currentVersion;
HTTPClientStatus _currentStatus; HTTPClientStatus _currentStatus;
unsigned long _statusChange; unsigned long _statusChange;
boolean _nullDelay;
RequestHandler* _currentHandler; RequestHandler* _currentHandler;
RequestHandler* _firstHandler; RequestHandler* _firstHandler;