These are chat archives for SmingHub/Sming

26th
Oct 2017
√Črico GR
@ericogr
Oct 26 2017 16:34

Hi, I'm in trouble with interrupts and HardwareSerial. Randomly, ESP restarts (WDT) if I'm using Serial.setCallback()
It works for some time, but restarts after some minutes... I'm using IRAM_ATTR in callback method and no prints.

Does Anybody have a clue?

slaff
@slaff
Oct 26 2017 18:55
@ericogr Best will be to decode the stack trace and see what is wrong.
bbourdel
@bbourdel
Oct 26 2017 18:56
@slaff Any idea of how we can close an HTTPServer ? I try to delete the pointer, but it not seems to be the best way (causing Fatal exception when some old TCP connection try to close themselves) .
slaff
@slaff
Oct 26 2017 19:01
@bbourdel Hmm. we can think of a fail-safe shutdown sequence... If you want create an issue with "[feature request] ....."
bbourdel
@bbourdel
Oct 26 2017 19:03
Ok, I will. I just want to be sure that this shutdown sequence doesn't exist before spending time on it. ;-)
bbourdel
@bbourdel
Oct 26 2017 19:09
Done ;-)
bbourdel
@bbourdel
Oct 26 2017 19:31
diff --git a/Sming/SmingCore/Network/HttpServer.cpp b/Sming/SmingCore/Network/HttpServer.cpp
--- a/Sming/SmingCore/Network/HttpServer.cpp
+++ b/Sming/SmingCore/Network/HttpServer.cpp
@@ -17,12 +17,14 @@

 HttpServer::HttpServer()
 {
+    active = true;
     settings.keepAliveSeconds = 0;
     configure(settings);
 }

 HttpServer::HttpServer(HttpServerSettings settings)
 {
+    active = true;
     configure(settings);
 }

@@ -44,6 +46,7 @@ void HttpServer::configure(HttpServerSettings settings) {

 HttpServer::~HttpServer()
 {
+    active = true;
     for(int i=0; i< resourceTree.count(); i++) {
         if(resourceTree.valueAt(i) != NULL) {
             delete resourceTree.valueAt(i);
@@ -99,7 +102,23 @@ void HttpServer::setDefaultResource(HttpResource* resource) {
     addPath("*", resource);
 }

+void HttpServer::close() {
+    active = false;
+    if(totalConnections==0){
+        debugf("Closing server");
+        delete this;
+    }
+    else{
+        debugf("Wait end connection before closing server");
+    }
+}
+
+
 void HttpServer::onConnectionClose(TcpClient& connection, bool success) {
     totalConnections--;
+    if(totalConnections==0 && !active){
+        debugf("Closing server");
+        delete this;
+    }
     debugf("Closing connection. Total connections: %d", totalConnections);
 }

diff --git a/Sming/SmingCore/Network/HttpServer.h b/Sming/SmingCore/Network/HttpServer.h
--- a/Sming/SmingCore/Network/HttpServer.h
+++ b/Sming/SmingCore/Network/HttpServer.h
@@ -77,6 +77,8 @@ public:
     void setDefaultHandler(const HttpPathDelegate& callback);
     void setDefaultResource(HttpResource* resource);

+    void close();
+

 protected:
     virtual TcpConnection* createClient(tcp_pcb *clientTcp);
@@ -91,6 +93,7 @@ private:
     HttpServerSettings settings;
     ResourceTree resourceTree;
     BodyParsers bodyParsers;
+    bool active = true;
 };

 /** @} */
It seems to work well ;-)
slaff
@slaff
Oct 26 2017 21:27
@bbourdel Make a PR out of the code above. I can test it later and discuss it with you.