diff --git a/Flakkari/Network/IOMultiplexer.cpp b/Flakkari/Network/IOMultiplexer.cpp index db7ad521..3eccea0a 100644 --- a/Flakkari/Network/IOMultiplexer.cpp +++ b/Flakkari/Network/IOMultiplexer.cpp @@ -16,10 +16,7 @@ namespace Flakkari::Network { PSELECT::PSELECT(FileDescriptor fileDescriptor, long int seconds, long int microseconds) { if (fileDescriptor == -1) - { - FLAKKARI_LOG_ERROR("Socket is -1"); throw std::runtime_error("Socket is -1"); - } FD_ZERO(&_fds); _maxFd = fileDescriptor; @@ -43,6 +40,7 @@ void PSELECT::addSocket(FileDescriptor socket) throw std::runtime_error("Socket is -1"); if (socket > FD_SETSIZE) throw std::runtime_error("Index out of range"); + FD_SET(socket, &_fds); if (socket > _maxFd) _maxFd = socket; @@ -55,6 +53,7 @@ void PSELECT::removeSocket(FileDescriptor socket) throw std::runtime_error("Socket is -1"); if (socket > _maxFd) throw std::runtime_error("Index out of range"); + FD_CLR(socket, &_fds); _sockets.erase(std::remove(_sockets.begin(), _sockets.end(), socket), _sockets.end()); _maxFd = *std::max_element(_sockets.begin(), _sockets.end()); @@ -78,6 +77,7 @@ bool PSELECT::isReady(FileDescriptor socket) throw std::runtime_error("Socket is -1"); if (socket > _maxFd) throw std::runtime_error("Index out of range"); + return FD_ISSET(socket, &_fds); } @@ -90,13 +90,11 @@ bool PSELECT::skipableError() { return errno == EINTR || errno == EAGAIN || errn PPOLL::PPOLL(FileDescriptor fileDescriptor, event_t events, long int seconds, long int microseconds) { if (fileDescriptor == -1) - { - FLAKKARI_LOG_ERROR("Socket is -1"); throw std::runtime_error("Socket is -1"); - } if (_pollfds.size() < std::size_t(fileDescriptor)) _pollfds.resize(fileDescriptor + 1); + _pollfds[fileDescriptor] = pollfd{fileDescriptor, events, 0}; _timeout.tv_sec = seconds; @@ -115,6 +113,7 @@ void PPOLL::addSocket(FileDescriptor socket) throw std::runtime_error("Socket is -1"); if (_pollfds.size() < std::size_t(socket)) _pollfds.resize(socket + 1); + _pollfds[socket] = pollfd{socket, POLLIN | POLLPRI, 0}; } @@ -124,6 +123,7 @@ void PPOLL::addSocket(FileDescriptor socket, event_t events) throw std::runtime_error("Socket is -1"); if (_pollfds.size() < std::size_t(socket)) _pollfds.resize(socket + 1); + _pollfds[socket] = pollfd{socket, events, 0}; } @@ -152,6 +152,7 @@ pollfd &PPOLL::operator[](std::size_t index) { if (_pollfds.size() < index) throw std::runtime_error("Index out of range"); + return _pollfds[index]; } @@ -161,6 +162,7 @@ pollfd &PPOLL::operator[](FileDescriptor socket) throw std::runtime_error("Socket is -1"); if (_pollfds.size() < std::size_t(socket)) throw std::runtime_error("Index out of range"); + return _pollfds[socket]; } @@ -168,6 +170,7 @@ bool PPOLL::isReady(pollfd fd) { if (fd.fd == -1) throw std::runtime_error("Socket is -1"); + return fd.revents & (POLLIN | POLLPRI); } @@ -179,6 +182,7 @@ bool PPOLL::isReady(FileDescriptor socket) throw std::runtime_error("Index out of range"); if (_pollfds[socket].revents & (POLLIN | POLLPRI)) return true; + return false; } @@ -191,7 +195,7 @@ bool PPOLL::skipableError() { return errno == EINTR || errno == EAGAIN || errno WSA::WSA(FileDescriptor socket, int seconds, int microseconds) { if (socket == -1) - FLAKKARI_LOG_FATAL("Socket is -1"); + throw std::runtime_error("Socket is -1"); _sockets.reserve(MAX_POLLFD); _fdArray.reserve(MAX_POLLFD); diff --git a/Flakkari/Network/Network.cpp b/Flakkari/Network/Network.cpp index 509186a6..1f153924 100644 --- a/Flakkari/Network/Network.cpp +++ b/Flakkari/Network/Network.cpp @@ -17,7 +17,7 @@ void init() WSADATA WSAData; if (::WSAStartup(MAKEWORD(2, 2), &WSAData) != NO_ERROR) - FLAKKARI_LOG_FATAL("WSAStartup failed"); + throw std::runtime_error("WSAStartup failed"); #endif } diff --git a/Flakkari/Network/Socket.cpp b/Flakkari/Network/Socket.cpp index 71376445..bdd58384 100644 --- a/Flakkari/Network/Socket.cpp +++ b/Flakkari/Network/Socket.cpp @@ -26,20 +26,14 @@ void Socket::create(const std::shared_ptr
&address) _socket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (_socket == INVALID_SOCKET) - { - FLAKKARI_LOG_FATAL("Failed to create socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to create socket, error: " + STD_ERROR); #if __APPLE__ int optval = 1; ::setsockopt(_socket, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)); #elif __linux__ if (::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, "\001", 4)) - { - FLAKKARI_LOG_FATAL("Failed to set socket to reuse address and port, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to set socket to reuse address and port, error: " + STD_ERROR); #endif } @@ -51,19 +45,13 @@ void Socket::create(socket_t socket, const std::shared_ptr &address) #if _WIN32 u_long mode = 1; if (::ioctlsocket(_socket, FIONBIO, &mode) != NO_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to set socket to non-blocking, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to set socket to non-blocking, error: " + STD_ERROR); #elif __APPLE__ int optval = 1; ::setsockopt(_socket, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)); #elif __linux__ if (::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, "\001", 4)) - { - FLAKKARI_LOG_FATAL("Failed to set socket to reuse address and port, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to set socket to reuse address and port, error: " + STD_ERROR); #endif } @@ -82,20 +70,14 @@ void Socket::create(const Address &address) _socket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (_socket == INVALID_SOCKET) - { - FLAKKARI_LOG_FATAL("Failed to create socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to create socket, error: " + STD_ERROR); #if __APPLE__ int optval = 1; ::setsockopt(_socket, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)); #elif __linux__ if (::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, "\001", 4)) - { - FLAKKARI_LOG_FATAL("Failed to set socket to reuse address and port, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to set socket to reuse address and port, error: " + STD_ERROR); #endif } @@ -114,20 +96,14 @@ void Socket::create(ip_t address, port_t port, Address::IpType ip_type, Address: _socket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if (_socket == INVALID_SOCKET) - { - FLAKKARI_LOG_FATAL("Failed to create socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to create socket, error: " + STD_ERROR); #if __APPLE__ int optval = 1; ::setsockopt(_socket, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)); #elif __linux__ if (::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, "\001", 4)) - { - FLAKKARI_LOG_FATAL("Failed to set socket to reuse address and port, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to set socket to reuse address and port, error: " + STD_ERROR); #endif } @@ -141,19 +117,13 @@ void Socket::bind() return FLAKKARI_LOG_ERROR("Address is nullptr"), void(); if (::bind(_socket, addr->ai_addr, (int) addr->ai_addrlen) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to bind socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to bind socket, error: " + STD_ERROR); } void Socket::listen(int backlog) { if (::listen(_socket, backlog) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to listen on socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to listen on socket, error: " + STD_ERROR); } void Socket::connect() @@ -164,40 +134,25 @@ void Socket::connect() return FLAKKARI_LOG_ERROR("Address is nullptr"), void(); if (::connect(_socket, addr->ai_addr, (int) addr->ai_addrlen) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to connect socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to connect socket, error: " + STD_ERROR); } void Socket::disconnect() { #if _WIN32 if (::shutdown(_socket, SD_BOTH) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to disconnect socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to disconnect socket, error: " + STD_ERROR); #else if (::shutdown(_socket, SHUT_RDWR) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to disconnect socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to disconnect socket, error: " + STD_ERROR); #endif #ifdef _WIN32 if (::closesocket(_socket) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to close socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to close socket, error: " + STD_ERROR); #else if (::close(_socket) == SOCKET_ERROR) - { - FLAKKARI_LOG_FATAL("Failed to close socket, error: " + STD_ERROR); - return; - } + throw std::runtime_error("Failed to close socket, error: " + STD_ERROR); #endif #ifdef _WIN32 @@ -214,10 +169,7 @@ std::shared_ptr