This commit is contained in:
LikhenkoVG 2025-04-04 00:05:34 +03:00
parent 994b0f8712
commit 9f0faac821
5 changed files with 237 additions and 6 deletions

View File

@ -0,0 +1,65 @@
#pragma once
#include <Windows.h>
#include <vcclr.h>
#using <System.dll>
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
namespace DroneClient {
public ref class Drone
{
public:
value struct DataOut
{
float AccX, AccY, AccZ;
float GyrX, GyrY, GyrZ;
float PosX, PosY;
float LaserRange;
};
value struct DataIn
{
float MotorUL, MotorUR, MotorDL, MotorDR;
};
static array<Byte>^ GetBytes(Object^ data)
{
int size = Marshal::SizeOf(data);
array<Byte>^ arr = gcnew array<Byte>(size);
IntPtr ptr = IntPtr::Zero;
try
{
ptr = Marshal::AllocHGlobal(size);
Marshal::StructureToPtr(data, ptr, true);
Marshal::Copy(ptr, arr, 0, size);
}
finally
{
Marshal::FreeHGlobal(ptr);
}
return arr;
}
static Object^ FromBytes(array<Byte>^ arr, Type^ type)
{
int size = Marshal::SizeOf(type);
IntPtr ptr = IntPtr::Zero;
try
{
ptr = Marshal::AllocHGlobal(size);
Marshal::Copy(arr, 0, ptr, size);
return Marshal::PtrToStructure(ptr, type);
}
finally
{
Marshal::FreeHGlobal(ptr);
}
}
};
}

View File

@ -118,14 +118,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="FormMain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
<ClInclude Include="DroneClient.h" />
<ClInclude Include="FormMain.h">
<FileType>CppForm</FileType>
</ClInclude>
<ClInclude Include="NetClient.h" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="FormMain.resx">

View File

@ -18,15 +18,15 @@
<ClCompile Include="FormMain.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="FormMain.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="Client.h">
<ClInclude Include="NetClient.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="DroneClient.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>

Binary file not shown.

166
DroneClientCpp/NetClient.h Normal file
View File

@ -0,0 +1,166 @@
// NetClient.h
#pragma once
#include <Windows.h>
#include <vcclr.h>
#using <System.dll>
#using <System.Net.dll>
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Runtime::InteropServices;
namespace DroneSimulator
{
public ref class NetClient
{
public:
// Вложенный класс для данных подключения
ref class ConnectData
{
public:
property bool Connect;
property Socket^ Server;
};
// Вложенный класс для данных приема
ref class ReceiveData
{
public:
property array<Byte>^ Buffer;
property int Size;
property Socket^ Server;
};
// Состояния клиента
enum class ClientState { Error, Connected, Stop };
// Делегат для callback-функций
delegate void ClientCallback(Object^ o);
NetClient()
{
Connected = false;
ServerSocket = nullptr;
DataServer = gcnew ServerData();
}
~NetClient()
{
Close();
}
ClientState Connect(String^ addr, int port, ClientCallback^ connectionCallback, ClientCallback^ receiveCallback)
{
if (Connected)
{
Disconnect();
return ClientState::Stop;
}
ConnectionCallback = connectionCallback;
ReceiveCallback = receiveCallback;
try
{
IPEndPoint^ ep = gcnew IPEndPoint(IPAddress::Parse(addr), port);
ServerSocket = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
ServerSocket->Connect(ep);
Connected = true;
// Уведомление о подключении
ConnectData^ connectData = gcnew ConnectData();
connectData->Connect = true;
connectData->Server = ServerSocket;
ConnectionCallback(connectData);
// Начало приема данных
ReceiveData^ receiveData = gcnew ReceiveData();
receiveData->Buffer = DataServer->buffer;
receiveData->Size = ServerData::size;
receiveData->Server = ServerSocket;
ServerSocket->BeginReceive(DataServer->buffer, 0, ServerData::size,
SocketFlags::None, gcnew AsyncCallback(this, &NetClient::ReadCallback), receiveData);
return ClientState::Connected;
}
catch (Exception^)
{
if (ServerSocket != nullptr)
ServerSocket->Close();
return ClientState::Error;
}
}
void Close()
{
Disconnect();
}
private:
ref class ServerData
{
public:
static const int size = 1024;
array<Byte>^ buffer = gcnew array<Byte>(size);
};
bool Connected;
Socket^ ServerSocket;
ServerData^ DataServer;
ClientCallback^ ConnectionCallback;
ClientCallback^ ReceiveCallback;
void Disconnect()
{
if (ServerSocket != nullptr)
{
try { ServerSocket->Shutdown(SocketShutdown::Both); }
catch (...) {}
ServerSocket->Close();
ServerSocket = nullptr;
}
Connected = false;
}
void ReadCallback(IAsyncResult^ ar)
{
ReceiveData^ cd = safe_cast<ReceiveData^>(ar->AsyncState);
if (cd == nullptr) return;
int bytes = 0;
try { bytes = ServerSocket->EndReceive(ar); }
catch (...) {}
if (bytes == 0)
{
Disconnect();
if (ConnectionCallback != nullptr)
{
ConnectData^ disconnectData = gcnew ConnectData();
disconnectData->Connect = false;
disconnectData->Server = nullptr;
ConnectionCallback(disconnectData);
}
return;
}
if (ReceiveCallback != nullptr)
{
ReceiveCallback(cd);
}
try
{
ServerSocket->BeginReceive(cd->Buffer, 0, ServerData::size,
SocketFlags::None, gcnew AsyncCallback(this, &NetClient::ReadCallback), cd);
}
catch (...) {}
}
};
}