Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnshschl committed Jan 5, 2024
1 parent b4caf35 commit b37e09a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
27 changes: 12 additions & 15 deletions AnTCP.Server/src/AnTcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,36 @@ AnTcpError AnTcpServer::Run() noexcept
if (result == SOCKET_ERROR)
{
DEBUG_ONLY(std::cout << ">> bind() failed: " << WSAGetLastError() << std::endl);
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
SocketCleanup();
WSACleanup();
return AnTcpError::SocketBindingFailed;
}

if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
{
DEBUG_ONLY(std::cout << ">> listen() failed: " << WSAGetLastError() << std::endl);
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
SocketCleanup();
WSACleanup();
return AnTcpError::SocketListeningFailed;
}


while (!ShouldExit)
{
// accept client and get socket info from it, the socket info contains the ip address
// and port used to connect o the server
SOCKADDR_IN clientInfo{ 0 };
int size = sizeof(SOCKADDR_IN);
SOCKET clientSocket = accept(ListenSocket, reinterpret_cast<sockaddr*>(&clientInfo), &size);
int sockAddrSize = static_cast<int>(sizeof(SOCKADDR_IN));
SOCKET clientSocket = accept(ListenSocket, reinterpret_cast<sockaddr*>(&clientInfo), &sockAddrSize);

if (clientSocket == INVALID_SOCKET)
{
DEBUG_ONLY(std::cout << ">> accept() failed: " << WSAGetLastError() << std::endl);
continue;
}

// cleanup old disconnected clients
// cleanup old disconnected clients and add the new
ClientCleanup();

Clients.push_back(new ClientHandler(clientSocket, clientInfo, ShouldExit, &Callbacks, &OnClientConnected, &OnClientDisconnected));
}

Expand All @@ -86,26 +84,25 @@ AnTcpError AnTcpServer::Run() noexcept
}
}

Clients.clear();

SocketCleanup();
WSACleanup();
return AnTcpError::Success;
}

void ClientHandler::Listen() noexcept
{
if (OnClientConnected)
{
(*OnClientConnected)(this);
}

// the total packet size and data ptr offset
AnTcpSizeType packetSize = 0;
AnTcpSizeType packetOffset = 0;

// buffer for the packet
char packet[sizeof(AnTcpSizeType) + ANTCP_MAX_PACKET_SIZE]{ 0 };

if (OnClientConnected)
{
(*OnClientConnected)(this);
}

while (!ShouldExit)
{
auto packetBytesMissing = packetSize - packetOffset;
Expand Down
44 changes: 26 additions & 18 deletions AnTCP.Server/src/AnTcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <ws2tcpip.h>
#include <iphlpapi.h>

constexpr auto ANTCP_SERVER_VERSION = "1.2.0.0";
constexpr auto ANTCP_SERVER_VERSION = "1.2.1.0";
constexpr auto ANTCP_MAX_PACKET_SIZE = 256;

// type used in the payload to specify the size of a packet
Expand Down Expand Up @@ -104,7 +104,7 @@ class ClientHandler
/// <summary>
/// Get the AnTCP handler id.
/// </summary>
constexpr int GetId() noexcept { return Id; }
constexpr auto GetId() const noexcept { return Id; }

/// <summary>
/// Used to delete old disconnected clients.
Expand Down Expand Up @@ -151,10 +151,10 @@ class ClientHandler
/// <returns>True if data was sent, false if not.</returns>
inline bool SendData(AnTcpMessageType type, const void* data, size_t size) const noexcept
{
const int packetSize = size + static_cast<int>(sizeof(AnTcpMessageType));
return send(Socket, reinterpret_cast<const char*>(&packetSize), sizeof(decltype(packetSize)), 0) != SOCKET_ERROR
&& send(Socket, &type, sizeof(AnTcpMessageType), 0) != SOCKET_ERROR
&& send(Socket, static_cast<const char*>(data), size, 0) != SOCKET_ERROR;
const size_t packetSize = size + sizeof(AnTcpMessageType);
return send(Socket, reinterpret_cast<const char*>(&packetSize), static_cast<int>(sizeof(decltype(packetSize))), 0) != SOCKET_ERROR
&& send(Socket, &type, static_cast<int>(sizeof(AnTcpMessageType)), 0) != SOCKET_ERROR
&& send(Socket, static_cast<const char*>(data), static_cast<int>(size), 0) != SOCKET_ERROR;
}

/// <summary>
Expand All @@ -176,7 +176,7 @@ class ClientHandler
/// Get the clients ip address.
/// </summary>
/// <returns>IP address as string.</returns>
inline std::string GetIpAddress() noexcept
inline std::string GetIpAddress() const noexcept
{
char ipAddressBuffer[128]{ 0 };
inet_ntop(AF_INET, &SocketInfo.sin_addr, ipAddressBuffer, 128);
Expand All @@ -186,12 +186,12 @@ class ClientHandler
/// <summary>
/// Get the client connection port.
/// </summary>
constexpr unsigned short GetPort() noexcept
constexpr unsigned short GetPort() const noexcept
{
return SocketInfo.sin_port;
}

constexpr unsigned short GetAddressFamily() noexcept
constexpr unsigned short GetAddressFamily() const noexcept
{
return SocketInfo.sin_family;
}
Expand Down Expand Up @@ -344,6 +344,8 @@ class AnTcpServer
ShouldExit = true;
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
Clients.clear();
WSACleanup();
}

/// <summary>
Expand All @@ -353,27 +355,33 @@ class AnTcpServer
AnTcpError Run() noexcept;

private:
constexpr void SocketCleanup() noexcept
{
if (ListenSocket != INVALID_SOCKET)
{
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
}
}

/// <summary>
/// Delete old clients that are not running.
/// </summary>
constexpr void ClientCleanup() noexcept
{
for (size_t i = 0; i < Clients.size(); ++i)
{
if (Clients[i]->IsConnected())
if (Clients[i] && Clients[i]->IsConnected())
{
if (Clients[i])
if (OnClientDisconnected)
{
if (OnClientDisconnected)
{
OnClientDisconnected(Clients[i]);
}

delete Clients[i];
OnClientDisconnected(Clients[i]);
}

Clients.erase(Clients.begin() + i);
delete Clients[i];
}

Clients.erase(Clients.begin() + i);
}
}
};

0 comments on commit b37e09a

Please sign in to comment.