aboutsummaryrefslogtreecommitdiff
path: root/src/proto.rs
blob: b0442806c8fe35107b0a1bb82ce51bf10827b656 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::Arc;

use log::trace;

use async_trait::async_trait;

use async_std::io::prelude::WriteExt;
use async_std::io::ReadExt;

use tokio::io::{ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use tokio::sync::{mpsc, watch};

use crate::error::*;

use kuska_handshake::async_std::{BoxStreamRead, BoxStreamWrite, TokioCompat};

/// Priority of a request (click to read more about priorities).
///
/// This priority value is used to priorize messages
/// in the send queue of the client, and their responses in the send queue of the
/// server. Lower values mean higher priority.
///
/// This mechanism is usefull for messages bigger than the maximum chunk size
/// (set at `0x4000` bytes), such as large file transfers.
/// In such case, all of the messages in the send queue with the highest priority
/// will take turns to send individual chunks, in a round-robin fashion.
/// Once all highest priority messages are sent successfully, the messages with
/// the next highest priority will begin being sent in the same way.
///
/// The same priority value is given to a request and to its associated response.
pub type RequestPriority = u8;

/// Priority class: high
pub const PRIO_HIGH: RequestPriority = 0x20;
/// Priority class: normal
pub const PRIO_NORMAL: RequestPriority = 0x40;
/// Priority class: background
pub const PRIO_BACKGROUND: RequestPriority = 0x80;
/// Priority: primary among given class
pub const PRIO_PRIMARY: RequestPriority = 0x00;
/// Priority: secondary among given class (ex: `PRIO_HIGH || PRIO_SECONDARY`)
pub const PRIO_SECONDARY: RequestPriority = 0x01;

const MAX_CHUNK_SIZE: usize = 0x4000;

pub(crate) type RequestID = u16;

struct SendQueueItem {
	id: RequestID,
	prio: RequestPriority,
	data: Vec<u8>,
	cursor: usize,
}

struct SendQueue {
	items: BTreeMap<u8, VecDeque<SendQueueItem>>,
}

impl SendQueue {
	fn new() -> Self {
		Self {
			items: BTreeMap::new(),
		}
	}
	fn push(&mut self, item: SendQueueItem) {
		let prio = item.prio;
		let mut items_at_prio = self
			.items
			.remove(&prio)
			.unwrap_or(VecDeque::with_capacity(4));
		items_at_prio.push_back(item);
		self.items.insert(prio, items_at_prio);
	}
	fn pop(&mut self) -> Option<SendQueueItem> {
		match self.items.pop_first() {
			None => None,
			Some((prio, mut items_at_prio)) => {
				let ret = items_at_prio.pop_front();
				if !items_at_prio.is_empty() {
					self.items.insert(prio, items_at_prio);
				}
				ret
			}
		}
	}
}

#[async_trait]
pub(crate) trait SendLoop: Sync {
	async fn send_loop(
		self: Arc<Self>,
		mut msg_recv: mpsc::UnboundedReceiver<(RequestID, RequestPriority, Vec<u8>)>,
		mut write: BoxStreamWrite<TokioCompat<WriteHalf<TcpStream>>>,
		mut must_exit: watch::Receiver<bool>,
	) -> Result<(), Error> {
		let mut sending = SendQueue::new();
		while !*must_exit.borrow() {
			if let Ok((id, prio, data)) = msg_recv.try_recv() {
				trace!("send_loop: got {}, {} bytes", id, data.len());
				sending.push(SendQueueItem {
					id,
					prio,
					data,
					cursor: 0,
				});
			} else if let Some(mut item) = sending.pop() {
				trace!(
					"send_loop: sending bytes for {} ({} bytes, {} already sent)",
					item.id,
					item.data.len(),
					item.cursor
				);
				let header_id = u16::to_be_bytes(item.id);
				if write_all_or_exit(&header_id[..], &mut write, &mut must_exit)
					.await?
					.is_none()
				{
					break;
				}

				if item.data.len() - item.cursor > MAX_CHUNK_SIZE {
					let header_size = u16::to_be_bytes(MAX_CHUNK_SIZE as u16 | 0x8000);
					if write_all_or_exit(&header_size[..], &mut write, &mut must_exit)
						.await?
						.is_none()
					{
						break;
					}

					let new_cursor = item.cursor + MAX_CHUNK_SIZE as usize;
					if write_all_or_exit(
						&item.data[item.cursor..new_cursor],
						&mut write,
						&mut must_exit,
					)
					.await?
					.is_none()
					{
						break;
					}
					item.cursor = new_cursor;

					sending.push(item);
				} else {
					let send_len = (item.data.len() - item.cursor) as u16;

					let header_size = u16::to_be_bytes(send_len);
					if write_all_or_exit(&header_size[..], &mut write, &mut must_exit)
						.await?
						.is_none()
					{
						break;
					}

					if write_all_or_exit(&item.data[item.cursor..], &mut write, &mut must_exit)
						.await?
						.is_none()
					{
						break;
					}
				}
				write.flush().await.log_err("Could not flush in send_loop");
			} else {
				let (id, prio, data) = msg_recv
					.recv()
					.await
					.ok_or(Error::Message("Connection closed.".into()))?;
				trace!("send_loop: got {}, {} bytes", id, data.len());
				sending.push(SendQueueItem {
					id,
					prio,
					data,
					cursor: 0,
				});
			}
		}
		Ok(())
	}
}

#[async_trait]
pub(crate) trait RecvLoop: Sync + 'static {
	async fn recv_handler(self: Arc<Self>, id: RequestID, msg: Vec<u8>);

	async fn recv_loop(
		self: Arc<Self>,
		mut read: BoxStreamRead<TokioCompat<ReadHalf<TcpStream>>>,
		mut must_exit: watch::Receiver<bool>,
	) -> Result<(), Error> {
		let mut receiving = HashMap::new();
		while !*must_exit.borrow() {
			trace!("recv_loop: reading packet");
			let mut header_id = [0u8; 2];
			if read_exact_or_exit(&mut header_id[..], &mut read, &mut must_exit)
				.await?
				.is_none()
			{
				break;
			}
			let id = RequestID::from_be_bytes(header_id);
			trace!("recv_loop: got header id: {:04x}", id);

			let mut header_size = [0u8; 2];
			if read_exact_or_exit(&mut header_size[..], &mut read, &mut must_exit)
				.await?
				.is_none()
			{
				break;
			}
			let size = RequestID::from_be_bytes(header_size);
			trace!("recv_loop: got header size: {:04x}", id);

			let has_cont = (size & 0x8000) != 0;
			let size = size & !0x8000;

			let mut next_slice = vec![0; size as usize];
			if read_exact_or_exit(&mut next_slice[..], &mut read, &mut must_exit)
				.await?
				.is_none()
			{
				break;
			}
			trace!("recv_loop: read {} bytes", size);

			let mut msg_bytes = receiving.remove(&id).unwrap_or(vec![]);
			msg_bytes.extend_from_slice(&next_slice[..]);

			if has_cont {
				receiving.insert(id, msg_bytes);
			} else {
				tokio::spawn(self.clone().recv_handler(id, msg_bytes));
			}
		}
		Ok(())
	}
}

async fn read_exact_or_exit(
	buf: &mut [u8],
	read: &mut BoxStreamRead<TokioCompat<ReadHalf<TcpStream>>>,
	must_exit: &mut watch::Receiver<bool>,
) -> Result<Option<()>, Error> {
	tokio::select!(
		res = read.read_exact(buf) => Ok(Some(res?)),
		_ = await_exit(must_exit) => Ok(None),
	)
}

async fn write_all_or_exit(
	buf: &[u8],
	write: &mut BoxStreamWrite<TokioCompat<WriteHalf<TcpStream>>>,
	must_exit: &mut watch::Receiver<bool>,
) -> Result<Option<()>, Error> {
	tokio::select!(
		res = write.write_all(buf) => Ok(Some(res?)),
		_ = await_exit(must_exit) => Ok(None),
	)
}

async fn await_exit(must_exit: &mut watch::Receiver<bool>) {
	loop {
		if must_exit.recv().await == Some(true) {
			return;
		}
	}
}