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
// reference: https://docs.rs/tokio-io-timeout/0.4.0/src/tokio_io_timeout/lib.rs.html

use std::pin::Pin;
use std::task::{ Context, Poll };
use std::io::IoSlice;
use std::future::Future;

use tokio::net::{TcpStream, tcp};
use tokio::time::{ Duration, Instant, sleep_until, Sleep };
use tokio::io::{ self, AsyncRead, AsyncWrite, ReadBuf };

#[derive(Debug)]
pub struct TimeoutReader<S> {
	stream: S,
	timeout: Duration,
	timer: Pin<Box<Sleep>>,
	active: bool
}

impl<S> TimeoutReader<S>
where S: AsyncRead {
	pub fn new(stream: S, timeout: Duration) -> Self {
		Self {
			stream,
			timeout,
			timer: Box::pin(sleep_until(Instant::now())),
			active: false
		}
	}

	pub fn timeout(&self) -> Duration {
		self.timeout
	}

	pub fn set_timeout(&mut self, timeout: Duration) {
		self.timeout = timeout;
		self.timer.as_mut().reset(Instant::now());
		self.active = false;
	}

	pub fn poll_timeout(&mut self, cx: &mut Context) -> io::Result<()> {

		// activate if not activated
		if !self.active {
			self.timer.as_mut().reset(Instant::now() + self.timeout);
			self.active = true;
		}

		match self.timer.as_mut().poll(cx) {
			// timer has ended
			Poll::Ready(_) => Err(io::Error::from(io::ErrorKind::TimedOut)),
			// timer is still running
			Poll::Pending => Ok(())
		}
	}

	#[allow(dead_code)]
	pub fn inner_mut(&mut self) -> &mut S {
		&mut self.stream
	}
}

impl TimeoutReader<TcpStream> {
	#[allow(dead_code)]
	pub fn split<'a>(
		&'a mut self
	) -> (TimeoutReader<tcp::ReadHalf<'a>>, tcp::WriteHalf<'a>) {
		let (read, write) = self.stream.split();
		(TimeoutReader::new(read, self.timeout), write)
	}
}

impl<S> AsyncRead for TimeoutReader<S>
where S: AsyncRead + Unpin {
	fn poll_read(
		mut self: Pin<&mut Self>,
		cx: &mut Context,
		buf: &mut ReadBuf<'_>
	) -> Poll< io::Result<()> > {

		// call poll read on stream
		let r = Pin::new(&mut self.stream).poll_read(cx, buf);

		match r {
			Poll::Pending => self.get_mut().poll_timeout(cx)?,
			_ => { self.active = false }
		}

		r
	}
}

impl<S> AsyncWrite for TimeoutReader<S>
where S: AsyncWrite + Unpin {
	#[inline]
	fn poll_write(
		mut self: Pin<&mut Self>,
		cx: &mut Context,
		buf: &[u8]
	) -> Poll< io::Result<usize> > {
		Pin::new(&mut self.stream).poll_write(cx, buf)
	}

	#[inline]
	fn poll_flush(
		mut self: Pin<&mut Self>,
		cx: &mut Context
	) -> Poll< io::Result<()> > {
		Pin::new(&mut self.stream).poll_flush(cx)
	}

	#[inline]
	fn poll_shutdown(
		mut self: Pin<&mut Self>,
		cx: &mut Context
	) -> Poll< io::Result<()> > {
		Pin::new(&mut self.stream).poll_shutdown(cx)
	}

	#[inline]
	fn poll_write_vectored(
		mut self: Pin<&mut Self>,
		cx: &mut Context<'_>,
		bufs: &[IoSlice<'_>]
	) -> Poll<io::Result<usize>> {
		Pin::new(&mut self.stream).poll_write_vectored(cx, bufs)
	}

	#[inline]
	fn is_write_vectored(&self) -> bool {
		self.stream.is_write_vectored()
	}
}


#[cfg(test)]
mod tests {

	use super::*;
	use tokio::io::AsyncReadExt;
	use tokio::time::sleep;

	struct DelayStream(Pin<Box<Sleep>>);

	impl DelayStream {
		fn new(until: Instant) -> Self {
			Self(Box::pin(sleep_until(until)))
		}
	}

	impl AsyncRead for DelayStream {
		fn poll_read(
			mut self: Pin<&mut Self>,
			cx: &mut Context,
			_: &mut ReadBuf<'_>
		) -> Poll<io::Result<()>> {
			self.0.as_mut().poll(cx)
				.map(|_| Ok(()))
		}
	}

	#[tokio::test]
	async fn read_timeout() {
		let reader = DelayStream::new(Instant::now() + Duration::from_millis(500));
		let mut reader = TimeoutReader::new(reader, Duration::from_millis(100));

		let r = reader.read(&mut [0]).await;
		assert_eq!(r.unwrap_err().kind(), io::ErrorKind::TimedOut);
		let r = reader.read(&mut [0]).await;
		assert_eq!(r.unwrap_err().kind(), io::ErrorKind::TimedOut);
		// now around 200ms passed
		sleep(Duration::from_millis(400)).await;
		reader.read(&mut [0]).await.unwrap();
	}

	#[tokio::test]
	async fn read_ok() {
		let reader = DelayStream::new(Instant::now() + Duration::from_millis(100));
		let mut reader = TimeoutReader::new(reader, Duration::from_millis(500));

		reader.read(&mut [0]).await.unwrap();
	}

}