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()) {
StreamAPI::loop();
} else if(isConnected) {
// 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 this;
close();
return true;
} else {
return false;
}
}
@ -58,15 +59,25 @@ int32_t WiFiServerPort::runOnce()
auto client = available();
if (client) {
// Close any previous connection (see FIXME in header file)
if (openAPI)
if (openAPI) {
DEBUG_MSG("Force closing previous TCP connection\n");
delete openAPI;
}
openAPI = new WiFiServerAPI(client);
}
if (openAPI) {
// 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
} else
return 100; // only check occasionally for incoming connections

Wyświetl plik

@ -18,7 +18,11 @@ class WiFiServerAPI : public StreamAPI
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:
/// Hookable to find out when connection changes

Wyświetl plik

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

Wyświetl plik

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