Fragments::Fragments(IMC::Message* msg, int mtu) { m_uid = s_uid++; m_num_frags = 0; int frag_size = mtu - sizeof(IMC::Header) - 5; if (frag_size <= 0) { DUNE_ERR("Fragments", "MTU is too small"); return; } Utils::ByteBuffer buff; int size = IMC::Packet::serialize(msg, buff); uint8_t* buffer = buff.getBuffer(); int part = 0, pos = 0; m_num_frags = (int)std::ceil((float)size / (float)mtu); while (pos < size) { int remaining = size - pos; int cur_size = std::min(remaining, mtu); IMC::MessagePart* mpart = new IMC::MessagePart(); mpart->frag_number = part++; mpart->num_frags = m_num_frags; mpart->uid = m_uid; mpart->data.assign(buffer + pos, buffer + pos + cur_size); pos += cur_size; m_fragments.push_back(mpart); } }
uint16_t Packet::serialize(const Message* msg, Utils::ByteBuffer& bfr) { unsigned size = msg->getSerializationSize(); if (size > 65535) throw InvalidMessageSize(size); bfr.setSize(size); return serialize(msg, bfr.getBuffer(), size); }