From 3fbb7f069133de96f07028c9c24ea2bc1fe0abf6 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Sat, 1 Aug 2020 07:35:18 +0100 Subject: [PATCH] Logging to TCPListener --- src/network/TCPListener.cpp | 55 +++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/network/TCPListener.cpp b/src/network/TCPListener.cpp index 1cc98f2..859cb82 100644 --- a/src/network/TCPListener.cpp +++ b/src/network/TCPListener.cpp @@ -17,9 +17,34 @@ #include #include +#include namespace Feather::Network { + namespace + { + template + void WriteIP(char(&outIP)[N], uint16_t& outPort, sockaddr* addr) + { + if (addr->sa_family == AF_INET) + { + auto sin = reinterpret_cast(addr); + evutil_inet_ntop(addr->sa_family, &sin->sin_addr, + outIP, sizeof(outIP)); + + outPort = sin->sin_port; + } + else + { + auto sin = reinterpret_cast(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(self)->ReadCallback(); }, [](bufferevent* be, void* self) { static_cast(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& TCPClient::GetData() @@ -174,28 +201,50 @@ namespace Feather::Network auto socket = std::make_unique(); 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(self); listener->OnClientConnect(std::make_unique(listener, socket)); }; if (!socket->Listen(ListenerCallback, this)) + { + Log_Error("Failed to start listening on socket."); return; + } m_socket = std::move(socket); }