Skip to content

Commit

Permalink
Merge pull request #43 from MasterLaplace/42-bug-failed-to-connect
Browse files Browse the repository at this point in the history
Fix bug failed to connect
  • Loading branch information
MasterLaplace authored Nov 8, 2024
2 parents 7026c57 + 273bc8c commit 6375b59
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
15 changes: 7 additions & 8 deletions Flakkari/Network/Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Flakkari/Network/IOMultiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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())
{
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions Flakkari/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Flakkari::Network {

void Socket::create(std::shared_ptr<Address> address)
void Socket::create(const std::shared_ptr<Address> &address)
{
_address = address;
_socket = INVALID_SOCKET;
Expand Down Expand Up @@ -43,7 +43,7 @@ void Socket::create(std::shared_ptr<Address> address)
#endif
}

void Socket::create(socket_t socket, std::shared_ptr<Address> address)
void Socket::create(socket_t socket, const std::shared_ptr<Address> &address)
{
_socket = socket;
_address = address;
Expand All @@ -67,7 +67,7 @@ void Socket::create(socket_t socket, std::shared_ptr<Address> address)
#endif
}

void Socket::create(Address address)
void Socket::create(const Address &address)
{
_address = std::make_shared<Address>(address);
_socket = INVALID_SOCKET;
Expand Down
6 changes: 3 additions & 3 deletions Flakkari/Network/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ class Socket {
Socket(Socket &&) = delete;
~Socket();

void create(std::shared_ptr<Address> address);
void create(socket_t socket, std::shared_ptr<Address> address);
void create(Address address);
void create(const std::shared_ptr<Address> &address);
void create(socket_t socket, const std::shared_ptr<Address> &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; }
Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Server/UDPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IO_SELECTED>(1);
_io->addSocket((int) _socket->getSocket());
_io = std::make_unique<IO_SELECTED>(_socket->getSocket());
_io->addSocket(STDIN_FILENO);

ClientManager::CreateInstance(_socket);
Expand Down
2 changes: 1 addition & 1 deletion Flakkari/Server/UDPServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6375b59

Please sign in to comment.