From 2bb7e5cfe45ab1e7dbc6fa6e45c70e318323a87d Mon Sep 17 00:00:00 2001 From: LegendaV <115637903+LegendaV@users.noreply.github.com> Date: Thu, 22 Jan 2026 13:20:00 +0500 Subject: [PATCH 1/6] v0.01 --- .gitignore | 1 + CMakeLists.txt | 2 +- client.cpp | 20 ++++++++++++++++++++ client.hpp | 17 +++++++++++++++++ main.cpp | 15 +++++++++++---- serial.cpp | 11 +++++++++++ serial.hpp | 2 ++ serial_client.cpp | 21 +++++++++++++++++++++ serial_client.hpp | 22 ++++++++++++++++++++++ udp_client.cpp | 21 +++++++++++++++++++++ udp_client.hpp | 23 +++++++++++++++++++++++ 11 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 client.cpp create mode 100644 client.hpp create mode 100644 serial_client.cpp create mode 100644 serial_client.hpp create mode 100644 udp_client.cpp create mode 100644 udp_client.hpp diff --git a/.gitignore b/.gitignore index a4fb4fb..d877c2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ .cache/ +.vs \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a61bd74..be82c96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,4 +15,4 @@ target_sources(multicast PRIVATE udp.cpp serial.cpp serial.hpp -) + "client.cpp" "udp_client.cpp" "serial_client.cpp") diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..7406574 --- /dev/null +++ b/client.cpp @@ -0,0 +1,20 @@ +#include "client.hpp" +#include + +namespace multicast +{ +namespace client +{ + +Client::Client() +{ + +} + +bool Client::send(std::string_view message) +{ + return true; +} + +} // namespace client +} // namespace multicast \ No newline at end of file diff --git a/client.hpp b/client.hpp new file mode 100644 index 0000000..0a1b3b7 --- /dev/null +++ b/client.hpp @@ -0,0 +1,17 @@ +#include + +namespace multicast +{ +namespace client +{ + +class Client +{ +public: + Client(); + + bool send(std::string_view message); +}; + +} // namespace client +} // namespace multicast \ No newline at end of file diff --git a/main.cpp b/main.cpp index a709ecb..3657626 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,9 @@ #include "config.hpp" #include "udp.hpp" +#include "client.hpp" +#include "udp_client.hpp" +#include "serial_client.hpp" namespace { @@ -45,15 +48,16 @@ int main(int argc, char* argv[]) Config config{argv[1]}; udp::Socket socket{}; + std::vector clients; + for ([[maybe_unused]] const auto& entry : config.GetUdpEntries()) { - // TODO: добавьте клиента (используйте объект socket) + clients.push_back(udp_client::UdpClient(&socket, entry.address)); } for ([[maybe_unused]] const auto& entry : config.GetSerialEntries()) { - // TODO: добавьте клиента (для каждого клиента создавайте - // новые объекты serial::Device) + clients.push_back(serial_client::SerialClient(serial::Device(entry.device))); } std::string input{}; @@ -61,7 +65,10 @@ int main(int argc, char* argv[]) { input.push_back('\n'); - // TODO: отправьте всем клиентам + for (client::Client& client : clients) + { + client.send(input); + } } return EXIT_SUCCESS; } diff --git a/serial.cpp b/serial.cpp index 0edeb76..82be2d9 100644 --- a/serial.cpp +++ b/serial.cpp @@ -52,6 +52,17 @@ int Device::Handler() const noexcept return fd_; } +Device::Device(Device&& other) noexcept : fd_(std::exchange(other.fd_, -1)) { } + +Device& Device::operator=(Device&& other) noexcept +{ + if (this != &other) + { + fd_ = std::exchange(other.fd_, -1); + } + return *this; +} + bool send(const Device& device, std::string_view message) { return write(device.Handler(), message.data(), message.size()) >= 0; diff --git a/serial.hpp b/serial.hpp index cbf833e..489cd02 100644 --- a/serial.hpp +++ b/serial.hpp @@ -12,6 +12,8 @@ class Device public: Device(std::string_view path); int Handler() const noexcept; + Device(Device&&) noexcept; + Device& operator=(Device&&) noexcept; private: int fd_; diff --git a/serial_client.cpp b/serial_client.cpp new file mode 100644 index 0000000..f8c0c2d --- /dev/null +++ b/serial_client.cpp @@ -0,0 +1,21 @@ +#include +#include "serial_client.hpp" + +namespace multicast +{ +namespace serial_client +{ + +SerialClient::SerialClient(const serial::Device& device) +{ + this->device = std::move(device); +} + + +bool SerialClient::send(std::string_view message) +{ + return serial::send(device, message); +} + +} // namespace serial_client +} // namespace multicast \ No newline at end of file diff --git a/serial_client.hpp b/serial_client.hpp new file mode 100644 index 0000000..4658075 --- /dev/null +++ b/serial_client.hpp @@ -0,0 +1,22 @@ +#include +#include "client.hpp" +#include "serial.hpp" + +namespace multicast +{ +namespace serial_client +{ + +class SerialClient : public client::Client +{ +public: + SerialClient(const serial::Device& device); + + bool send(std::string_view message); + +private: + const serial::Device device; +}; + +} // namespace serial_client +} // namespace multicast \ No newline at end of file diff --git a/udp_client.cpp b/udp_client.cpp new file mode 100644 index 0000000..7661e24 --- /dev/null +++ b/udp_client.cpp @@ -0,0 +1,21 @@ +#include +#include "udp_client.hpp" + +namespace multicast +{ +namespace udp_client +{ + +UdpClient::UdpClient(const udp::Socket* socket, const sockaddr_in& address) +{ + this->socket = socket; + this->address = address; +} + +bool UdpClient::send(std::string_view message) +{ + return udp::send(*socket, address, message); +} + +} // namespace udp_client +} // namespace multicast \ No newline at end of file diff --git a/udp_client.hpp b/udp_client.hpp new file mode 100644 index 0000000..d42f9d0 --- /dev/null +++ b/udp_client.hpp @@ -0,0 +1,23 @@ +#include +#include "client.hpp" +#include "udp.hpp" + +namespace multicast +{ +namespace udp_client +{ + +class UdpClient: public client::Client +{ +public: + UdpClient(const udp::Socket* socket, const sockaddr_in& address); + + bool send(std::string_view message); + +private: + const udp::Socket* socket; + const sockaddr_in& address; +}; + +} // namespace udp_client +} // namespace multicast \ No newline at end of file From bd5c1944a0d0a7dcbbd46580a58b36b62b5d4ee9 Mon Sep 17 00:00:00 2001 From: LegendaV <115637903+LegendaV@users.noreply.github.com> Date: Thu, 22 Jan 2026 13:27:06 +0500 Subject: [PATCH 2/6] #pragma once --- client.hpp | 1 + config.hpp | 1 + serial_client.hpp | 1 + udp.hpp | 1 + udp_client.hpp | 1 + 5 files changed, 5 insertions(+) diff --git a/client.hpp b/client.hpp index 0a1b3b7..1ea3509 100644 --- a/client.hpp +++ b/client.hpp @@ -1,3 +1,4 @@ +#pragma once #include namespace multicast diff --git a/config.hpp b/config.hpp index 1e82971..1b1b2cd 100644 --- a/config.hpp +++ b/config.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/serial_client.hpp b/serial_client.hpp index 4658075..020f2c8 100644 --- a/serial_client.hpp +++ b/serial_client.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include "client.hpp" #include "serial.hpp" diff --git a/udp.hpp b/udp.hpp index 54de239..6fd770c 100644 --- a/udp.hpp +++ b/udp.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include diff --git a/udp_client.hpp b/udp_client.hpp index d42f9d0..409b320 100644 --- a/udp_client.hpp +++ b/udp_client.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include "client.hpp" #include "udp.hpp" From 067e23047b4d87c34cf7d348f54aa7285955d455 Mon Sep 17 00:00:00 2001 From: LegendaV <115637903+LegendaV@users.noreply.github.com> Date: Thu, 22 Jan 2026 13:29:24 +0500 Subject: [PATCH 3/6] add utility --- serial.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/serial.cpp b/serial.cpp index 82be2d9..180cf95 100644 --- a/serial.cpp +++ b/serial.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "serial.hpp" From 916621cbb6dff9d59e0b908a7f78bef42cf84c40 Mon Sep 17 00:00:00 2001 From: LegendaV <115637903+LegendaV@users.noreply.github.com> Date: Thu, 22 Jan 2026 13:41:17 +0500 Subject: [PATCH 4/6] fix --- client.cpp | 1 + udp_client.cpp | 4 ++-- udp_client.hpp | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client.cpp b/client.cpp index 7406574..aa2a81e 100644 --- a/client.cpp +++ b/client.cpp @@ -13,6 +13,7 @@ Client::Client() bool Client::send(std::string_view message) { + (bool)message; return true; } diff --git a/udp_client.cpp b/udp_client.cpp index 7661e24..bdc19f5 100644 --- a/udp_client.cpp +++ b/udp_client.cpp @@ -7,13 +7,13 @@ namespace udp_client { UdpClient::UdpClient(const udp::Socket* socket, const sockaddr_in& address) + : socket(socket), address(address) { - this->socket = socket; - this->address = address; } bool UdpClient::send(std::string_view message) { + if (!socket) return false; return udp::send(*socket, address, message); } diff --git a/udp_client.hpp b/udp_client.hpp index 409b320..9ffb358 100644 --- a/udp_client.hpp +++ b/udp_client.hpp @@ -17,7 +17,7 @@ class UdpClient: public client::Client private: const udp::Socket* socket; - const sockaddr_in& address; + sockaddr_in address{}; }; } // namespace udp_client From f5c8d6f0aa1676a7572cf9ee1a16bf4562fc73ab Mon Sep 17 00:00:00 2001 From: LegendaV <115637903+LegendaV@users.noreply.github.com> Date: Thu, 22 Jan 2026 13:54:15 +0500 Subject: [PATCH 5/6] fix --- client.cpp | 2 +- serial_client.cpp | 8 +++----- serial_client.hpp | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/client.cpp b/client.cpp index aa2a81e..c95e148 100644 --- a/client.cpp +++ b/client.cpp @@ -13,7 +13,7 @@ Client::Client() bool Client::send(std::string_view message) { - (bool)message; + (void)message; return true; } diff --git a/serial_client.cpp b/serial_client.cpp index f8c0c2d..29e471b 100644 --- a/serial_client.cpp +++ b/serial_client.cpp @@ -1,16 +1,14 @@ #include #include "serial_client.hpp" +#include namespace multicast { namespace serial_client { -SerialClient::SerialClient(const serial::Device& device) -{ - this->device = std::move(device); -} - +SerialClient::SerialClient(serial::Device& device_) : device(std::move(device_)) +{ } bool SerialClient::send(std::string_view message) { diff --git a/serial_client.hpp b/serial_client.hpp index 020f2c8..1b621de 100644 --- a/serial_client.hpp +++ b/serial_client.hpp @@ -11,7 +11,7 @@ namespace serial_client class SerialClient : public client::Client { public: - SerialClient(const serial::Device& device); + SerialClient(serial::Device& device); bool send(std::string_view message); From 1dd200a3870826c1dbae9a5c3c5f9b235a9683cb Mon Sep 17 00:00:00 2001 From: LegendaV <115637903+LegendaV@users.noreply.github.com> Date: Thu, 22 Jan 2026 13:55:23 +0500 Subject: [PATCH 6/6] fix --- serial_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_client.cpp b/serial_client.cpp index 29e471b..2193c5a 100644 --- a/serial_client.cpp +++ b/serial_client.cpp @@ -7,7 +7,7 @@ namespace multicast namespace serial_client { -SerialClient::SerialClient(serial::Device& device_) : device(std::move(device_)) +SerialClient::SerialClient(serial::Device&& device_) : device(std::move(device_)) { } bool SerialClient::send(std::string_view message)