diff --git a/app/src/app.cpp b/app/src/app.cpp index e46f163..f3074fc 100644 --- a/app/src/app.cpp +++ b/app/src/app.cpp @@ -1,9 +1,11 @@ #include #include +#include #define MIN_PORT 8000 -#define MAX_PORT 8000 -#define RANGE MAX_PORT - MIN_PORT + 1 +#define MAX_PORT 8010 +#define MAX_EVENTS 10 +#define RANGE MAX_PORT - MIN_PORT #define TARGET "127.0.0.1" using namespace Bk; @@ -11,80 +13,83 @@ using namespace Bk::Net; struct Binder { - Scope src = nullptr; - Scope dest = nullptr; + Socket* src = nullptr; + Socket* dest = nullptr; int port = 0; - Binder(Scope src, int port) - :src(std::move(src)), port(port) { } - Binder(const Binder& bind) = default; + Binder(Socket* src, int port) + :src(src), port(port) { } + //Binder(const Binder& bind) = default; + ~Binder() + { + if(src) delete src; + if(dest) delete dest; + } }; int main() { - //Log::Init("Bakanet"); - ////hostent* h = gethostbyname("localhost"); - ////BK_INFO(std::string(h->h_addr_list[0], h->h_length)); - //IpAddress ip("127.0.0.1"); - //HttpServer server(ip, 8080); - //server.get("/", [](HttpRequest& req) - //{ + // Log::Init("Bakanet"); + // //hostent* h = gethostbyname("localhost"); + // //BK_INFO(std::string(h->h_addr_list[0], h->h_length)); + // IpAddress ip("127.0.0.1"); + // HttpServer server(ip, 8080); + // server.get("/", [](HttpRequest& req) + // { // HttpReponse res(HTTP_RES_200, req.version); // res.body = "

Bakanet

"; // res.body += "

Working http server

"; - // res.body += "\n

URL /

"; // return res; - //}); - //server.start(); - //return 0; - + // }); + // server.start(); + // return 0; + Log::Init("Bakanet"); IpAddress src; ThreadPool pool(5); std::vector sockets; - sockets.reserve(RANGE); + sockets.resize(RANGE + 1); - for (int i = 0; i < RANGE; i++) + struct epoll_event events[MAX_EVENTS]; + int mtpxInstance = epoll_create(1); + BK_ASSERT(mtpxInstance != -1); + + BK_INFO("Starting binds min {0} max {1} RANGE {2}", MIN_PORT, MAX_PORT, RANGE); + for (int i = 0; i <= RANGE; i++) { int port = MIN_PORT + i; - Binder bind = { - CreateScope(Socket::create(src, port, IpProtocol::TCP)), - + BK_INFO("Buiding binder on port {0}", port); + sockets.push_back({ + Socket::create(src, port, IpProtocol::TCP), port - }; - if (!(bind.src->init() && bind.src->start(10))) throw new std::exception(Tools::string_format("Couldn't bind on port %d", port)); - sockets.push_back(bind); + }); + BK_INFO("Trying to bind"); + BK_MSG_ASSERT(sockets[i].src->init() && sockets[i].src->start(10), Tools::string_format("Couldn't bind on port %d", port)); + BK_INFO("Init event listener"); + struct epoll_event ev; + ev.events = EPOLLIN; + ev.data.fd = sockets[i].src->get_raw_socket(); + BK_INFO("Adding to queue"); + if (epoll_ctl(mtpxInstance, EPOLL_CTL_ADD, sockets[i].src->get_raw_socket(), &ev) == -1) { + BK_MSG_ASSERT(false, "epoll_ctl: listen_sock"); + exit(EXIT_FAILURE); + } + BK_INFO("Added to sockets"); } while (true) { - for(const Binder& bind : sockets) + int waiters = epoll_wait(mtpxInstance, events, MAX_EVENTS, -1); + BK_MSG_ASSERT(waiters != -1, "epoll_ctl: listen_sock"); + for(int n = 0; n < waiters; n++) { - if (bind.src->hasConnection(0, 50000)) { - Socket* socket = bind.src->ack(); - int port = bind.port; - pool.queue([socket, port] { - IpAddress dest(target); - Type::DataStream req; - std::vector data; - do - { - data = socket->obtain(1024); - req.append_data(data); - } while (data.size() >= 1024); - auto socketDest = Socket::create(dest, port, IpProtocol::TCP); - socketDest->conn(); - socketDest->emit(req.payload); - Type::DataStream res; - do - { - data = socketDest->obtain(1024); - res.append_data(data); - } while (data.size() >= 1024); - socket->emit(); - }); - } + // for(auto& bind : sockets) + // { + // if (bind.src->get_raw_socket() == events[n].data.fd) { + // BK_INFO("New connection on port : {0}", bind.port); + // } + // } } } return 0; diff --git a/app/src/httpserver.cpp b/app/src/httpserver.cpp deleted file mode 100644 index e6644c0..0000000 --- a/app/src/httpserver.cpp +++ /dev/null @@ -1,24 +0,0 @@ -//#include -//#include -// -//#define PORT 80 -// -//using namespace Bk; -//using namespace Bk::Net; -// -//int main() -//{ -// Log::Init("Bakanet"); -// IpAddress ip; -// HttpServer server(ip, PORT); -// server.get("/", [](HttpRequest& req) -// { -// HttpReponse res(HTTP_RES_200, req.version); -// res.body = "

Bakanet

"; -// res.body += "

Working http server

"; -// res.body += "\n

URL /

"; -// return res; -// }); -// server.start(); -// return 0; -//} \ No newline at end of file diff --git a/package.json b/package.json index 88c8d46..b1d368d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "git": "", "packages": [ { - "author": "anulax1225", + "author": "anulax", "name": "bakanet" } ]