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/external/libs b/external/libs index fe4aec9..68b5ad9 160000 --- a/external/libs +++ b/external/libs @@ -1 +1 @@ -Subproject commit fe4aec9d8aa1e7d611b78ee9d8e7097f36dd5fe8 +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 f12ad87..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" @@ -12,6 +11,7 @@ #include #include #include +#include extern "C" { #include @@ -26,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); @@ -64,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; @@ -78,30 +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()) { } - skt->stop(); - BFRelease(skt); - Office::stop(); + BFLockWait(&_appRunSema); - LOG_CLOSE; - - return error; + return 0; } 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); diff --git a/todo.md b/todo.md index 61dcad7..44bdff8 100644 --- a/todo.md +++ b/todo.md @@ -1,3 +1,5 @@ +**x.x** + **0.1** - [x] serve html - [x] receive http requests from client (be sure to assemble the packets) @@ -7,6 +9,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