forked from sixonionpotatoes/commons
add send() function to client
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "boost/bind.hpp"
|
#include "boost/bind.hpp"
|
||||||
|
#include "protocol.hpp"
|
||||||
|
|
||||||
namespace commons {
|
namespace commons {
|
||||||
|
|
||||||
@@ -16,15 +17,14 @@ class client {
|
|||||||
const boost::asio::ip::address& multicast_address,
|
const boost::asio::ip::address& multicast_address,
|
||||||
short multicast_port, unsigned short tcp_port);
|
short multicast_port, unsigned short tcp_port);
|
||||||
|
|
||||||
void init();
|
template <typename BufferType, typename HandlerFunc>
|
||||||
void set_outbound_interface(const boost::asio::ip::address_v4& address);
|
void send(const BufferType& buffer, HandlerFunc handler) {
|
||||||
void handle_send_to(const boost::system::error_code& error);
|
socket_.async_send_to(buffer, endpoint_, handler);
|
||||||
void handle_timeout(const boost::system::error_code& error);
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::asio::ip::udp::endpoint endpoint_;
|
boost::asio::ip::udp::endpoint endpoint_;
|
||||||
boost::asio::ip::udp::socket socket_;
|
boost::asio::ip::udp::socket socket_;
|
||||||
boost::asio::deadline_timer timer_;
|
|
||||||
std::string message_;
|
std::string message_;
|
||||||
unsigned short port_;
|
unsigned short port_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -46,6 +46,14 @@ class generic_message_base
|
|||||||
spdlog::debug("create_message is not implemented yet");
|
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:
|
protected:
|
||||||
bool is_handler = true;
|
bool is_handler = true;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,50 +12,6 @@ client::client(boost::asio::io_service& io_service,
|
|||||||
short multicast_port, unsigned short tcp_port)
|
short multicast_port, unsigned short tcp_port)
|
||||||
: endpoint_(multicast_address, multicast_port),
|
: endpoint_(multicast_address, multicast_port),
|
||||||
socket_(io_service, endpoint_.protocol()),
|
socket_(io_service, endpoint_.protocol()),
|
||||||
timer_(io_service),
|
|
||||||
port_(tcp_port) {}
|
port_(tcp_port) {}
|
||||||
|
|
||||||
void client::init() {
|
|
||||||
spdlog::debug("Send test msg");
|
|
||||||
|
|
||||||
auto msg = std::make_unique<commons::protocol::draw_rectangle>();
|
|
||||||
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<commons::protocol::draw_pixel>();
|
|
||||||
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
|
} // namespace commons
|
||||||
|
|||||||
13
src/main.cpp
13
src/main.cpp
@@ -16,7 +16,18 @@ void run_client() {
|
|||||||
spdlog::info("Running client...");
|
spdlog::info("Running client...");
|
||||||
commons::client s(io_context, boost::asio::ip::make_address("0.0.0.0"), 9000,
|
commons::client s(io_context, boost::asio::ip::make_address("0.0.0.0"), 9000,
|
||||||
0);
|
0);
|
||||||
s.init();
|
|
||||||
|
for (int i = 0; i < 100; ++i) {
|
||||||
|
auto msg = std::make_shared<commons::protocol::draw_pixel>();
|
||||||
|
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();
|
io_context.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user