From 994b0f87128b51cd3014f6bd00c190c2f2d69ef4 Mon Sep 17 00:00:00 2001 From: Dizel Date: Thu, 3 Apr 2025 13:07:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DroneClientCpp/Client.cpp | 106 ++++++++++++++++++ DroneClientCpp/Client.h | 44 ++++++++ DroneClientCpp/DroneClientCpp.vcxproj | 2 + DroneClientCpp/DroneClientCpp.vcxproj.filters | 6 + 4 files changed, 158 insertions(+) create mode 100644 DroneClientCpp/Client.cpp create mode 100644 DroneClientCpp/Client.h diff --git a/DroneClientCpp/Client.cpp b/DroneClientCpp/Client.cpp new file mode 100644 index 0000000..7fab2d4 --- /dev/null +++ b/DroneClientCpp/Client.cpp @@ -0,0 +1,106 @@ +#pragma once + +#include "Client.h" + +Client::Client(const char* ip, int port) : running(true), Connection(INVALID_SOCKET) +{ + if (!ConnectToServer(ip, port)) + { + std::cerr << "Failed to connect to server!" << std::endl; + } +} + +Client::~Client() +{ + Stop(); + CloseConnection(); +} + +bool Client::ConnectToServer(const char* ip, int port) +{ + WSAData wsaData; + WORD DLLVersion = MAKEWORD(2, 2); + + if (WSAStartup(DLLVersion, &wsaData) != 0) + { + std::cerr << "Error: WSAStartup failed" << std::endl; + return false; + } + + SOCKADDR_IN addr; + inet_pton(AF_INET, ip, &addr.sin_addr); + addr.sin_port = htons(port); + addr.sin_family = AF_INET; + + Connection = socket(AF_INET, SOCK_STREAM, 0); + if (Connection == INVALID_SOCKET) + { + std::cerr << "Error: Failed to create socket" << std::endl; + WSACleanup(); + return false; + } + + if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) + { + std::cerr << "Error: Failed to connect to server" << std::endl; + closesocket(Connection); + WSACleanup(); + return false; + } + + return true; +} + +void Client::CloseConnection() +{ + if (Connection != INVALID_SOCKET) + { + closesocket(Connection); + WSACleanup(); + Connection = INVALID_SOCKET; + } +} + +void Client::ReceiveHandler() +{ + while (running) + { + DataIn tempData; + int result = recv(Connection, (char*)&tempData, sizeof(tempData), 0); + + if (result <= 0) + { + std::cerr << "Error: Connection lost (recv failed)" << std::endl; + running = false; + break; + } + + std::lock_guard lock(dataMutex); + dataIn = tempData; + } +} + +void Client::SendHandler() +{ + while (running) + { + { + std::lock_guard lock(dataMutex); + send(Connection, (char*)&dataOut, sizeof(dataOut), 0); + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } +} + +void Client::Start() +{ + recvThread = std::thread(&Client::ReceiveHandler, this); + sendThread = std::thread(&Client::SendHandler, this); +} + +void Client::Stop() +{ + running = false; + if (recvThread.joinable()) recvThread.join(); + if (sendThread.joinable()) sendThread.join(); +} diff --git a/DroneClientCpp/Client.h b/DroneClientCpp/Client.h new file mode 100644 index 0000000..6801903 --- /dev/null +++ b/DroneClientCpp/Client.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#pragma comment(lib, "Ws2_32.lib") + +class Client +{ +public: + Client(const char* ip, int port); + ~Client(); + + void Start(); // Запуск потоков + void Stop(); // Остановка потоков + struct DataIn + { + float AccX, AccY, AccZ; + float GyrX, GyrY, GyrZ; + float PosX, PosY, LaserRange; + } dataIn; + + struct DataOut + { + float MotorUL, MotorUR, MotorDL, MotorDR; + } dataOut; + bool ConnectToServer(const char* ip, int port); + void CloseConnection(); + +private: + std::atomic running; + std::mutex dataMutex; + + SOCKET Connection; + std::thread recvThread; + std::thread sendThread; + + void ReceiveHandler(); + void SendHandler(); +}; \ No newline at end of file diff --git a/DroneClientCpp/DroneClientCpp.vcxproj b/DroneClientCpp/DroneClientCpp.vcxproj index 46349e2..ce872f9 100644 --- a/DroneClientCpp/DroneClientCpp.vcxproj +++ b/DroneClientCpp/DroneClientCpp.vcxproj @@ -118,9 +118,11 @@ + + CppForm diff --git a/DroneClientCpp/DroneClientCpp.vcxproj.filters b/DroneClientCpp/DroneClientCpp.vcxproj.filters index 5ad5172..7b8fd21 100644 --- a/DroneClientCpp/DroneClientCpp.vcxproj.filters +++ b/DroneClientCpp/DroneClientCpp.vcxproj.filters @@ -18,10 +18,16 @@ Исходные файлы + + Исходные файлы + Файлы заголовков + + Файлы заголовков + \ No newline at end of file