Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
.DS_Store
http
*.swp
nohup.out
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 37 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* author: brando
* date: 2/26/25
* author: brando * date: 2/26/25
*/

#include "office.hpp"
Expand All @@ -12,6 +11,7 @@
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

extern "C" {
#include <bflibc/bflibc.h>
Expand All @@ -26,8 +26,8 @@ using namespace std;

LOG_INIT;

Atomic<bool> _running;
uint16_t _port = 8080;
BFLock _appRunSema;

void help(const char * toolname) {
printf("usage: %s <args>\n", toolname);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

2 changes: 2 additions & 0 deletions src/office.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ void __IncomingRequestsWorkerThread(void * in) {
BFRelease(respData);
}

envelope->connection()->closeConnection();

BFRelease(resp);
BFRelease(req);
BFRelease(envelope);
Expand Down
6 changes: 4 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**x.x**

**0.1**
- [x] serve html
- [x] receive http requests from client (be sure to assemble the packets)
Expand All @@ -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