Log connection/disconnect properly

This commit is contained in:
Joshua Ashton 2020-08-02 00:35:54 +01:00
parent e8f6842c30
commit bee6fde1a7
5 changed files with 50 additions and 30 deletions

View File

@ -41,12 +41,18 @@ namespace Feather
void DedicatedServer::OnClientConnect(Network::TCPClientHandle&& client) void DedicatedServer::OnClientConnect(Network::TCPClientHandle&& client)
{ {
const auto& address = client->GetAddress();
Log_Info("New connection from %s:%hu", address.ip, address.port);
auto [clients, lock] = m_clients.borrow(); auto [clients, lock] = m_clients.borrow();
clients.emplace_back(std::move(client)); clients.emplace_back(std::move(client));
} }
void DedicatedServer::OnClientDisconnect(const Network::TCPClient* client) void DedicatedServer::OnClientDisconnect(const Network::TCPClient* client)
{ {
const auto& address = client->GetAddress();
Log_Info("Disconnected from %s:%hu", address.ip, address.port);
auto [clients, lock] = m_clients.borrow(); auto [clients, lock] = m_clients.borrow();
clients.remove_if([&](MinecraftClient& other) { return other.GetTCPClient().get() == client; }); clients.remove_if([&](MinecraftClient& other) { return other.GetTCPClient().get() == client; });
} }

View File

@ -0,0 +1,18 @@
#pragma once
#include "TCPCommon.h"
#include <string>
namespace Feather::Network
{
struct SocketAddress
{
static constexpr size_t AddressLength = 65;
SocketAddress(sockaddr* addr);
uint16_t port;
char ip[AddressLength];
};
}

View File

@ -2,6 +2,7 @@
#include "Lockable.h" #include "Lockable.h"
#include "TCPCommon.h" #include "TCPCommon.h"
#include "SocketAddress.h"
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
@ -11,7 +12,7 @@ namespace Feather::Network
class TCPClient class TCPClient
{ {
public: public:
TCPClient(TCPListener* parent, SocketHandle socket); TCPClient(TCPListener* parent, SocketHandle socket, SocketAddress&& addr);
~TCPClient(); ~TCPClient();
void ReadCallback(); void ReadCallback();
@ -19,7 +20,9 @@ namespace Feather::Network
void EventCallback(int16_t event); void EventCallback(int16_t event);
void Write(const uint8_t* data, size_t size); void Write(const uint8_t* data, size_t size);
LockableVector<uint8_t>& GetData();
LockableVector<uint8_t>& GetData() { return m_data; }
const SocketAddress& GetAddress() const { return m_address; }
private: private:
TCPListener* m_parent; TCPListener* m_parent;
@ -27,5 +30,7 @@ namespace Feather::Network
bufferevent* m_bufferEvent; bufferevent* m_bufferEvent;
LockableVector<uint8_t> m_data; LockableVector<uint8_t> m_data;
SocketAddress m_address;
}; };
} }

View File

@ -4,6 +4,7 @@
#include <memory> #include <memory>
struct bufferevent; struct bufferevent;
struct sockaddr;
namespace Feather::Network namespace Feather::Network
{ {
@ -13,6 +14,8 @@ namespace Feather::Network
using SocketHandle = int; using SocketHandle = int;
#endif #endif
struct SocketAddress;
class IListenerInterface; class IListenerInterface;
class TCPListener; class TCPListener;
class TCPSocket; class TCPSocket;

View File

@ -21,27 +21,23 @@
namespace Feather::Network namespace Feather::Network
{ {
namespace SocketAddress::SocketAddress(sockaddr* addr)
{ {
template <size_t N> if (addr->sa_family == AF_INET)
void WriteIP(char(&outIP)[N], uint16_t& outPort, sockaddr* addr)
{ {
if (addr->sa_family == AF_INET) auto sin = reinterpret_cast<const sockaddr_in*>(addr);
{ evutil_inet_ntop(sin->sin_family, &sin->sin_addr,
auto sin = reinterpret_cast<const sockaddr_in*>(addr); ip, sizeof(ip));
evutil_inet_ntop(addr->sa_family, &sin->sin_addr,
outIP, sizeof(outIP));
outPort = sin->sin_port; port = sin->sin_port;
} }
else else
{ {
auto sin = reinterpret_cast<const sockaddr_in6*>(addr); auto sin = reinterpret_cast<const sockaddr_in6*>(addr);
evutil_inet_ntop(addr->sa_family, &sin->sin6_addr, evutil_inet_ntop(sin->sin6_family, &sin->sin6_addr,
outIP, sizeof(outIP)); ip, sizeof(ip));
outPort = sin->sin6_port; port = sin->sin6_port;
}
} }
} }
@ -136,9 +132,10 @@ namespace Feather::Network
bool m_ipv6; bool m_ipv6;
}; };
TCPClient::TCPClient(TCPListener* parent, SocketHandle socket) TCPClient::TCPClient(TCPListener* parent, SocketHandle socket, SocketAddress&& addr)
: m_parent(parent) : m_parent(parent)
, m_bufferEvent(bufferevent_socket_new(NetworkManager::Instance().GetEventBase(), socket, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE | BEV_OPT_DEFER_CALLBACKS | BEV_OPT_UNLOCK_CALLBACKS)) , m_bufferEvent(bufferevent_socket_new(NetworkManager::Instance().GetEventBase(), socket, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE | BEV_OPT_DEFER_CALLBACKS | BEV_OPT_UNLOCK_CALLBACKS))
, m_address(std::move(addr))
{ {
bufferevent_setcb(m_bufferEvent, bufferevent_setcb(m_bufferEvent,
[](bufferevent* be, void* self) { static_cast<TCPClient*>(self)->ReadCallback(); }, [](bufferevent* be, void* self) { static_cast<TCPClient*>(self)->ReadCallback(); },
@ -189,11 +186,6 @@ namespace Feather::Network
Log_Error("Failed to write to socket, size: " PRIuPTR ".", size); Log_Error("Failed to write to socket, size: " PRIuPTR ".", size);
} }
LockableVector<uint8_t>& TCPClient::GetData()
{
return m_data;
}
TCPListener::TCPListener(uint16_t port, IListenerInterface* callbacks) TCPListener::TCPListener(uint16_t port, IListenerInterface* callbacks)
: m_callbacks(callbacks) : m_callbacks(callbacks)
{ {
@ -232,12 +224,8 @@ namespace Feather::Network
auto ListenerCallback = [](evconnlistener* evListener, SocketHandle socket, sockaddr* addr, int len, void* self) auto ListenerCallback = [](evconnlistener* evListener, SocketHandle socket, sockaddr* addr, int len, void* self)
{ {
char ipAddress[INET6_ADDRSTRLEN]; uint16_t port;
WriteIP(ipAddress, port, addr);
Log_Info("Connection from: %s:%hu", ipAddress, port);
TCPListener* listener = static_cast<TCPListener*>(self); TCPListener* listener = static_cast<TCPListener*>(self);
listener->OnClientConnect(std::make_unique<TCPClient>(listener, socket)); listener->OnClientConnect(std::make_unique<TCPClient>(listener, socket, addr));
}; };
if (!socket->Listen(ListenerCallback, this)) if (!socket->Listen(ListenerCallback, this))