1
0

fix packet fragment size calculating

This commit is contained in:
EAimTY 2023-05-30 20:44:05 +09:00
parent d6e4e7eeee
commit c92ed4b810

View File

@ -199,9 +199,14 @@ where
P: AsRef<[u8]> + 'a, P: AsRef<[u8]> + 'a,
{ {
fn new(assoc_id: u16, pkt_id: u16, addr: Address, max_pkt_size: usize, payload: P) -> Self { fn new(assoc_id: u16, pkt_id: u16, addr: Address, max_pkt_size: usize, payload: P) -> Self {
let first_frag_size = max_pkt_size - PacketHeader::len_without_addr() - addr.len(); let header_addr_ref = Header::Packet(PacketHeader::new(0, 0, 0, 0, 0, addr));
let frag_size_addr_none = let header_addr_none_ref = Header::Packet(PacketHeader::new(0, 0, 0, 0, 0, Address::None));
max_pkt_size - PacketHeader::len_without_addr() - Address::None.len();
let first_frag_size = max_pkt_size - header_addr_ref.len();
let frag_size_addr_none = max_pkt_size - header_addr_none_ref.len();
let Header::Packet(pkt) = header_addr_ref else { unreachable!() };
let (_, _, _, _, _, addr) = pkt.into();
let frag_total = if first_frag_size < payload.as_ref().len() { let frag_total = if first_frag_size < payload.as_ref().len() {
(1 + (payload.as_ref().len() - first_frag_size) / frag_size_addr_none + 1) as u8 (1 + (payload.as_ref().len() - first_frag_size) / frag_size_addr_none + 1) as u8
@ -231,18 +236,22 @@ where
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.next_frag_id < self.frag_total { if self.next_frag_id < self.frag_total {
let payload_size = let header_ref = Header::Packet(PacketHeader::new(0, 0, 0, 0, 0, self.addr.take()));
self.max_pkt_size - PacketHeader::len_without_addr() - self.addr.len();
let payload_size = self.max_pkt_size - header_ref.len();
let next_frag_end = let next_frag_end =
(self.next_frag_start + payload_size).min(self.payload.as_ref().len()); (self.next_frag_start + payload_size).min(self.payload.as_ref().len());
let Header::Packet(pkt) = header_ref else { unreachable!() };
let (_, _, _, _, _, addr) = pkt.into();
let header = Header::Packet(PacketHeader::new( let header = Header::Packet(PacketHeader::new(
self.assoc_id, self.assoc_id,
self.pkt_id, self.pkt_id,
self.frag_total, self.frag_total,
self.next_frag_id, self.next_frag_id,
(next_frag_end - self.next_frag_start) as u16, (next_frag_end - self.next_frag_start) as u16,
self.addr.take(), addr,
)); ));
let payload_ptr = &(self.payload.as_ref()[self.next_frag_start]) as *const u8; let payload_ptr = &(self.payload.as_ref()[self.next_frag_start]) as *const u8;