Skip to content

Commit

Permalink
communicator
Browse files Browse the repository at this point in the history
  • Loading branch information
abushev committed Jul 27, 2024
1 parent ae181b7 commit 0302023
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions rtb/messaging/communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

namespace vanilla { namespace messaging {


using data_segment = std::pair<char const*, size_t>;
using data_segments_vector = std::vector<data_segment>;

template<typename Serializable>
std::string serialize( Serializable && data ) {
std::stringstream ss(std::ios_base::out|std::ios_base::binary);
Expand Down Expand Up @@ -169,7 +171,6 @@ class sender : ConnectionPolicy
{
public:
using data_type = std::array<char, MAX_DATA_SIZE> ;

template<typename ...IPAddress>
sender(boost::asio::io_service& io_service, const unsigned short port, IPAddress && ...addresses) :
socket_{io_service},
Expand All @@ -192,6 +193,17 @@ class sender : ConnectionPolicy
[](const boost::system::error_code&, std::size_t) {
});
}

void send_async(data_segments_vector const & data) {
size_t total_size{};
std::for_each(data.begin(), data.end(), [&total_size](auto &it) { total_size += it.second;});
out_data_.reserve(total_size);
std::for_each(data.begin(), data.end(), [&](auto &it) { out_data_.append(it.first, it.second);});
socket_.async_send_to(
boost::asio::buffer(out_data_), to_endpoint_,
[](const boost::system::error_code&, std::size_t) {}
);
}

template<typename Handler>
void receive_async(Handler handler) {
Expand Down Expand Up @@ -273,6 +285,13 @@ class communicator {
}
return *this;
}

self_type & distribute_segments(data_segments_vector const & data) {
if(distributor_) {
distributor_->send_async(data);
}
return *this;
}

template<typename T, typename Handler>
self_type & process(Handler handler) {
Expand Down

0 comments on commit 0302023

Please sign in to comment.