#include "ServerAPI.h" #include "configuration.h" #include template ServerAPI::ServerAPI(T &_client) : StreamAPI(&client), concurrency::OSThread("ServerAPI"), client(_client) { LOG_INFO("Incoming API connection"); } template ServerAPI::~ServerAPI() { client.stop(); } template void ServerAPI::close() { client.stop(); // drop tcp connection StreamAPI::close(); } /// Check the current underlying physical link to see if the client is currently connected template bool ServerAPI::checkIsConnected() { return client.connected(); } template int32_t ServerAPI::runOnce() { if (client.connected()) { return StreamAPI::runOncePart(); } else { LOG_INFO("Client dropped connection, suspend API service"); enabled = false; // we no longer need to run return 0; } } template APIServerPort::APIServerPort(int port) : U(port), concurrency::OSThread("ApiServer") {} template void APIServerPort::init() { U::begin(); } template int32_t APIServerPort::runOnce() { #ifdef ARCH_ESP32 #if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0) auto client = U::accept(); #else auto client = U::available(); #endif #elif defined(ARCH_RP2040) auto client = U::accept(); #else auto client = U::available(); #endif if (client) { // Close any previous connection (see FIXME in header file) if (openAPI) { #if RAK_4631 // RAK13800 Ethernet requests periodically take more time // This backoff addresses most cases keeping max wait < 1s // Reconnections are delayed by full wait time if (waitTime < 400) { waitTime *= 2; LOG_INFO("Previous TCP connection still open, try again in %dms", waitTime); return waitTime; } #endif LOG_INFO("Force close previous TCP connection"); delete openAPI; } openAPI = new T(client); } #if RAK_4631 waitTime = 100; #endif return 100; // only check occasionally for incoming connections }