move streamapi into a thread, saves power and increases responsiveness

This commit is contained in:
Kevin Hester
2021-03-25 06:15:15 +08:00
parent e17fe7e075
commit b20930c111
13 changed files with 121 additions and 114 deletions

View File

@@ -40,18 +40,20 @@ void WiFiServerAPI::onConnectionChanged(bool connected)
}
/// override close to also shutdown the TCP link
void WiFiServerAPI::close() {
void WiFiServerAPI::close()
{
client.stop(); // drop tcp connection
StreamAPI::close();
}
bool WiFiServerAPI::loop()
int32_t WiFiServerAPI::runOnce()
{
if (client.connected()) {
StreamAPI::loop();
return true;
return StreamAPI::runOnce();
} else {
return false;
DEBUG_MSG("Client dropped connection, suspending API service\n");
enabled = false; // we no longer need to run
return 0;
}
}
@@ -78,18 +80,5 @@ int32_t WiFiServerPort::runOnce()
openAPI = new WiFiServerAPI(client);
}
if (openAPI) {
// Allow idle processing so the API can read from its incoming stream
if(!openAPI->loop()) {
// If our API link was up, shut it down
DEBUG_MSG("Client dropped connection, closing API client\n");
// Note: we can't call delete here because this object includes other state
// besides the stream API. Instead kill it later when we start a new instance
delete openAPI;
openAPI = NULL;
}
return 0; // run fast while our API server is running
} else
return 100; // only check occasionally for incoming connections
return 100; // only check occasionally for incoming connections
}

View File

@@ -1,7 +1,6 @@
#pragma once
#include "StreamAPI.h"
#include "concurrency/OSThread.h"
#include <WiFi.h>
/**
@@ -18,15 +17,14 @@ class WiFiServerAPI : public StreamAPI
virtual ~WiFiServerAPI();
/// @return true if we want to keep running, or false if we are ready to be destroyed
virtual bool loop(); // Check for dropped client connections
/// override close to also shutdown the TCP link
virtual void close();
protected:
/// Hookable to find out when connection changes
virtual void onConnectionChanged(bool connected);
virtual int32_t runOnce(); // Check for dropped client connections
};
/**