partial fix for #608 - when a new TCP API connection arrives, close old one completely

1.2-legacy
Kevin Hester 2020-12-31 09:52:08 +08:00
rodzic de37a0c31e
commit cdf416cb73
4 zmienionych plików z 39 dodań i 15 usunięć

Wyświetl plik

@ -28,18 +28,19 @@ void WiFiServerAPI::onConnectionChanged(bool connected)
} }
} }
void WiFiServerAPI::loop() /// override close to also shutdown the TCP link
void WiFiServerAPI::close() {
client.stop(); // drop tcp connection
StreamAPI::close();
}
bool WiFiServerAPI::loop()
{ {
if (client.connected()) { if (client.connected()) {
StreamAPI::loop(); StreamAPI::loop();
} else if(isConnected) { return true;
// If our API link was up, shut it down } else {
return false;
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 this;
close();
} }
} }
@ -58,15 +59,25 @@ int32_t WiFiServerPort::runOnce()
auto client = available(); auto client = available();
if (client) { if (client) {
// Close any previous connection (see FIXME in header file) // Close any previous connection (see FIXME in header file)
if (openAPI) if (openAPI) {
DEBUG_MSG("Force closing previous TCP connection\n");
delete openAPI; delete openAPI;
}
openAPI = new WiFiServerAPI(client); openAPI = new WiFiServerAPI(client);
} }
if (openAPI) { if (openAPI) {
// Allow idle processing so the API can read from its incoming stream // Allow idle processing so the API can read from its incoming stream
openAPI->loop(); 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 return 0; // run fast while our API server is running
} else } else
return 100; // only check occasionally for incoming connections return 100; // only check occasionally for incoming connections

Wyświetl plik

@ -18,7 +18,11 @@ class WiFiServerAPI : public StreamAPI
virtual ~WiFiServerAPI(); virtual ~WiFiServerAPI();
virtual void loop(); // Check for dropped client connections /// @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: protected:
/// Hookable to find out when connection changes /// Hookable to find out when connection changes

Wyświetl plik

@ -21,11 +21,17 @@ void PhoneAPI::init()
observe(&service.fromNumChanged); observe(&service.fromNumChanged);
} }
PhoneAPI::~PhoneAPI() {
close();
}
void PhoneAPI::close() { void PhoneAPI::close() {
unobserve(); unobserve();
state = STATE_SEND_NOTHING; state = STATE_SEND_NOTHING;
bool oldConnected = isConnected;
isConnected = false; isConnected = false;
onConnectionChanged(isConnected); if(oldConnected != isConnected)
onConnectionChanged(isConnected);
} }
void PhoneAPI::checkConnectionTimeout() void PhoneAPI::checkConnectionTimeout()

Wyświetl plik

@ -55,12 +55,15 @@ class PhoneAPI
public: public:
PhoneAPI(); PhoneAPI();
/// Destructor - calls close()
virtual ~PhoneAPI();
/// Do late init that can't happen at constructor time /// Do late init that can't happen at constructor time
virtual void init(); virtual void init();
// Call this when the client drops the connection, resets the state to STATE_SEND_NOTHING // Call this when the client drops the connection, resets the state to STATE_SEND_NOTHING
// Unregisters our observer // Unregisters our observer. A closed connection **can** be reopened by calling init again.
void close(); virtual void close();
/** /**
* Handle a ToRadio protobuf * Handle a ToRadio protobuf