Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Stop a HTTPServer #1272

Closed
bbourdel opened this issue Oct 26, 2017 · 2 comments
Closed

[feature request] Stop a HTTPServer #1272

bbourdel opened this issue Oct 26, 2017 · 2 comments
Milestone

Comments

@bbourdel
Copy link

bbourdel commented Oct 26, 2017

There is any fail-safe shutdown sequence to HttpServer class.

I'd tried something like this :

HttpServer* server = new HttpServer();
server->listen(80);
server->addPath("/", onServer);

//Bla bla bla

delete server;

It's work but only if delete server; is called without any TCPConnection opened. If not, a fatal exception will be raised when the remaining TCPConnection will be closed.

@bbourdel
Copy link
Author

Here my modifications for HTTPServer, it's nearly good.

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;
 };

 /** @} */

@slaff slaff added this to the 3.5.0 milestone Nov 7, 2017
@slaff
Copy link
Contributor

slaff commented Nov 11, 2017

Implemented in PR #1284. @bbourdel Thanks for the help and initial code.

@slaff slaff closed this as completed Nov 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants