diff --git a/misc/main.cpp b/misc/main.cpp index cf3b651..fc45e94 100644 --- a/misc/main.cpp +++ b/misc/main.cpp @@ -3,6 +3,8 @@ //#include "../examples/helpers/AsyncFileReader.h" //#include "../examples/helpers/AsyncFileStreamer.h" +us_listen_socket *token; + int main(int argc, char **argv) { struct PerSocketData { @@ -14,12 +16,23 @@ int main(int argc, char **argv) { .key_file_name = "/home/alexhultman/key.pem", .cert_file_name = "/home/alexhultman/cert.pem", .passphrase = "1234" - }).get("/hello", [](auto *res, auto *req) { - res->end("Hello HTTP!"); + }).get("/exit", [](auto *res, auto *req) { + + if (!token) { + res->end("Server already closed down!"); + return; + } + + res->end("Closing down server now"); + + /* Use this route to signal stop listening */ + us_listen_socket_close(token); + token = nullptr; }).ws("/*", { /* Settings */ .compression = uWS::DEDICATED_COMPRESSOR, .maxPayloadLength = 16 * 1024 * 1024, + /* autoPingInterval */ /* Handlers */ .open = [](auto *ws, auto *req) { std::cout << "WebSocket connected" << std::endl; @@ -48,6 +61,7 @@ int main(int argc, char **argv) { std::cout << "OK per socket data: " << (perSocketData->hello == 13) << std::endl; } }).listen(9001, [](auto *token) { + ::token = token; if (token) { std::cout << "Listening on port " << 3000 << std::endl; } diff --git a/src/App.h b/src/App.h index fa54e7c..d427e89 100644 --- a/src/App.h +++ b/src/App.h @@ -55,7 +55,8 @@ public: } ~TemplatedApp() { - + /* Let's just put everything here */ + httpContext->free(); } TemplatedApp(const TemplatedApp &other) { diff --git a/src/HttpRouter.h b/src/HttpRouter.h index fc3f74a..217f7ff 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -169,6 +169,15 @@ private: } } + void freeNode(Node *node) { + for (auto *p : node->children) { + freeNode(p); + } + if (node != &tree) { + delete node; + } + } + public: HttpRouter() { /* Make sure unhandled is at index 0 */ @@ -179,6 +188,7 @@ public: ~HttpRouter() { // todo: delete all Nodes or use unique_ptr + freeNode(&tree); } /* For debugging you may want to print this */ diff --git a/src/PerMessageDeflate.h b/src/PerMessageDeflate.h index 1f6b0f4..47e10cb 100644 --- a/src/PerMessageDeflate.h +++ b/src/PerMessageDeflate.h @@ -111,6 +111,7 @@ struct DeflationStream { ~DeflationStream() { std::cout << "Destructing DeflationStream" << std::endl; + deflateEnd(&deflationStream); } };