#include "../include/sibs/BootstrapNode.hpp" #include "../include/Log.hpp" #ifndef WIN32 #include #include #else #include #include #endif #include namespace sibs { BootstrapNode::BootstrapNode(const Ipv4 &address) : socket(connections.createSocket(address, false, true)) { if(UDT::listen(socket, 10) == UDT::ERROR) { std::string errMsg = "UDT: Failed to listen, error: "; errMsg += UDT::getlasterror_desc(); throw BootstrapException(errMsg); } acceptConnectionsThread = std::thread(&BootstrapNode::acceptConnections, this); } BootstrapNode::~BootstrapNode() { connections.alive = false; UDT::close(socket); acceptConnectionsThread.join(); } void BootstrapNode::acceptConnections() { sockaddr_storage clientAddr; int addrLen = sizeof(clientAddr); while(connections.alive) { UDTSOCKET clientSocket = UDT::accept(socket, (sockaddr*)&clientAddr, &addrLen); if(clientSocket == UDT::INVALID_SOCK) { // Connection was killed because bootstrap node was taken down if(!connections.alive) return; Log::error("UDT: Failed to accept connection, error: %s", UDT::getlasterror_desc()); std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } char clientHost[NI_MAXHOST]; char clientService[NI_MAXSERV]; getnameinfo((sockaddr *)&clientAddr, addrLen, clientHost, sizeof(clientHost), clientService, sizeof(clientService), NI_NUMERICHOST | NI_NUMERICSERV); Log::debug("UDT: New connection: %s:%s", clientHost, clientService); UDT::epoll_add_usock(connections.eid, clientSocket); std::shared_ptr peer = std::make_shared(); peer->socket = clientSocket; peer->receiveDataCallbackFunc = nullptr; connections.peersMutex.lock(); connections.peers[clientSocket] = peer; connections.peersMutex.unlock(); } } }