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 readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![CI](https://github.com/BrandonMFong/http/actions/workflows/test.yml/badge.svg)](https://github.com/BrandonMFong/http/actions/workflows/test.yml)
# http
An http server using POSIX sockets
![Demo](./docs/demo.gif)
Expand Down
35 changes: 26 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
#include <bflibcpp/bflibcpp.hpp>
#include <bfnet/bfnet.hpp>
#include <iostream>
#include <signal.h>
#include <stdlib.h>

extern "C" {
#include <bflibc/bflibc.h>
}

#define ARGUMENT_ROOT "-root"
#define ARGUMENT_PORT "-port"

using namespace BF::Net;
using namespace BF;
using namespace std;

LOG_INIT;

Atomic<bool> _running;
uint16_t _port = 8080;

void help(const char * toolname) {
printf("usage: %s %s <path>\n", toolname, ARGUMENT_ROOT);
printf("\n");
Expand All @@ -43,15 +49,23 @@ int __ReadArguments(int argc, char * argv[]) {

for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], ARGUMENT_ROOT)) {
if (!Resource::setRootFolder(argv[++i])) {
if (++i < argc && !Resource::setRootFolder(argv[i])) {
LOG_ERROR("'%s' is not accepted as a root folder", argv[i]);
}
} else if (!strcmp(argv[i], ARGUMENT_PORT)) {
if (++i < argc) {
_port = atoi(argv[i]);
}
}
}

return 0;
}

void __HandleSignal(int signum) {
_running = false;
}

int main(int argc, char * argv[]) {
LOG_OPEN;

Expand All @@ -63,22 +77,25 @@ int main(int argc, char * argv[]) {
Log::SetCallback(__LogCallbackBFNet);

Office::start();
Socket * skt = Socket::create(SOCKET_MODE_SERVER, "0.0.0.0", 8080, &error);

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();
}

if (!error) {
cout << "Press any key to stop...";
cin.get();
error = skt->stop();

cout << "Stopped..." << endl;
}
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();

Expand Down