diff --git a/Flakkari/Network/Address.cpp b/Flakkari/Network/Address.cpp index 7b6e8e17..a7a4813d 100644 --- a/Flakkari/Network/Address.cpp +++ b/Flakkari/Network/Address.cpp @@ -58,7 +58,7 @@ Address::Address(address_t &address, port_t port, SocketType socket_type, IpType Address::Address(port_t port, SocketType socket_type, IpType ip_type) : _socket_type(socket_type), _ip_type(ip_type) { - addrinfo hints; + addrinfo hints{}; hints.ai_family = (ip_type == IpType::IPv4) ? AF_INET : AF_INET6; hints.ai_socktype = (socket_type == SocketType::TCP) ? SOCK_STREAM : SOCK_DGRAM; hints.ai_protocol = (socket_type == SocketType::TCP) ? IPPROTO_TCP : IPPROTO_UDP; @@ -80,8 +80,7 @@ Address::Address(port_t port, SocketType socket_type, IpType ip_type) : _socket_ Address::Address(const sockaddr_in &clientAddr, SocketType socket_type, IpType ip_type) : _socket_type(socket_type), _ip_type(ip_type) { - addrinfo hints; - + addrinfo hints{}; hints.ai_family = (_ip_type == IpType::IPv4) ? AF_INET : AF_INET6; hints.ai_socktype = (_socket_type == SocketType::TCP) ? SOCK_STREAM : SOCK_DGRAM; hints.ai_protocol = (_socket_type == SocketType::TCP) ? IPPROTO_TCP : IPPROTO_UDP; @@ -104,15 +103,16 @@ Address::Address(const sockaddr_in &clientAddr, SocketType socket_type, IpType i Address::Address(const sockaddr_storage &clientAddr, SocketType socket_type, IpType ip_type) : _socket_type(socket_type), _ip_type(ip_type) { - addrinfo hints; + addrinfo hints{}; hints.ai_family = (_ip_type == IpType::IPv4) ? AF_INET : AF_INET6; hints.ai_socktype = (_socket_type == SocketType::TCP) ? SOCK_STREAM : SOCK_DGRAM; hints.ai_protocol = (_socket_type == SocketType::TCP) ? IPPROTO_TCP : IPPROTO_UDP; - hints.ai_flags = (AI_PASSIVE | AI_V4MAPPED | AI_ALL); + hints.ai_flags = + (_ip_type == IpType::IPv4) ? (AI_ADDRCONFIG | AI_PASSIVE | AI_V4MAPPED) : (AI_ADDRCONFIG | AI_PASSIVE | AI_ALL); addrinfo *result = nullptr; #if defined(_WIN32) - char name[INET6_ADDRSTRLEN]; + char name[INET6_ADDRSTRLEN] = {0}; if (clientAddr.ss_family == AF_INET) inet_ntop(AF_INET, &(((sockaddr_in *) &clientAddr)->sin_addr), name, INET_ADDRSTRLEN); @@ -123,9 +123,8 @@ Address::Address(const sockaddr_storage &clientAddr, SocketType socket_type, IpT #endif const std::string serviceStr = std::to_string(ntohs(((sockaddr_in *) &clientAddr)->sin_port)); - const char *service = serviceStr.c_str(); - if (getaddrinfo(name, service, &hints, &result) != 0) + if (getaddrinfo(name, serviceStr.c_str(), &hints, &result) != 0) { FLAKKARI_LOG_ERROR("getaddrinfo() failed"); return; diff --git a/Flakkari/Network/IOMultiplexer.cpp b/Flakkari/Network/IOMultiplexer.cpp index fb3206a7..db7ad521 100644 --- a/Flakkari/Network/IOMultiplexer.cpp +++ b/Flakkari/Network/IOMultiplexer.cpp @@ -201,7 +201,7 @@ WSA::WSA(FileDescriptor socket, int seconds, int microseconds) WSAPOLLFD pollFd; pollFd.fd = socket; - pollFd.events = POLLIN | POLLOUT; + pollFd.events = POLLIN; pollFd.revents = 0; _fdArray.emplace_back(pollFd); _sockets.emplace_back(socket); @@ -217,7 +217,7 @@ void WSA::addSocket(FileDescriptor socket) WSAPOLLFD pollFd; pollFd.fd = socket; - pollFd.events = POLLIN | POLLOUT; + pollFd.events = POLLIN; pollFd.revents = 0; if (_freeSpace.empty()) { @@ -259,7 +259,7 @@ bool WSA::isReady(FileDescriptor socket) throw std::runtime_error("Socket not found"); size_t index = std::distance(_sockets.begin(), it); - return _fdArray[index].revents & (POLLIN | POLLOUT); + return _fdArray[index].revents & POLLIN; } bool WSA::skipableError() diff --git a/Flakkari/Network/Socket.cpp b/Flakkari/Network/Socket.cpp index ef934956..71376445 100644 --- a/Flakkari/Network/Socket.cpp +++ b/Flakkari/Network/Socket.cpp @@ -11,7 +11,7 @@ namespace Flakkari::Network { -void Socket::create(std::shared_ptr
address) +void Socket::create(const std::shared_ptr
&address) { _address = address; _socket = INVALID_SOCKET; @@ -43,7 +43,7 @@ void Socket::create(std::shared_ptr
address) #endif } -void Socket::create(socket_t socket, std::shared_ptr
address) +void Socket::create(socket_t socket, const std::shared_ptr
&address) { _socket = socket; _address = address; @@ -67,7 +67,7 @@ void Socket::create(socket_t socket, std::shared_ptr
address) #endif } -void Socket::create(Address address) +void Socket::create(const Address &address) { _address = std::make_shared
(address); _socket = INVALID_SOCKET; diff --git a/Flakkari/Network/Socket.hpp b/Flakkari/Network/Socket.hpp index 0cfb67d3..ae1c8a51 100644 --- a/Flakkari/Network/Socket.hpp +++ b/Flakkari/Network/Socket.hpp @@ -133,9 +133,9 @@ class Socket { Socket(Socket &&) = delete; ~Socket(); - void create(std::shared_ptr
address); - void create(socket_t socket, std::shared_ptr
address); - void create(Address address); + void create(const std::shared_ptr
&address); + void create(socket_t socket, const std::shared_ptr
&address); + void create(const Address &address); void create(ip_t address, port_t port, Address::IpType ip_type, Address::SocketType socket_type); bool operator==(const Socket &other) const { return _socket == other._socket; } diff --git a/Flakkari/Server/UDPServer.cpp b/Flakkari/Server/UDPServer.cpp index 9030a9c7..1eaec131 100644 --- a/Flakkari/Server/UDPServer.cpp +++ b/Flakkari/Server/UDPServer.cpp @@ -19,10 +19,10 @@ UDPServer::UDPServer(const std::string &gameDir, const std::string &ip, unsigned _socket->create(ip, port, Network::Address::IpType::IPv4, Network::Address::SocketType::UDP); FLAKKARI_LOG_INFO(std::string(*_socket)); + _socket->setBlocking(false); _socket->bind(); - _io = std::make_unique(1); - _io->addSocket((int) _socket->getSocket()); + _io = std::make_unique(_socket->getSocket()); _io->addSocket(STDIN_FILENO); ClientManager::CreateInstance(_socket); diff --git a/Flakkari/Server/UDPServer.hpp b/Flakkari/Server/UDPServer.hpp index 7baf4616..ffd265a2 100644 --- a/Flakkari/Server/UDPServer.hpp +++ b/Flakkari/Server/UDPServer.hpp @@ -29,7 +29,7 @@ namespace Flakkari { #define GOTO_LOOP goto loop; #ifndef STDIN_FILENO -# define STDIN_FILENO 0 +# define STDIN_FILENO _fileno(stdin) #endif #define STR(x) #x