anulax1225 ago%!(EXTRA string=2 months)
parent 12b952333e
commit 18edb6be5c
  1. 107
      app/src/app.cpp
  2. 24
      app/src/httpserver.cpp
  3. 2
      package.json

@ -1,9 +1,11 @@
#include <bakatools.h> #include <bakatools.h>
#include <bakanet.h> #include <bakanet.h>
#include <sys/epoll.h>
#define MIN_PORT 8000 #define MIN_PORT 8000
#define MAX_PORT 8000 #define MAX_PORT 8010
#define RANGE MAX_PORT - MIN_PORT + 1 #define MAX_EVENTS 10
#define RANGE MAX_PORT - MIN_PORT
#define TARGET "127.0.0.1" #define TARGET "127.0.0.1"
using namespace Bk; using namespace Bk;
@ -11,32 +13,36 @@ using namespace Bk::Net;
struct Binder struct Binder
{ {
Scope<Socket> src = nullptr; Socket* src = nullptr;
Scope<Socket> dest = nullptr; Socket* dest = nullptr;
int port = 0; int port = 0;
Binder(Scope<Socket> src, int port) Binder(Socket* src, int port)
:src(std::move(src)), port(port) { } :src(src), port(port) { }
Binder(const Binder& bind) = default; //Binder(const Binder& bind) = default;
~Binder()
{
if(src) delete src;
if(dest) delete dest;
}
}; };
int main() int main()
{ {
//Log::Init("Bakanet"); // Log::Init("Bakanet");
////hostent* h = gethostbyname("localhost"); // //hostent* h = gethostbyname("localhost");
////BK_INFO(std::string(h->h_addr_list[0], h->h_length)); // //BK_INFO(std::string(h->h_addr_list[0], h->h_length));
//IpAddress ip("127.0.0.1"); // IpAddress ip("127.0.0.1");
//HttpServer server(ip, 8080); // HttpServer server(ip, 8080);
//server.get("/", [](HttpRequest& req) // server.get("/", [](HttpRequest& req)
//{ // {
// HttpReponse res(HTTP_RES_200, req.version); // HttpReponse res(HTTP_RES_200, req.version);
// res.body = "<h1>Bakanet</h1>"; // res.body = "<h1>Bakanet</h1>";
// res.body += "<p>Working http server</p>"; // res.body += "<p>Working http server</p>";
// res.body += "\n<p>URL /</p>";
// return res; // return res;
//}); // });
//server.start(); // server.start();
//return 0; // return 0;
Log::Init("Bakanet"); Log::Init("Bakanet");
@ -44,47 +50,46 @@ int main()
ThreadPool pool(5); ThreadPool pool(5);
std::vector<Binder> sockets; std::vector<Binder> sockets;
sockets.reserve(RANGE); sockets.resize(RANGE + 1);
struct epoll_event events[MAX_EVENTS];
int mtpxInstance = epoll_create(1);
BK_ASSERT(mtpxInstance != -1);
for (int i = 0; i < RANGE; i++) 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; int port = MIN_PORT + i;
Binder bind = { BK_INFO("Buiding binder on port {0}", port);
CreateScope<Socket>(Socket::create(src, port, IpProtocol::TCP)), sockets.push_back({
Socket::create(src, port, IpProtocol::TCP),
port port
}; });
if (!(bind.src->init() && bind.src->start(10))) throw new std::exception(Tools::string_format("Couldn't bind on port %d", port)); BK_INFO("Trying to bind");
sockets.push_back(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) { 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)) { // for(auto& bind : sockets)
Socket* socket = bind.src->ack(); // {
int port = bind.port; // if (bind.src->get_raw_socket() == events[n].data.fd) {
pool.queue([socket, port] { // BK_INFO("New connection on port : {0}", bind.port);
IpAddress dest(target); // }
Type::DataStream req; // }
std::vector<char> 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();
});
}
} }
} }
return 0; return 0;

@ -1,24 +0,0 @@
//#include <bakatools.h>
//#include <bakanet.h>
//
//#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 = "<h1>Bakanet</h1>";
// res.body += "<p>Working http server</p>";
// res.body += "\n<p>URL /</p>";
// return res;
// });
// server.start();
// return 0;
//}

@ -4,7 +4,7 @@
"git": "", "git": "",
"packages": [ "packages": [
{ {
"author": "anulax1225", "author": "anulax",
"name": "bakanet" "name": "bakanet"
} }
] ]

Loading…
Cancel
Save