|
|
@ -1,9 +1,10 @@ |
|
|
|
#include <bakatools.h> |
|
|
|
#include <bakatools.h> |
|
|
|
#include <bakanet.h> |
|
|
|
#include <bakanet.h> |
|
|
|
|
|
|
|
#include <cstddef> |
|
|
|
#include <sys/epoll.h> |
|
|
|
#include <sys/epoll.h> |
|
|
|
|
|
|
|
|
|
|
|
#define MIN_PORT 8000 |
|
|
|
#define MIN_PORT 10000 |
|
|
|
#define MAX_PORT 8010 |
|
|
|
#define MAX_PORT 10005 |
|
|
|
#define MAX_EVENTS 10 |
|
|
|
#define MAX_EVENTS 10 |
|
|
|
#define RANGE MAX_PORT - MIN_PORT |
|
|
|
#define RANGE MAX_PORT - MIN_PORT |
|
|
|
#define TARGET "127.0.0.1" |
|
|
|
#define TARGET "127.0.0.1" |
|
|
@ -13,18 +14,11 @@ using namespace Bk::Net; |
|
|
|
|
|
|
|
|
|
|
|
struct Binder
|
|
|
|
struct Binder
|
|
|
|
{ |
|
|
|
{ |
|
|
|
Socket* src = nullptr; |
|
|
|
Scope<Socket> src;
|
|
|
|
Socket* dest = nullptr; |
|
|
|
|
|
|
|
int port = 0; |
|
|
|
int port = 0; |
|
|
|
|
|
|
|
|
|
|
|
Binder(Socket* src, int port) |
|
|
|
Binder(Socket* src, int port) |
|
|
|
:src(src), port(port) { } |
|
|
|
:src(src), port(port) { } |
|
|
|
//Binder(const Binder& bind) = default;
|
|
|
|
|
|
|
|
~Binder() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if(src) delete src; |
|
|
|
|
|
|
|
if(dest) delete dest; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
int main() |
|
|
|
int main() |
|
|
@ -50,33 +44,30 @@ int main() |
|
|
|
ThreadPool pool(5); |
|
|
|
ThreadPool pool(5); |
|
|
|
|
|
|
|
|
|
|
|
std::vector<Binder> sockets; |
|
|
|
std::vector<Binder> sockets; |
|
|
|
sockets.resize(RANGE + 1); |
|
|
|
sockets.reserve(RANGE + 1); |
|
|
|
|
|
|
|
|
|
|
|
struct epoll_event events[MAX_EVENTS]; |
|
|
|
struct epoll_event events[MAX_EVENTS]; |
|
|
|
int mtpxInstance = epoll_create(1); |
|
|
|
int mtpxInstance = epoll_create(1); |
|
|
|
BK_ASSERT(mtpxInstance != -1); |
|
|
|
BK_ASSERT(mtpxInstance != -1); |
|
|
|
|
|
|
|
|
|
|
|
BK_INFO("Starting binds min {0} max {1} RANGE {2}", MIN_PORT, MAX_PORT, RANGE); |
|
|
|
BK_INFO("Starting binds min {0} max {1} RANGE {2}", MIN_PORT, MAX_PORT, RANGE); |
|
|
|
for (int i = 0; i <= RANGE; i++) |
|
|
|
for (size_t i = 0; i <= RANGE; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int port = MIN_PORT + i; |
|
|
|
int port = MIN_PORT + i; |
|
|
|
BK_INFO("Buiding binder on port {0}", port); |
|
|
|
BK_INFO("Buiding binder on port {0}", port); |
|
|
|
sockets.push_back({ |
|
|
|
sockets.insert( |
|
|
|
Socket::create(src, port, IpProtocol::TCP), |
|
|
|
sockets.begin() + i,
|
|
|
|
port |
|
|
|
Binder(Socket::create(src, port, IpProtocol::TCP),port) |
|
|
|
}); |
|
|
|
); |
|
|
|
BK_INFO("Trying to bind"); |
|
|
|
BK_ASSERT(sockets[i].src->init()); |
|
|
|
BK_MSG_ASSERT(sockets[i].src->init() && sockets[i].src->start(10), Tools::string_format("Couldn't bind on port %d", port)); |
|
|
|
BK_ASSERT(sockets[i].src->start(10));; |
|
|
|
BK_INFO("Init event listener"); |
|
|
|
|
|
|
|
struct epoll_event ev; |
|
|
|
struct epoll_event ev; |
|
|
|
ev.events = EPOLLIN; |
|
|
|
ev.events = EPOLLIN; |
|
|
|
ev.data.fd = sockets[i].src->get_raw_socket(); |
|
|
|
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) { |
|
|
|
if (epoll_ctl(mtpxInstance, EPOLL_CTL_ADD, sockets[i].src->get_raw_socket(), &ev) == -1) { |
|
|
|
BK_MSG_ASSERT(false, "epoll_ctl: listen_sock"); |
|
|
|
BK_MSG_ASSERT(false, "epoll_ctl: listen_sock"); |
|
|
|
exit(EXIT_FAILURE); |
|
|
|
exit(EXIT_FAILURE); |
|
|
|
} |
|
|
|
} |
|
|
|
BK_INFO("Added to sockets"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
while (true) { |
|
|
@ -84,12 +75,15 @@ int main() |
|
|
|
BK_MSG_ASSERT(waiters != -1, "epoll_ctl: listen_sock"); |
|
|
|
BK_MSG_ASSERT(waiters != -1, "epoll_ctl: listen_sock"); |
|
|
|
for(int n = 0; n < waiters; n++) |
|
|
|
for(int n = 0; n < waiters; n++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// for(auto& bind : sockets)
|
|
|
|
for(auto& bind : sockets) |
|
|
|
// {
|
|
|
|
{ |
|
|
|
// if (bind.src->get_raw_socket() == events[n].data.fd) {
|
|
|
|
if (bind.src->get_raw_socket() == events[n].data.fd) { |
|
|
|
// BK_INFO("New connection on port : {0}", bind.port);
|
|
|
|
BK_INFO("New connection on port : {0}", bind.port); |
|
|
|
// }
|
|
|
|
Socket* dest = bind.src->ack(); |
|
|
|
// }
|
|
|
|
BK_INFO("IP Address : ", dest->get_ip()); |
|
|
|
|
|
|
|
delete dest; |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|