Commit 0900b3d1 authored by Joseph Noir's avatar Joseph Noir

Adapt dgram handling to one BASP message per dgram

parent 30cd1164
......@@ -120,7 +120,7 @@ connection_state instance::handle(execution_unit* ctx,
bool instance::handle(execution_unit* ctx, new_datagram_msg& dm, header& hdr) {
using itr_t = std::vector<char>::iterator;
// Call in case of an error
// function object providing cleanup code on errors
auto err = [&]() -> bool {
auto cb = make_callback([&](const node_id& nid) -> error {
callee_.purge_state(nid);
......@@ -129,43 +129,31 @@ bool instance::handle(execution_unit* ctx, new_datagram_msg& dm, header& hdr) {
tbl_.erase(dm.handle, cb);
return false;
};
// Split message into hdr and payload
// Loop until all messages in the datagram have been processed.
auto dgram_itr = dm.buf.begin();
do {
// extract payload
std::vector<char> pl_buf{std::move_iterator<itr_t>(std::begin(dm.buf) +
basp::header_size),
std::move_iterator<itr_t>(std::end(dm.buf))};
// resize header
dm.buf.resize(basp::header_size);
// extract header
std::vector<char> hdr_buf{std::move_iterator<itr_t>(dgram_itr),
std::move_iterator<itr_t>(dgram_itr +
basp::header_size)};
dgram_itr += basp::header_size;
// deserialize header
binary_deserializer bd{ctx, hdr_buf};
binary_deserializer bd{ctx, dm.buf};
auto e = bd(hdr);
if (e || !valid(hdr)) {
CAF_LOG_WARNING("received invalid header:" << CAF_ARG(hdr));
std::cerr << "[I] Received invalid header of type: "
<< to_string(hdr.operation) << std::endl;
std::cerr << "Received invalid header!" << std::endl;
return err();
}
CAF_LOG_DEBUG(CAF_ARG(hdr));
// extract payload
std::vector<char> pl_buf{std::move_iterator<itr_t>(dgram_itr),
std::move_iterator<itr_t>(dgram_itr +
hdr.payload_len)};
dgram_itr += hdr.payload_len;
std::vector<char>* payload = nullptr;
if (hdr.payload_len > 0) {
payload = &pl_buf;
}
// needs forwarding?
if (!is_handshake(hdr) && !is_heartbeat(hdr) && hdr.dest_node != this_node_) {
std::cerr << "[I] Needs forwarding?" << std::endl;
// TODO: anything to do here? (i.e., do we still need forwarding?)
if (payload->size() != hdr.payload_len) {
CAF_LOG_WARNING("received invalid payload");
return err();
}
}
if (!handle_msg(ctx, dm.handle, hdr, payload, false, none))
return err();
} while (dgram_itr != dm.buf.end());
return true;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment