Skip to content

Commit

Permalink
Stop accepting new connection when the server is shutting down.
Browse files Browse the repository at this point in the history
Force-close all connections.
  • Loading branch information
slav-at-attachix committed Nov 8, 2017
1 parent 3247705 commit 2b78200
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
55 changes: 33 additions & 22 deletions Sming/SmingCore/Network/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
#include "TcpClient.h"
#include "../Wiring/WString.h"

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

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

void HttpServer::configure(HttpServerSettings settings) {
void HttpServer::configure(HttpServerSettings settings)
{
this->settings = settings;
if(settings.minHeapSize != -1 && settings.minHeapSize > -1) {
minHeapSize = settings.minHeapSize;
Expand Down Expand Up @@ -61,12 +60,18 @@ void HttpServer::setBodyParser(const String& contentType, HttpBodyParserDelegate

TcpConnection* HttpServer::createClient(tcp_pcb *clientTcp)
{
if(!active) {
debugf("Refusing new connections. The server is shutting down");
return NULL;
}

HttpServerConnection* con = new HttpServerConnection(clientTcp);
con->setResourceTree(&resourceTree);
con->setBodyParsers(&bodyParsers);
con->setCompleteDelegate(TcpClientCompleteDelegate(&HttpServer::onConnectionClose, this));

totalConnections++;
connections.add(con);
totalConnections = connections.count();
debugf("Opening connection. Total connections: %d", totalConnections);

return con;
Expand All @@ -88,37 +93,43 @@ void HttpServer::setDefaultHandler(const HttpPathDelegate& callback)
addPath("*", callback);
}

void HttpServer::addPath(const String& path, const HttpResourceDelegate& onRequestComplete) {
void HttpServer::addPath(const String& path, const HttpResourceDelegate& onRequestComplete)
{
HttpResource* resource = new HttpResource;
resource->onRequestComplete = onRequestComplete;
resourceTree[path] = resource;
}

void HttpServer::addPath(const String& path, HttpResource* resource) {
void HttpServer::addPath(const String& path, HttpResource* resource)
{
resourceTree[path] = resource;
}

void HttpServer::setDefaultResource(HttpResource* resource) {
void HttpServer::setDefaultResource(HttpResource* resource)
{
addPath("*", resource);
}

void HttpServer::close() {
void HttpServer::shutdown()
{
active = false;
if(totalConnections==0){
debugf("Closing server");
delete this;
}
else{
debugf("Wait end connection before closing server");
for(int i; i < connections.count(); i++) {
HttpServerConnection* connection = connections[i];
if(connection == NULL) {
continue;
}

connection->close();
}
}


void HttpServer::onConnectionClose(TcpClient& connection, bool success) {
totalConnections--;
if(totalConnections==0 && !active){
debugf("Closing server");
void HttpServer::onConnectionClose(TcpClient& connection, bool success)
{
connections.removeElement((HttpServerConnection *)connection);
totalConnections = connections.count();
if(totalConnections == 0 && !active){
debugf("Shutting down the Http Server");
delete this;
}
debugf("Closing connection. Total connections: %d", totalConnections);
}
}
7 changes: 4 additions & 3 deletions Sming/SmingCore/Network/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class HttpServer: public TcpServer
void setDefaultHandler(const HttpPathDelegate& callback);
void setDefaultResource(HttpResource* resource);

void close();

void shutdown();

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

Vector<HttpServerConnection*> connections;
};

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

0 comments on commit 2b78200

Please sign in to comment.