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, ...@@ -120,7 +120,7 @@ connection_state instance::handle(execution_unit* ctx,
bool instance::handle(execution_unit* ctx, new_datagram_msg& dm, header& hdr) { bool instance::handle(execution_unit* ctx, new_datagram_msg& dm, header& hdr) {
using itr_t = std::vector<char>::iterator; using itr_t = std::vector<char>::iterator;
// Call in case of an error // function object providing cleanup code on errors
auto err = [&]() -> bool { auto err = [&]() -> bool {
auto cb = make_callback([&](const node_id& nid) -> error { auto cb = make_callback([&](const node_id& nid) -> error {
callee_.purge_state(nid); callee_.purge_state(nid);
...@@ -129,43 +129,31 @@ bool instance::handle(execution_unit* ctx, new_datagram_msg& dm, header& hdr) { ...@@ -129,43 +129,31 @@ bool instance::handle(execution_unit* ctx, new_datagram_msg& dm, header& hdr) {
tbl_.erase(dm.handle, cb); tbl_.erase(dm.handle, cb);
return false; return false;
}; };
// Split message into hdr and payload // extract payload
// Loop until all messages in the datagram have been processed. std::vector<char> pl_buf{std::move_iterator<itr_t>(std::begin(dm.buf) +
auto dgram_itr = dm.buf.begin(); basp::header_size),
do { std::move_iterator<itr_t>(std::end(dm.buf))};
// resize header
dm.buf.resize(basp::header_size);
// extract header // extract header
std::vector<char> hdr_buf{std::move_iterator<itr_t>(dgram_itr), binary_deserializer bd{ctx, dm.buf};
std::move_iterator<itr_t>(dgram_itr +
basp::header_size)};
dgram_itr += basp::header_size;
// deserialize header
binary_deserializer bd{ctx, hdr_buf};
auto e = bd(hdr); auto e = bd(hdr);
if (e || !valid(hdr)) { if (e || !valid(hdr)) {
CAF_LOG_WARNING("received invalid header:" << CAF_ARG(hdr)); CAF_LOG_WARNING("received invalid header:" << CAF_ARG(hdr));
std::cerr << "[I] Received invalid header of type: " std::cerr << "Received invalid header!" << std::endl;
<< to_string(hdr.operation) << std::endl;
return err(); return err();
} }
CAF_LOG_DEBUG(CAF_ARG(hdr)); 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; std::vector<char>* payload = nullptr;
if (hdr.payload_len > 0) { if (hdr.payload_len > 0) {
payload = &pl_buf; payload = &pl_buf;
} if (payload->size() != hdr.payload_len) {
// needs forwarding? CAF_LOG_WARNING("received invalid payload");
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?)
return err(); return err();
} }
}
if (!handle_msg(ctx, dm.handle, hdr, payload, false, none)) if (!handle_msg(ctx, dm.handle, hdr, payload, false, none))
return err(); return err();
} while (dgram_itr != dm.buf.end());
return true; 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