Skip to content

Commit

Permalink
update antcp, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnshschl committed Jan 6, 2024
1 parent f95e476 commit fd8eeb4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion AmeisenNavigation.Server/AmeisenNavigation.Server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
Expand Down
2 changes: 1 addition & 1 deletion AmeisenNavigation.Server/src/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <iostream>
#include <mutex>

constexpr auto AMEISENNAV_VERSION = "1.8.0.0";
constexpr auto AMEISENNAV_VERSION = "1.8.1.0";

constexpr auto VEC3_SIZE = sizeof(float) * 3;

Expand Down
4 changes: 2 additions & 2 deletions AmeisenNavigation.Tester/AmeisenNavigation.Tester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net8.0-windows7.0</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand All @@ -18,7 +18,7 @@

<ItemGroup>
<PackageReference Include="AnTCP.Client" Version="1.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion AmeisenNavigation.Tester/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void Run(int flags, PathType type)
int boundsX = (int)(maxX - minX + (padding * 2.0f));
int boundsY = (int)(maxY - minY + (padding * 2.0f));

List<Vector3> normalizedPath = new();
List<Vector3> normalizedPath = [];

foreach (Vector3 v3 in path)
{
Expand Down
61 changes: 34 additions & 27 deletions dep/AnTCP.Server/include/AnTcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
#include <ws2tcpip.h>
#include <iphlpapi.h>

constexpr auto ANTCP_SERVER_VERSION = "1.1.0.0";
constexpr auto ANTCP_BUFFER_LENGTH = 512;
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 @@ -78,12 +77,12 @@ class ClientHandler
std::function<void(ClientHandler*)>* onClientConnected = nullptr,
std::function<void(ClientHandler*)>* onClientDisconnected = nullptr
)
: IsActive(true),
Id(static_cast<unsigned int>(socketInfo.sin_addr.S_un.S_addr + socketInfo.sin_port)),
: Id(static_cast<unsigned int>(socketInfo.sin_addr.S_un.S_addr + socketInfo.sin_port)),
Socket(socket),
SocketInfo(socketInfo),
ShouldExit(shouldExit),
Callbacks(callbacks),
IsActive(true),
Thread(new std::thread(&ClientHandler::Listen, this)),
OnClientConnected(onClientConnected),
OnClientDisconnected(onClientDisconnected)
Expand All @@ -105,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 @@ -152,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 @@ -177,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 @@ -187,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 All @@ -211,27 +210,27 @@ class ClientHandler
/// <param name="type">Message type.</param>
/// <param name="data">Data received.</param>
/// <returns>True if callback was fired, false if not.</returns>
inline bool ProcessPacket(const char* data, int size) noexcept
inline bool ProcessPacket(const char* data, AnTcpMessageType size) noexcept
{
const AnTcpMessageType msgType = *reinterpret_cast<const AnTcpMessageType*>(data);
auto msgType = *reinterpret_cast<const AnTcpMessageType*>(data);

if ((*Callbacks).contains(msgType))
{
// measure packet processing time in debug mode
BENCHMARK(const auto packetStart = std::chrono::high_resolution_clock::now());

// fire the callback with the raw data
(*Callbacks)[msgType](this, msgType, data + static_cast<int>(sizeof(AnTcpMessageType)), size - static_cast<int>(sizeof(AnTcpMessageType)));
(*Callbacks)[msgType](this, msgType, data + sizeof(AnTcpMessageType), size - sizeof(AnTcpMessageType));

BENCHMARK(std::cout << "[" << Id << "] " << "Processing packet of type \""
<< std::to_string(*reinterpret_cast<const AnTcpMessageType*>(data)) << "\" took: "
<< std::to_string(msgType) << "\" took: "
<< std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - packetStart) << std::endl);

return true;
}

DEBUG_ONLY(std::cout << "[" << Id << "] " << "\"" << std::to_string(*reinterpret_cast<const AnTcpMessageType*>(data))
<< "\" is an unknown message type, disconnecting client..." << std::endl);
DEBUG_ONLY(std::cout << "[" << Id << "] " << "\"" << std::to_string(msgType)
<< "\" is an unknown message type..." << std::endl);

return false;
}
Expand Down Expand Up @@ -345,6 +344,8 @@ class AnTcpServer
ShouldExit = true;
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
Clients.clear();
WSACleanup();
}

/// <summary>
Expand All @@ -354,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);
}
}
};
Binary file modified dep/AnTCP.Server/lib/Debug/x64/AnTCP.Server.lib
Binary file not shown.
Binary file modified dep/AnTCP.Server/lib/Debug/x86/AnTCP.Server.lib
Binary file not shown.
Binary file modified dep/AnTCP.Server/lib/Release/x64/AnTCP.Server.lib
Binary file not shown.
Binary file modified dep/AnTCP.Server/lib/Release/x86/AnTCP.Server.lib
Binary file not shown.

0 comments on commit fd8eeb4

Please sign in to comment.