2022-07-21 15:34:53 +00:00
|
|
|
use std::collections::VecDeque;
|
2022-06-05 13:33:43 +00:00
|
|
|
use std::pin::Pin;
|
2020-12-02 12:30:47 +00:00
|
|
|
use std::sync::Arc;
|
2022-06-05 13:33:43 +00:00
|
|
|
use std::task::{Context, Poll};
|
2020-12-02 12:30:47 +00:00
|
|
|
|
2022-07-21 15:34:53 +00:00
|
|
|
use async_trait::async_trait;
|
2022-06-20 21:40:31 +00:00
|
|
|
use log::trace;
|
2020-12-02 12:30:47 +00:00
|
|
|
|
2022-07-21 15:34:53 +00:00
|
|
|
use futures::AsyncWriteExt;
|
|
|
|
use futures::Stream;
|
2021-10-25 07:27:57 +00:00
|
|
|
use kuska_handshake::async_std::BoxStreamWrite;
|
2020-12-07 12:35:24 +00:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
2020-12-02 12:30:47 +00:00
|
|
|
use crate::error::*;
|
2022-07-21 15:34:53 +00:00
|
|
|
use crate::message::*;
|
|
|
|
use crate::util::{ByteStream, Packet};
|
2020-12-02 12:30:47 +00:00
|
|
|
|
2021-10-12 16:13:07 +00:00
|
|
|
// Messages are sent by chunks
|
|
|
|
// Chunk format:
|
|
|
|
// - u32 BE: request id (same for request and response)
|
|
|
|
// - u16 BE: chunk length, possibly with CHUNK_HAS_CONTINUATION flag
|
|
|
|
// when this is not the last chunk of the message
|
|
|
|
// - [u8; chunk_length] chunk data
|
|
|
|
|
2021-10-12 15:59:46 +00:00
|
|
|
pub(crate) type RequestID = u32;
|
2022-07-21 15:34:53 +00:00
|
|
|
pub(crate) type ChunkLength = u16;
|
|
|
|
pub(crate) const MAX_CHUNK_LENGTH: ChunkLength = 0x3FF0;
|
|
|
|
pub(crate) const ERROR_MARKER: ChunkLength = 0x4000;
|
|
|
|
pub(crate) const CHUNK_HAS_CONTINUATION: ChunkLength = 0x8000;
|
2020-12-02 12:30:47 +00:00
|
|
|
|
|
|
|
struct SendQueueItem {
|
|
|
|
id: RequestID,
|
|
|
|
prio: RequestPriority,
|
2022-06-05 13:33:43 +00:00
|
|
|
data: DataReader,
|
|
|
|
}
|
|
|
|
|
2022-06-20 21:40:31 +00:00
|
|
|
#[pin_project::pin_project]
|
|
|
|
struct DataReader {
|
|
|
|
#[pin]
|
2022-07-21 15:34:53 +00:00
|
|
|
reader: ByteStream,
|
2022-07-18 13:21:13 +00:00
|
|
|
packet: Packet,
|
2022-06-20 21:40:31 +00:00
|
|
|
pos: usize,
|
|
|
|
buf: Vec<u8>,
|
|
|
|
eos: bool,
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 15:34:53 +00:00
|
|
|
impl From<ByteStream> for DataReader {
|
|
|
|
fn from(data: ByteStream) -> DataReader {
|
2022-06-20 21:40:31 +00:00
|
|
|
DataReader {
|
|
|
|
reader: data,
|
|
|
|
packet: Ok(Vec::new()),
|
|
|
|
pos: 0,
|
|
|
|
buf: Vec::with_capacity(MAX_CHUNK_LENGTH as usize),
|
|
|
|
eos: false,
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 16:42:27 +00:00
|
|
|
enum DataFrame {
|
|
|
|
Data {
|
|
|
|
/// a fixed size buffer containing some data, possibly padded with 0s
|
|
|
|
data: [u8; MAX_CHUNK_LENGTH as usize],
|
|
|
|
/// actual lenght of data
|
|
|
|
len: usize,
|
|
|
|
},
|
|
|
|
Error(u8),
|
|
|
|
}
|
|
|
|
|
2022-06-19 15:44:07 +00:00
|
|
|
struct DataReaderItem {
|
2022-06-19 16:42:27 +00:00
|
|
|
data: DataFrame,
|
2022-06-19 15:44:07 +00:00
|
|
|
/// whethere there may be more data comming from this stream. Can be used for some
|
|
|
|
/// optimization. It's an error to set it to false if there is more data, but it is correct
|
|
|
|
/// (albeit sub-optimal) to set it to true if there is nothing coming after
|
|
|
|
may_have_more: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DataReaderItem {
|
|
|
|
fn empty_last() -> Self {
|
|
|
|
DataReaderItem {
|
2022-06-19 16:42:27 +00:00
|
|
|
data: DataFrame::Data {
|
|
|
|
data: [0; MAX_CHUNK_LENGTH as usize],
|
|
|
|
len: 0,
|
|
|
|
},
|
2022-06-19 15:44:07 +00:00
|
|
|
may_have_more: false,
|
|
|
|
}
|
|
|
|
}
|
2022-06-19 16:42:27 +00:00
|
|
|
|
|
|
|
fn header(&self) -> [u8; 2] {
|
|
|
|
let continuation = if self.may_have_more {
|
|
|
|
CHUNK_HAS_CONTINUATION
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let len = match self.data {
|
|
|
|
DataFrame::Data { len, .. } => len as u16,
|
|
|
|
DataFrame::Error(e) => e as u16 | ERROR_MARKER,
|
|
|
|
};
|
|
|
|
|
|
|
|
ChunkLength::to_be_bytes(len | continuation)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data(&self) -> &[u8] {
|
|
|
|
match self.data {
|
|
|
|
DataFrame::Data { ref data, len } => &data[..len],
|
|
|
|
DataFrame::Error(_) => &[],
|
|
|
|
}
|
|
|
|
}
|
2022-06-19 15:44:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 13:33:43 +00:00
|
|
|
impl Stream for DataReader {
|
2022-06-19 15:44:07 +00:00
|
|
|
type Item = DataReaderItem;
|
2022-06-05 13:33:43 +00:00
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
2022-06-20 21:40:31 +00:00
|
|
|
let mut this = self.project();
|
|
|
|
|
|
|
|
if *this.eos {
|
|
|
|
// eos was reached at previous call to poll_next, where a partial packet
|
|
|
|
// was returned. Now return None
|
|
|
|
return Poll::Ready(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let packet = match this.packet {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
let e = *e;
|
|
|
|
*this.packet = Ok(Vec::new());
|
|
|
|
return Poll::Ready(Some(DataReaderItem {
|
|
|
|
data: DataFrame::Error(e),
|
|
|
|
may_have_more: true,
|
|
|
|
}));
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
2022-06-20 21:40:31 +00:00
|
|
|
};
|
|
|
|
let packet_left = packet.len() - *this.pos;
|
|
|
|
let buf_left = MAX_CHUNK_LENGTH as usize - this.buf.len();
|
|
|
|
let to_read = std::cmp::min(buf_left, packet_left);
|
|
|
|
this.buf
|
|
|
|
.extend_from_slice(&packet[*this.pos..*this.pos + to_read]);
|
|
|
|
*this.pos += to_read;
|
|
|
|
if this.buf.len() == MAX_CHUNK_LENGTH as usize {
|
|
|
|
// we have a full buf, ready to send
|
|
|
|
break;
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
2022-06-05 14:47:29 +00:00
|
|
|
|
2022-06-20 21:40:31 +00:00
|
|
|
// we don't have a full buf, packet is empty; try receive more
|
|
|
|
if let Some(p) = futures::ready!(this.reader.as_mut().poll_next(cx)) {
|
|
|
|
*this.packet = p;
|
|
|
|
*this.pos = 0;
|
|
|
|
// if buf is empty, we will loop and return the error directly. If buf
|
|
|
|
// isn't empty, send it before by breaking.
|
|
|
|
if this.packet.is_err() && !this.buf.is_empty() {
|
|
|
|
break;
|
2022-06-05 14:47:29 +00:00
|
|
|
}
|
2022-06-20 21:40:31 +00:00
|
|
|
} else {
|
|
|
|
*this.eos = true;
|
|
|
|
break;
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 21:40:31 +00:00
|
|
|
|
|
|
|
let mut body = [0; MAX_CHUNK_LENGTH as usize];
|
|
|
|
let len = this.buf.len();
|
|
|
|
body[..len].copy_from_slice(this.buf);
|
|
|
|
this.buf.clear();
|
|
|
|
Poll::Ready(Some(DataReaderItem {
|
|
|
|
data: DataFrame::Data { data: body, len },
|
|
|
|
may_have_more: !*this.eos,
|
|
|
|
}))
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SendQueue {
|
2021-10-13 15:12:13 +00:00
|
|
|
items: VecDeque<(u8, VecDeque<SendQueueItem>)>,
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SendQueue {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
2021-10-13 15:12:13 +00:00
|
|
|
items: VecDeque::with_capacity(64),
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn push(&mut self, item: SendQueueItem) {
|
|
|
|
let prio = item.prio;
|
2021-10-13 15:12:13 +00:00
|
|
|
let pos_prio = match self.items.binary_search_by(|(p, _)| p.cmp(&prio)) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(i) => {
|
|
|
|
self.items.insert(i, (prio, VecDeque::new()));
|
|
|
|
i
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.items[pos_prio].1.push_back(item);
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
2022-06-19 16:42:27 +00:00
|
|
|
// used only in tests. They should probably be rewriten
|
|
|
|
#[allow(dead_code)]
|
2020-12-02 12:30:47 +00:00
|
|
|
fn pop(&mut self) -> Option<SendQueueItem> {
|
2021-10-13 15:12:13 +00:00
|
|
|
match self.items.pop_front() {
|
2020-12-02 12:30:47 +00:00
|
|
|
None => None,
|
|
|
|
Some((prio, mut items_at_prio)) => {
|
|
|
|
let ret = items_at_prio.pop_front();
|
|
|
|
if !items_at_prio.is_empty() {
|
2021-10-13 15:12:13 +00:00
|
|
|
self.items.push_front((prio, items_at_prio));
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
2020-12-07 15:00:12 +00:00
|
|
|
ret.or_else(|| self.pop())
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 12:35:24 +00:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.items.iter().all(|(_k, v)| v.is_empty())
|
|
|
|
}
|
2022-06-19 15:44:07 +00:00
|
|
|
|
|
|
|
// this is like an async fn, but hand implemented
|
|
|
|
fn next_ready(&mut self) -> SendQueuePollNextReady<'_> {
|
|
|
|
SendQueuePollNextReady { queue: self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SendQueuePollNextReady<'a> {
|
|
|
|
queue: &'a mut SendQueue,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> futures::Future for SendQueuePollNextReady<'a> {
|
|
|
|
type Output = (RequestID, DataReaderItem);
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
for i in 0..self.queue.items.len() {
|
|
|
|
let (_prio, items_at_prio) = &mut self.queue.items[i];
|
|
|
|
|
|
|
|
for _ in 0..items_at_prio.len() {
|
|
|
|
let mut item = items_at_prio.pop_front().unwrap();
|
|
|
|
|
|
|
|
match Pin::new(&mut item.data).poll_next(ctx) {
|
|
|
|
Poll::Pending => items_at_prio.push_back(item),
|
|
|
|
Poll::Ready(Some(data)) => {
|
|
|
|
let id = item.id;
|
|
|
|
if data.may_have_more {
|
|
|
|
self.queue.push(item);
|
|
|
|
} else {
|
|
|
|
if items_at_prio.is_empty() {
|
|
|
|
// this priority level is empty, remove it
|
|
|
|
self.queue.items.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Poll::Ready((id, data));
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
if items_at_prio.is_empty() {
|
|
|
|
// this priority level is empty, remove it
|
|
|
|
self.queue.items.remove(i);
|
|
|
|
}
|
|
|
|
return Poll::Ready((item.id, DataReaderItem::empty_last()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO what do we do if self.queue is empty? We won't get scheduled again.
|
|
|
|
Poll::Pending
|
|
|
|
}
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 11:01:04 +00:00
|
|
|
/// The SendLoop trait, which is implemented both by the client and the server
|
|
|
|
/// connection objects (ServerConna and ClientConn) adds a method `.send_loop()`
|
|
|
|
/// that takes a channel of messages to send and an asynchronous writer,
|
|
|
|
/// and sends messages from the channel to the async writer, putting them in a queue
|
|
|
|
/// before being sent and doing the round-robin sending strategy.
|
|
|
|
///
|
|
|
|
/// The `.send_loop()` exits when the sending end of the channel is closed,
|
|
|
|
/// or if there is an error at any time writing to the async writer.
|
2020-12-02 12:30:47 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub(crate) trait SendLoop: Sync {
|
2020-12-07 17:07:55 +00:00
|
|
|
async fn send_loop<W>(
|
2020-12-02 12:30:47 +00:00
|
|
|
self: Arc<Self>,
|
2022-07-21 15:34:53 +00:00
|
|
|
mut msg_recv: mpsc::UnboundedReceiver<(RequestID, RequestPriority, ByteStream)>,
|
2021-10-25 07:27:57 +00:00
|
|
|
mut write: BoxStreamWrite<W>,
|
2020-12-07 17:07:55 +00:00
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
2021-10-12 12:51:28 +00:00
|
|
|
W: AsyncWriteExt + Unpin + Send + Sync,
|
2020-12-07 17:07:55 +00:00
|
|
|
{
|
2020-12-02 12:30:47 +00:00
|
|
|
let mut sending = SendQueue::new();
|
2020-12-07 12:35:24 +00:00
|
|
|
let mut should_exit = false;
|
|
|
|
while !should_exit || !sending.is_empty() {
|
2022-06-19 15:44:07 +00:00
|
|
|
let recv_fut = msg_recv.recv();
|
|
|
|
futures::pin_mut!(recv_fut);
|
|
|
|
let send_fut = sending.next_ready();
|
|
|
|
|
|
|
|
// recv_fut is cancellation-safe according to tokio doc,
|
|
|
|
// send_fut is cancellation-safe as implemented above?
|
|
|
|
use futures::future::Either;
|
|
|
|
match futures::future::select(recv_fut, send_fut).await {
|
|
|
|
Either::Left((sth, _send_fut)) => {
|
|
|
|
if let Some((id, prio, data)) = sth {
|
|
|
|
sending.push(SendQueueItem {
|
|
|
|
id,
|
|
|
|
prio,
|
|
|
|
data: data.into(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
should_exit = true;
|
|
|
|
};
|
2022-06-05 13:33:43 +00:00
|
|
|
}
|
2022-06-19 15:44:07 +00:00
|
|
|
Either::Right(((id, data), _recv_fut)) => {
|
|
|
|
trace!("send_loop: sending bytes for {}", id);
|
2020-12-02 12:30:47 +00:00
|
|
|
|
2022-06-19 15:44:07 +00:00
|
|
|
let header_id = RequestID::to_be_bytes(id);
|
|
|
|
write.write_all(&header_id[..]).await?;
|
2022-06-05 13:33:43 +00:00
|
|
|
|
2022-06-19 16:42:27 +00:00
|
|
|
write.write_all(&data.header()).await?;
|
|
|
|
write.write_all(data.data()).await?;
|
2022-06-19 15:44:07 +00:00
|
|
|
write.flush().await?;
|
2020-12-07 12:35:24 +00:00
|
|
|
}
|
2020-12-02 12:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 12:45:41 +00:00
|
|
|
|
2021-10-25 11:58:42 +00:00
|
|
|
let _ = write.goodbye().await;
|
2020-12-02 12:30:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:32:47 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
2022-06-20 21:40:31 +00:00
|
|
|
fn empty_data() -> DataReader {
|
2022-07-18 13:21:13 +00:00
|
|
|
type Item = Packet;
|
2022-06-20 21:40:31 +00:00
|
|
|
let stream: Pin<Box<dyn futures::Stream<Item = Item> + Send + 'static>> =
|
2022-07-18 13:21:13 +00:00
|
|
|
Box::pin(futures::stream::empty::<Packet>());
|
2022-06-20 21:40:31 +00:00
|
|
|
stream.into()
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:32:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_priority_queue() {
|
|
|
|
let i1 = SendQueueItem {
|
|
|
|
id: 1,
|
|
|
|
prio: PRIO_NORMAL,
|
2022-06-20 21:40:31 +00:00
|
|
|
data: empty_data(),
|
2021-10-20 14:32:47 +00:00
|
|
|
};
|
|
|
|
let i2 = SendQueueItem {
|
|
|
|
id: 2,
|
|
|
|
prio: PRIO_HIGH,
|
2022-06-20 21:40:31 +00:00
|
|
|
data: empty_data(),
|
2021-10-20 14:32:47 +00:00
|
|
|
};
|
|
|
|
let i2bis = SendQueueItem {
|
|
|
|
id: 20,
|
|
|
|
prio: PRIO_HIGH,
|
2022-06-20 21:40:31 +00:00
|
|
|
data: empty_data(),
|
2021-10-20 14:32:47 +00:00
|
|
|
};
|
|
|
|
let i3 = SendQueueItem {
|
|
|
|
id: 3,
|
|
|
|
prio: PRIO_HIGH | PRIO_SECONDARY,
|
2022-06-20 21:40:31 +00:00
|
|
|
data: empty_data(),
|
2021-10-20 14:32:47 +00:00
|
|
|
};
|
|
|
|
let i4 = SendQueueItem {
|
|
|
|
id: 4,
|
|
|
|
prio: PRIO_BACKGROUND | PRIO_SECONDARY,
|
2022-06-20 21:40:31 +00:00
|
|
|
data: empty_data(),
|
2021-10-20 14:32:47 +00:00
|
|
|
};
|
|
|
|
let i5 = SendQueueItem {
|
|
|
|
id: 5,
|
|
|
|
prio: PRIO_BACKGROUND | PRIO_PRIMARY,
|
2022-06-20 21:40:31 +00:00
|
|
|
data: empty_data(),
|
2021-10-20 14:32:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut q = SendQueue::new();
|
|
|
|
|
|
|
|
q.push(i1); // 1
|
|
|
|
let a = q.pop().unwrap(); // empty -> 1
|
|
|
|
assert_eq!(a.id, 1);
|
|
|
|
assert!(q.pop().is_none());
|
|
|
|
|
|
|
|
q.push(a); // 1
|
|
|
|
q.push(i2); // 2 1
|
|
|
|
q.push(i2bis); // [2 20] 1
|
|
|
|
let a = q.pop().unwrap(); // 20 1 -> 2
|
|
|
|
assert_eq!(a.id, 2);
|
|
|
|
let b = q.pop().unwrap(); // 1 -> 20
|
|
|
|
assert_eq!(b.id, 20);
|
|
|
|
let c = q.pop().unwrap(); // empty -> 1
|
|
|
|
assert_eq!(c.id, 1);
|
|
|
|
assert!(q.pop().is_none());
|
|
|
|
|
|
|
|
q.push(a); // 2
|
|
|
|
q.push(b); // [2 20]
|
|
|
|
q.push(c); // [2 20] 1
|
|
|
|
q.push(i3); // [2 20] 3 1
|
|
|
|
q.push(i4); // [2 20] 3 1 4
|
|
|
|
q.push(i5); // [2 20] 3 1 5 4
|
|
|
|
|
|
|
|
let a = q.pop().unwrap(); // 20 3 1 5 4 -> 2
|
|
|
|
assert_eq!(a.id, 2);
|
|
|
|
q.push(a); // [20 2] 3 1 5 4
|
|
|
|
|
|
|
|
let a = q.pop().unwrap(); // 2 3 1 5 4 -> 20
|
|
|
|
assert_eq!(a.id, 20);
|
|
|
|
let b = q.pop().unwrap(); // 3 1 5 4 -> 2
|
|
|
|
assert_eq!(b.id, 2);
|
|
|
|
q.push(b); // 2 3 1 5 4
|
|
|
|
let b = q.pop().unwrap(); // 3 1 5 4 -> 2
|
|
|
|
assert_eq!(b.id, 2);
|
|
|
|
let c = q.pop().unwrap(); // 1 5 4 -> 3
|
|
|
|
assert_eq!(c.id, 3);
|
|
|
|
q.push(b); // 2 1 5 4
|
|
|
|
let b = q.pop().unwrap(); // 1 5 4 -> 2
|
|
|
|
assert_eq!(b.id, 2);
|
|
|
|
let e = q.pop().unwrap(); // 5 4 -> 1
|
|
|
|
assert_eq!(e.id, 1);
|
|
|
|
let f = q.pop().unwrap(); // 4 -> 5
|
|
|
|
assert_eq!(f.id, 5);
|
|
|
|
let g = q.pop().unwrap(); // empty -> 4
|
|
|
|
assert_eq!(g.id, 4);
|
|
|
|
assert!(q.pop().is_none());
|
|
|
|
}
|
|
|
|
}
|