Logging to TCPListener
This commit is contained in:
parent
50da8c2508
commit
3fbb7f0691
|
@ -17,9 +17,34 @@
|
|||
|
||||
#include <mutex>
|
||||
#include <cstring>
|
||||
#include <cinttypes>
|
||||
|
||||
namespace Feather::Network
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template <size_t N>
|
||||
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(addr->sa_family, &sin->sin_addr,
|
||||
outIP, sizeof(outIP));
|
||||
|
||||
outPort = sin->sin_port;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto sin = reinterpret_cast<const sockaddr_in6*>(addr);
|
||||
evutil_inet_ntop(addr->sa_family, &sin->sin6_addr,
|
||||
outIP, sizeof(outIP));
|
||||
|
||||
outPort = sin->sin6_port;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TCPSocket
|
||||
{
|
||||
public:
|
||||
|
@ -29,7 +54,10 @@ namespace Feather::Network
|
|||
|
||||
// Can't create IPv6 socket? Create an IPv4 one.
|
||||
if (!(m_ipv6 = IsValid()))
|
||||
{
|
||||
Log_Info("Failed to create IPv6 socket, falling back to IPv4.");
|
||||
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
}
|
||||
}
|
||||
|
||||
~TCPSocket()
|
||||
|
@ -112,7 +140,6 @@ namespace Feather::Network
|
|||
: 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))
|
||||
{
|
||||
printf("Created TCPListenerClient\n");
|
||||
bufferevent_setcb(m_bufferEvent,
|
||||
[](bufferevent* be, void* self) { static_cast<TCPClient*>(self)->ReadCallback(); },
|
||||
[](bufferevent* be, void* self) { static_cast<TCPClient*>(self)->WriteCallback(); },
|
||||
|
@ -135,7 +162,7 @@ namespace Feather::Network
|
|||
data.resize(offset + size);
|
||||
if (evbuffer_remove(buffer, &data[offset], size) != size)
|
||||
{
|
||||
printf("fuck");
|
||||
Log_Error("Failed to remove data from buffer.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -159,7 +186,7 @@ namespace Feather::Network
|
|||
void TCPClient::Write(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (bufferevent_write(m_bufferEvent, data, size) != 0)
|
||||
printf("Fuck!");
|
||||
Log_Error("Failed to write to socket, size: " PRIuPTR ".", size);
|
||||
}
|
||||
|
||||
LockableVector<uint8_t>& TCPClient::GetData()
|
||||
|
@ -174,28 +201,50 @@ namespace Feather::Network
|
|||
auto socket = std::make_unique<TCPSocket>();
|
||||
|
||||
if (!socket->IsValid())
|
||||
{
|
||||
Log_Error("Socket failed to be created.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!socket->MarkReusable())
|
||||
{
|
||||
Log_Error("Failed to mark socket as resuable.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (socket->IsIPV6() && !socket->MarkDualBind())
|
||||
{
|
||||
Log_Error("Failed to mark IPv6 socket as dual-bind.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!socket->Bind(port))
|
||||
{
|
||||
Log_Error("Failed to bind socket to port %hu.", port);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!socket->MarkNonBlocking())
|
||||
{
|
||||
Log_Error("Failed to mark socket as non-blocking.");
|
||||
return;
|
||||
}
|
||||
|
||||
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));
|
||||
};
|
||||
|
||||
if (!socket->Listen(ListenerCallback, this))
|
||||
{
|
||||
Log_Error("Failed to start listening on socket.");
|
||||
return;
|
||||
}
|
||||
|
||||
m_socket = std::move(socket);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue