| 
						
						
						
					 | 
					 | 
					@ -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,13 +13,18 @@ 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() | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -32,7 +39,6 @@ int main() | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    //    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();
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -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"); | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            if (bind.src->hasConnection(0, 50000)) { | 
					 | 
					 | 
					 | 
					        for(int n = 0; n < waiters; n++) | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                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); | 
					 | 
					 | 
					 | 
					            // for(auto& bind : sockets)
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                        res.append_data(data); | 
					 | 
					 | 
					 | 
					            // {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                    } while (data.size() >= 1024); | 
					 | 
					 | 
					 | 
					            //     if (bind.src->get_raw_socket() == events[n].data.fd) {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                    socket->emit(); | 
					 | 
					 | 
					 | 
					            //         BK_INFO("New connection on port : {0}", bind.port);
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                }); | 
					 | 
					 | 
					 | 
					            //     } 
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            } | 
					 | 
					 | 
					 | 
					            // }
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        } | 
					 | 
					 | 
					 | 
					        } | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    } 
 | 
					 | 
					 | 
					 | 
					    } 
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return 0; | 
					 | 
					 | 
					 | 
					    return 0; | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
					 | 
					 | 
					
  |