|
|
|
@ -1,9 +1,11 @@ |
|
|
|
|
#include <bakatools.h> |
|
|
|
|
#include <bakanet.h> |
|
|
|
|
#include <sys/epoll.h> |
|
|
|
|
|
|
|
|
|
#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<Socket> src = nullptr; |
|
|
|
|
Scope<Socket> dest = nullptr; |
|
|
|
|
Socket* src = nullptr; |
|
|
|
|
Socket* dest = nullptr; |
|
|
|
|
int port = 0; |
|
|
|
|
|
|
|
|
|
Binder(Scope<Socket> 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 = "<h1>Bakanet</h1>";
|
|
|
|
|
// res.body += "<p>Working http server</p>";
|
|
|
|
|
// res.body += "\n<p>URL /</p>";
|
|
|
|
|
// return res;
|
|
|
|
|
//});
|
|
|
|
|
//server.start();
|
|
|
|
|
//return 0;
|
|
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
// server.start();
|
|
|
|
|
// return 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log::Init("Bakanet"); |
|
|
|
|
IpAddress src; |
|
|
|
|
ThreadPool pool(5); |
|
|
|
|
|
|
|
|
|
std::vector<Binder> 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>(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<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(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
// 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; |
|
|
|
|