From 06b00843d20b0173a51aa6855ebfebd9f9390725 Mon Sep 17 00:00:00 2001 From: kalipso Date: Tue, 26 Apr 2022 19:31:09 +0200 Subject: [PATCH] add send() function to client --- include/client.hpp | 10 +++++----- include/protocol.hpp | 8 ++++++++ src/client.cpp | 44 -------------------------------------------- src/main.cpp | 13 ++++++++++++- 4 files changed, 25 insertions(+), 50 deletions(-) diff --git a/include/client.hpp b/include/client.hpp index 776a672..b537503 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -7,6 +7,7 @@ #include #include "boost/bind.hpp" +#include "protocol.hpp" namespace commons { @@ -16,15 +17,14 @@ class client { const boost::asio::ip::address& multicast_address, short multicast_port, unsigned short tcp_port); - void init(); - void set_outbound_interface(const boost::asio::ip::address_v4& address); - void handle_send_to(const boost::system::error_code& error); - void handle_timeout(const boost::system::error_code& error); + template + void send(const BufferType& buffer, HandlerFunc handler) { + socket_.async_send_to(buffer, endpoint_, handler); + } private: boost::asio::ip::udp::endpoint endpoint_; boost::asio::ip::udp::socket socket_; - boost::asio::deadline_timer timer_; std::string message_; unsigned short port_; }; diff --git a/include/protocol.hpp b/include/protocol.hpp index 00ab4ee..e686595 100644 --- a/include/protocol.hpp +++ b/include/protocol.hpp @@ -46,6 +46,14 @@ class generic_message_base spdlog::debug("create_message is not implemented yet"); } + virtual void handle_sent(const boost::system::error_code& er) { + if (!er) { + spdlog::debug("Message was sent"); + } else { + spdlog::error("Error occured on client::send"); + } + } + protected: bool is_handler = true; }; diff --git a/src/client.cpp b/src/client.cpp index b1c1e12..995e13e 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -12,50 +12,6 @@ client::client(boost::asio::io_service& io_service, short multicast_port, unsigned short tcp_port) : endpoint_(multicast_address, multicast_port), socket_(io_service, endpoint_.protocol()), - timer_(io_service), port_(tcp_port) {} -void client::init() { - spdlog::debug("Send test msg"); - - auto msg = std::make_unique(); - msg->position = commons::protocol::vec2{3, 2}; - - socket_.async_send_to( - msg->serialize(), endpoint_, - std::bind(&client::handle_send_to, this, std::placeholders::_1)); - - auto msg2 = std::make_unique(); - msg2->color = commons::protocol::vec3{255, 0, 0}; - - socket_.async_send_to( - msg2->serialize(), endpoint_, - std::bind(&client::handle_send_to, this, std::placeholders::_1)); -} - -void client::set_outbound_interface( - const boost::asio::ip::address_v4& address) { - boost::asio::ip::multicast::outbound_interface option(address); - socket_.set_option(option); -} - -void client::handle_send_to(const boost::system::error_code& error) { - if (!error) { - timer_.expires_from_now(boost::posix_time::seconds(1)); - timer_.async_wait( - std::bind(&client::handle_timeout, this, std::placeholders::_1)); - } -} - -void client::handle_timeout(const boost::system::error_code& error) { - if (!error) { - // spdlog::trace("Resending Message: '{}'", "TestMessage"); - spdlog::debug("done"); - // socket_.async_send_to( - // boost::asio::buffer("TestMessage"), endpoint_, - // std::bind(&client::handle_send_to, this, - // std::placeholders::_1)); - } -} - } // namespace commons diff --git a/src/main.cpp b/src/main.cpp index 28a93bb..a88ff5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,18 @@ void run_client() { spdlog::info("Running client..."); commons::client s(io_context, boost::asio::ip::make_address("0.0.0.0"), 9000, 0); - s.init(); + + for (int i = 0; i < 100; ++i) { + auto msg = std::make_shared(); + msg->color = commons::protocol::vec3{i, i, i}; + + // used as lifetime expansion so that msg doesnt go out of scope till it + // actually was sent + s.send(msg->serialize(), + std::bind(&commons::protocol::generic_message_base::handle_sent, msg, + std::placeholders::_1)); + } + io_context.run(); }