From bce444ad25fb8e6511d768b7829d0b8469f83b37 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 09:23:38 -0700 Subject: [PATCH 1/8] update --- external/libs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/libs b/external/libs index fe4aec9..df82f85 160000 --- a/external/libs +++ b/external/libs @@ -1 +1 @@ -Subproject commit fe4aec9d8aa1e7d611b78ee9d8e7097f36dd5fe8 +Subproject commit df82f85db8d45efe89a199041325257728ee4aeb From 7a3c26e8360663fe9dc7c2bf562f47c2580a3252 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 09:50:32 -0700 Subject: [PATCH 2/8] making changes --- external/libs | 2 +- src/main.cpp | 5 ++++- src/office.cpp | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/external/libs b/external/libs index df82f85..d2ca0d5 160000 --- a/external/libs +++ b/external/libs @@ -1 +1 @@ -Subproject commit df82f85db8d45efe89a199041325257728ee4aeb +Subproject commit d2ca0d52f2dfccf0ed1358c034438beb560af3be diff --git a/src/main.cpp b/src/main.cpp index f12ad87..22dc8ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include extern "C" { #include @@ -94,7 +95,9 @@ int main(int argc, char * argv[]) { signal(SIGHUP, __HandleSignal); // For terminal hangup _running = error == 0; - while (!error && _running.get()) { } + while (!error && _running.get()) { + usleep(50); + } skt->stop(); BFRelease(skt); diff --git a/src/office.cpp b/src/office.cpp index d44c79e..0dd724a 100644 --- a/src/office.cpp +++ b/src/office.cpp @@ -80,6 +80,8 @@ void __IncomingRequestsWorkerThread(void * in) { BFRelease(respData); } + envelope->connection()->closeConnection(); + BFRelease(resp); BFRelease(req); BFRelease(envelope); From d495dc1fb5354d9478cfcf9f6f441b63e0fc3356 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 13:51:21 -0700 Subject: [PATCH 3/8] updating todo --- .gitignore | 1 + todo.md | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 704f410..54b4e3c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build .DS_Store http *.swp +nohup.out diff --git a/todo.md b/todo.md index 61dcad7..827bceb 100644 --- a/todo.md +++ b/todo.md @@ -1,3 +1,6 @@ +**x.x** +- [ ] VIRT memory is 20.0T, reduce if possible + **0.1** - [x] serve html - [x] receive http requests from client (be sure to assemble the packets) @@ -7,6 +10,6 @@ - [x] multithread request packet handling - [x] seg fault issue when you spam the reload page button - [x] configure port in cli -- [ ] launch in background -- [ ] fix blocking issues that are preventing software to exit +- [x] launch in background +- [x] fix blocking issues that are preventing software to exit From 02a7aad90f135b86c6d0517200868283b3ec59fd Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 13:53:39 -0700 Subject: [PATCH 4/8] adding more notes --- todo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/todo.md b/todo.md index 827bceb..70eb495 100644 --- a/todo.md +++ b/todo.md @@ -1,5 +1,5 @@ **x.x** -- [ ] VIRT memory is 20.0T, reduce if possible +- [ ] VIRT memory is 20.0T, reduce if possible (too many dynamic allocations, work must be done in libs) **0.1** - [x] serve html From d7da2e2de806aa82e2745f26886046a6c3ade4ba Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 14:05:10 -0700 Subject: [PATCH 5/8] update todo --- todo.md | 1 - 1 file changed, 1 deletion(-) diff --git a/todo.md b/todo.md index 70eb495..44bdff8 100644 --- a/todo.md +++ b/todo.md @@ -1,5 +1,4 @@ **x.x** -- [ ] VIRT memory is 20.0T, reduce if possible (too many dynamic allocations, work must be done in libs) **0.1** - [x] serve html From e33ebd67cede5e5776c46330829ac5f0854b9f20 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 15:45:28 -0700 Subject: [PATCH 6/8] update libs --- external/libs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/libs b/external/libs index d2ca0d5..2a2a151 160000 --- a/external/libs +++ b/external/libs @@ -1 +1 @@ -Subproject commit d2ca0d52f2dfccf0ed1358c034438beb560af3be +Subproject commit 2a2a151d9c15be48abff93ec205aa8b9b59a5fb6 From aecdbc67646dbccbd45b8ffcb807944a4f437130 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 16:05:16 -0700 Subject: [PATCH 7/8] updating libs --- external/libs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/libs b/external/libs index 2a2a151..d4a5a49 160000 --- a/external/libs +++ b/external/libs @@ -1 +1 @@ -Subproject commit 2a2a151d9c15be48abff93ec205aa8b9b59a5fb6 +Subproject commit d4a5a49860fed7711925741377c4245fa09e374a From fc82cb3803327cd92986d4a94e9462d8338fb9b8 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 29 Apr 2025 17:03:00 -0700 Subject: [PATCH 8/8] using semaphore and defer logic --- external/libs | 2 +- makefile | 4 ++-- src/main.cpp | 56 +++++++++++++++++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/external/libs b/external/libs index d4a5a49..68b5ad9 160000 --- a/external/libs +++ b/external/libs @@ -1 +1 @@ -Subproject commit d4a5a49860fed7711925741377c4245fa09e374a +Subproject commit 68b5ad9380ff5db08dc9a224f4f82edd9525e213 diff --git a/makefile b/makefile index 7f0ff18..6556d75 100644 --- a/makefile +++ b/makefile @@ -57,14 +57,14 @@ LINKS = -lpthread $(BF_LIB_C_FLAGS) -ldl ifeq ($(CONFIG),release) # release MAIN_FILE = src/main.cpp BIN_NAME = http -FLAGS = $(CPPFLAGS) -Isrc/ $(CPPSTD) -Iexternal/bin/libs/release +FLAGS = $(CPPFLAGS) -Isrc/ $(CPPSTD) -Iexternal/bin/libs/release ### Debug settings else ifeq ($(CONFIG),debug) # debug MAIN_FILE = src/main.cpp BIN_NAME = http-debug ADDR_SANITIZER = -fsanitize=address -FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug +FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug ### Test settings else ifeq ($(CONFIG),test) # test diff --git a/src/main.cpp b/src/main.cpp index 22dc8ec..79cb823 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,5 @@ /** - * author: brando - * date: 2/26/25 + * author: brando * date: 2/26/25 */ #include "office.hpp" @@ -27,8 +26,8 @@ using namespace std; LOG_INIT; -Atomic _running; uint16_t _port = 8080; +BFLock _appRunSema; void help(const char * toolname) { printf("usage: %s \n", toolname); @@ -65,11 +64,22 @@ int __ReadArguments(int argc, char * argv[]) { } void __HandleSignal(int signum) { - _running = false; + BFLockRelease(&_appRunSema); } int main(int argc, char * argv[]) { LOG_OPEN; + BFDefer([&](){ + LOG_CLOSE; + }); + + if (BFLockCreate(&_appRunSema)) { + LOG_ERROR("couldn't create semaphore"); + return -1; + } + BFDefer([&](){ + BFLockDestroy(&_appRunSema); + }); if (__ReadArguments(argc, argv)) { return -1; @@ -79,32 +89,38 @@ int main(int argc, char * argv[]) { Log::SetCallback(__LogCallbackBFNet); Office::start(); + BFDefer([](){ + Office::stop(); + }); const char * ipaddr = "0.0.0.0"; LOG_WRITE("creating socket at %s:%u", ipaddr, _port); Socket * skt = Socket::create(SOCKET_MODE_SERVER, ipaddr, _port, &error); - if (!error) { - skt->setInStreamCallback(Office::envelopeReceive); - skt->setNewConnectionCallback(__NewConnection); - skt->setBufferSize(1024 * 1024 * 100); - error = skt->start(); + BFDefer([&](){ + BFRelease(skt); + }); + if (error) { + LOG_ERROR("couldn't create socket, error=%d", error); + return error; + } + + skt->setInStreamCallback(Office::envelopeReceive); + skt->setNewConnectionCallback(__NewConnection); + skt->setBufferSize(1024 * 1024 * 100); + if (skt->start()) { + LOG_ERROR("couldn't start listening on socket"); + return -1; } + BFDefer([&](){ + skt->stop(); + }); signal(SIGINT, __HandleSignal); // For Ctrl+C signal(SIGTERM, __HandleSignal); // For 'kill' command signal(SIGHUP, __HandleSignal); // For terminal hangup - - _running = error == 0; - while (!error && _running.get()) { - usleep(50); - } - skt->stop(); - BFRelease(skt); - Office::stop(); + BFLockWait(&_appRunSema); - LOG_CLOSE; - - return error; + return 0; }