Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

communicator #103

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UDP datagram should not be larger than the MTU (1500 bytes), ASIO wouldn't care and return EMSGSIZE I think.

man 7 udp

       By default, Linux UDP does path MTU (Maximum Transmission Unit)  discovery.
       This  means  the  kernel will keep track of the MTU to a specific target IP
       address and return EMSGSIZE when a UDP packet write exceeds it.  When  this
       happens, the application should decrease the packet size.  Path MTU discov-
       ery can be also turned off using the IP_MTU_DISCOVER socket option  or  the
       /proc/sys/net/ipv4/ip_no_pmtu_disc  file;  see  ip(7)  for  details.   When
       turned off, UDP will fragment outgoing UDP packets that exceed  the  inter-
       face MTU.  However, disabling it is not recommended for performance and re-
       liability reasons.

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