| 10 | #include <shared_mutex> |
| 11 | |
| 12 | bool RegistryServer::init() { |
| 13 | _epollFd = epoll_create1(0); |
| 14 | if (_epollFd == -1) { |
| 15 | LOG_ERROR(_env, "Failed to create epoll instance: %s", |
| 16 | strerror(errno)); |
| 17 | return false; |
| 18 | } |
| 19 | for (int i = 0; i < _options.addrs.size(); ++i) { |
| 20 | auto& addr = _options.addrs[i]; |
| 21 | if (addr.ip.data[0] == 0) { |
| 22 | continue; |
| 23 | } |
| 24 | int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); |
| 25 | if (fd == -1) { |
| 26 | LOG_ERROR(_env, "Failed to create socket: %s", strerror(errno)); |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | int opt = 1; |
| 31 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 32 | |
| 33 | sockaddr_in sockAddr{}; |
| 34 | addr.toSockAddrIn(sockAddr); |
| 35 | |
| 36 | if (bind(fd, (sockaddr *)&sockAddr, sizeof(sockAddr)) == -1) { |
| 37 | LOG_ERROR(_env, "Failed to bind socket: %s", strerror(errno)); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | if (listen(fd, SOMAXCONN) == -1) { |
| 42 | LOG_ERROR(_env, "Failed to listen on socket: %s", strerror(errno)); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | epoll_event event{}; |
| 47 | event.events = EPOLLIN; |
| 48 | event.data.fd = fd; |
| 49 | if (epoll_ctl(_epollFd, EPOLL_CTL_ADD, fd, &event) == -1){ |
| 50 | LOG_ERROR(_env, "Failed to register for epoll fd %s", fd); |
| 51 | return false; |
| 52 | } |
| 53 | _sockFds[i] = fd; |
| 54 | } |
| 55 | if (_socks[0].registerEpoll(_epollFd) == -1) { |
| 56 | LOG_ERROR(_env, "Failed to register udp socks for epoll"); |
| 57 | return false; |
| 58 | } |
| 59 | LOG_INFO(_env, "initialized sockets"); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool RegistryServer::receiveMessages(Duration timeout){ |
| 64 | ALWAYS_ASSERT(_receivedRequests.empty()); |
no test coverage detected