/// # Panics /// /// If the `BufferedStream` is initialized more than once, it will panic. pubfn init(&mutself, stream_id: StreamId) {
debug_assert_eq!(self, &Self::Uninitialized);
*self = Self::Initialized {
stream_id,
buf: Vec::new(),
};
}
pubfn encode_with<F: FnOnce(&mut Encoder<&>mut Vec<u8>>)>(&mutself, f: F) { ifletSelf::Initialized { buf, .. } = self {
f(&mut Encoder::new_borrowed_vec(buf));
} else {
debug_assert!(false, "Do not encode data before the stream is initialized");
}
}
/// # Panics /// /// This function cannot be called before the `BufferedStream` is initialized. pubfn buffer(&mutself, to_buf: &[u8]) { ifletSelf::Initialized { buf, .. } = self {
buf.extend_from_slice(to_buf);
} else {
debug_assert!(false, "Do not buffer data before the stream is initialized");
}
}
/// # Errors /// /// Returns `neqo_transport` errors. pubfn send_buffer(&mutself, conn: &mut Connection, now: Instant) -> Res<usize> { letSelf::Initialized { stream_id, buf } = selfelse { return Ok(0);
}; if buf.is_empty() { return Ok(0);
} let sent = conn.stream_send(*stream_id, &buf[..])?; if sent == 0 { return Ok(0);
} elseif sent == buf.len() {
buf.clear();
} else { let b = buf.split_off(sent);
*buf = b;
}
qlog::h3_data_moved_down(conn.qlog_mut(), *stream_id, sent, now);
Ok(sent)
}
/// Flush the buffer and return the stream ID and buffer if ready to send atomically. fn prepare_atomic_send(
&mutself,
conn: &mut Connection,
now: Instant,
) -> Res<Option<(StreamId, &mut Vec<u8>)>> { self.send_buffer(conn, now)?; letSelf::Initialized { stream_id, buf } = selfelse { return Ok(None);
}; if !buf.is_empty() { return Ok(None);
}
Ok(Some((*stream_id, buf)))
}
/// # Errors /// /// Returns `neqo_transport` errors. pubfn send_atomic(
&mutself,
conn: &mut Connection,
to_send: &[u8],
now: Instant,
) -> Res<bool> { let Some((stream_id, _)) = self.prepare_atomic_send(conn, now)? else { return Ok(false);
}; let sent = conn.stream_send_atomic(stream_id, to_send)?; if sent {
qlog::h3_data_moved_down(conn.qlog_mut(), stream_id, to_send.len(), now);
}
Ok(sent)
}
/// Encode data using the provided closure and send it atomically. /// /// This avoids allocating a temporary encoder at the call site by reusing /// the stream's internal buffer as scratch space. /// /// # Errors /// /// Returns `neqo_transport` errors. pubfn send_atomic_with<F: FnOnce(&mut Encoder<&mut Vec<u8>>)>(
&mutself,
conn: &mut Connection,
f: F,
now: Instant,
) -> Res<bool> { let Some((stream_id, buf)) = self.prepare_atomic_send(conn, now)? else { return Ok(false);
};
f(&mut Encoder::new_borrowed_vec(buf)); let len = buf.len(); let res = conn.stream_send_atomic(stream_id, buf);
buf.clear(); let sent = res?; if sent {
qlog::h3_data_moved_down(conn.qlog_mut(), stream_id, len, now);
}
Ok(sent)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.