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

Allow shutting down of TcpServers #1284

Merged
merged 6 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions Sming/SmingCore/Network/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
#include "TcpClient.h"
#include "../Wiring/WString.h"

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

HttpServer::HttpServer(HttpServerSettings settings)
HttpServer::HttpServer(HttpServerSettings settings): 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 All @@ -44,6 +45,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 All @@ -58,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 @@ -85,21 +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::onConnectionClose(TcpClient& connection, bool success) {
totalConnections--;
void HttpServer::shutdown()
{
active = false;
for(int i=0; i < connections.count(); i++) {
HttpServerConnection* connection = connections[i];
if(connection == NULL) {
continue;
}

connection->close();
}
}

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: That will cause a race condition - HttpServer will start freeing the resources and meanwhile the HttpServerConnections will try to use those resource in their destructors... A timer with 3 - 4 seconds delay should fix this...

}
debugf("Closing connection. Total connections: %d", totalConnections);
}
4 changes: 4 additions & 0 deletions Sming/SmingCore/Network/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class HttpServer: public TcpServer
void setDefaultHandler(const HttpPathDelegate& callback);
void setDefaultResource(HttpResource* resource);

void shutdown();

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

Vector<HttpServerConnection*> connections;
};

/** @} */
Expand Down
4 changes: 3 additions & 1 deletion Sming/SmingCore/Network/TcpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ void TcpConnection::close()
axl_free(tcp);
#endif

tcp_poll(tcp, staticOnPoll, 1);
if(tcp->state == LISTEN) {
tcp_poll(tcp, staticOnPoll, 1);
}
tcp_arg(tcp, NULL); // reset pointer to close connection on next callback
tcp = NULL;

Expand Down