Skip to content

Commit

Permalink
Add method to stop HttpSever
Browse files Browse the repository at this point in the history
Have to be improved to free all RAM (heap) taken when HTTP server is created.
  • Loading branch information
bbourdel authored and slav-at-attachix committed Nov 8, 2017
1 parent 89ac9a7 commit 3247705
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 20 additions & 1 deletion Sming/SmingCore/Network/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

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

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

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
5 changes: 4 additions & 1 deletion Sming/SmingCore/Network/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class HttpServer: public TcpServer
void setDefaultHandler(const HttpPathDelegate& callback);
void setDefaultResource(HttpResource* resource);

void close();


protected:
virtual TcpConnection* createClient(tcp_pcb *clientTcp);
Expand All @@ -91,7 +93,8 @@ class HttpServer: public TcpServer
HttpServerSettings settings;
ResourceTree resourceTree;
BodyParsers bodyParsers;
bool active = true;
};

/** @} */
#endif /* _SMING_CORE_HTTPSERVER_H_ */
#endif /* _SMING_CORE_HTTPSERVER_H_ */

0 comments on commit 3247705

Please sign in to comment.