Log connection/disconnect properly
This commit is contained in:
parent
e8f6842c30
commit
bee6fde1a7
|
@ -41,12 +41,18 @@ namespace Feather
|
|||
|
||||
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();
|
||||
clients.emplace_back(std::move(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();
|
||||
clients.remove_if([&](MinecraftClient& other) { return other.GetTCPClient().get() == client; });
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
};
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "Lockable.h"
|
||||
#include "TCPCommon.h"
|
||||
#include "SocketAddress.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
@ -11,7 +12,7 @@ namespace Feather::Network
|
|||
class TCPClient
|
||||
{
|
||||
public:
|
||||
TCPClient(TCPListener* parent, SocketHandle socket);
|
||||
TCPClient(TCPListener* parent, SocketHandle socket, SocketAddress&& addr);
|
||||
~TCPClient();
|
||||
|
||||
void ReadCallback();
|
||||
|
@ -19,7 +20,9 @@ namespace Feather::Network
|
|||
void EventCallback(int16_t event);
|
||||
|
||||
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:
|
||||
TCPListener* m_parent;
|
||||
|
@ -27,5 +30,7 @@ namespace Feather::Network
|
|||
bufferevent* m_bufferEvent;
|
||||
|
||||
LockableVector<uint8_t> m_data;
|
||||
|
||||
SocketAddress m_address;
|
||||
};
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
#include <memory>
|
||||
|
||||
struct bufferevent;
|
||||
struct sockaddr;
|
||||
|
||||
namespace Feather::Network
|
||||
{
|
||||
|
@ -13,6 +14,8 @@ namespace Feather::Network
|
|||
using SocketHandle = int;
|
||||
#endif
|
||||
|
||||
struct SocketAddress;
|
||||
|
||||
class IListenerInterface;
|
||||
class TCPListener;
|
||||
class TCPSocket;
|
||||
|
|
|
@ -21,27 +21,23 @@
|
|||
|
||||
namespace Feather::Network
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template <size_t N>
|
||||
void WriteIP(char(&outIP)[N], uint16_t& outPort, sockaddr* addr)
|
||||
SocketAddress::SocketAddress(sockaddr* addr)
|
||||
{
|
||||
if (addr->sa_family == AF_INET)
|
||||
{
|
||||
auto sin = reinterpret_cast<const sockaddr_in*>(addr);
|
||||
evutil_inet_ntop(addr->sa_family, &sin->sin_addr,
|
||||
outIP, sizeof(outIP));
|
||||
evutil_inet_ntop(sin->sin_family, &sin->sin_addr,
|
||||
ip, sizeof(ip));
|
||||
|
||||
outPort = sin->sin_port;
|
||||
port = sin->sin_port;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto sin = reinterpret_cast<const sockaddr_in6*>(addr);
|
||||
evutil_inet_ntop(addr->sa_family, &sin->sin6_addr,
|
||||
outIP, sizeof(outIP));
|
||||
evutil_inet_ntop(sin->sin6_family, &sin->sin6_addr,
|
||||
ip, sizeof(ip));
|
||||
|
||||
outPort = sin->sin6_port;
|
||||
}
|
||||
port = sin->sin6_port;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,9 +132,10 @@ namespace Feather::Network
|
|||
bool m_ipv6;
|
||||
};
|
||||
|
||||
TCPClient::TCPClient(TCPListener* parent, SocketHandle socket)
|
||||
TCPClient::TCPClient(TCPListener* parent, SocketHandle socket, SocketAddress&& addr)
|
||||
: 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_address(std::move(addr))
|
||||
{
|
||||
bufferevent_setcb(m_bufferEvent,
|
||||
[](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);
|
||||
}
|
||||
|
||||
LockableVector<uint8_t>& TCPClient::GetData()
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
TCPListener::TCPListener(uint16_t port, IListenerInterface* callbacks)
|
||||
: m_callbacks(callbacks)
|
||||
{
|
||||
|
@ -232,12 +224,8 @@ namespace Feather::Network
|
|||
|
||||
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);
|
||||
listener->OnClientConnect(std::make_unique<TCPClient>(listener, socket));
|
||||
listener->OnClientConnect(std::make_unique<TCPClient>(listener, socket, addr));
|
||||
};
|
||||
|
||||
if (!socket->Listen(ListenerCallback, this))
|
||||
|
|
Loading…
Reference in New Issue