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
mod framed;
pub mod handshake;
use bytes::Bytes;
use framed::{MAX_FRAME_LEN, NoiseFramed};
use futures::ready;
use futures::prelude::*;
use log::trace;
use std::{cmp::min, fmt, io, pin::Pin, task::{Context, Poll}};
pub struct NoiseOutput<T> {
io: NoiseFramed<T, snow::TransportState>,
recv_buffer: Bytes,
recv_offset: usize,
send_buffer: Vec<u8>,
send_offset: usize,
}
impl<T> fmt::Debug for NoiseOutput<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NoiseOutput")
.field("io", &self.io)
.finish()
}
}
impl<T> NoiseOutput<T> {
fn new(io: NoiseFramed<T, snow::TransportState>) -> Self {
NoiseOutput {
io,
recv_buffer: Bytes::new(),
recv_offset: 0,
send_buffer: Vec::new(),
send_offset: 0,
}
}
}
impl<T: AsyncRead + Unpin> AsyncRead for NoiseOutput<T> {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
loop {
let len = self.recv_buffer.len();
let off = self.recv_offset;
if len > 0 {
let n = min(len - off, buf.len());
buf[.. n].copy_from_slice(&self.recv_buffer[off .. off + n]);
trace!("read: copied {}/{} bytes", off + n, len);
self.recv_offset += n;
if len == self.recv_offset {
trace!("read: frame consumed");
self.recv_buffer = Bytes::new();
}
return Poll::Ready(Ok(n))
}
match Pin::new(&mut self.io).poll_next(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(None) => return Poll::Ready(Ok(0)),
Poll::Ready(Some(Err(e))) => return Poll::Ready(Err(e)),
Poll::Ready(Some(Ok(frame))) => {
self.recv_buffer = frame;
self.recv_offset = 0;
}
}
}
}
}
impl<T: AsyncWrite + Unpin> AsyncWrite for NoiseOutput<T> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
let this = Pin::into_inner(self);
let mut io = Pin::new(&mut this.io);
let frame_buf = &mut this.send_buffer;
if this.send_offset == MAX_FRAME_LEN {
trace!("write: sending {} bytes", MAX_FRAME_LEN);
ready!(io.as_mut().poll_ready(cx))?;
io.as_mut().start_send(&frame_buf)?;
this.send_offset = 0;
}
let off = this.send_offset;
let n = min(MAX_FRAME_LEN, off.saturating_add(buf.len()));
this.send_buffer.resize(n, 0u8);
let n = min(MAX_FRAME_LEN - off, buf.len());
this.send_buffer[off .. off + n].copy_from_slice(&buf[.. n]);
this.send_offset += n;
trace!("write: buffered {} bytes", this.send_offset);
return Poll::Ready(Ok(n))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let this = Pin::into_inner(self);
let mut io = Pin::new(&mut this.io);
let frame_buf = &mut this.send_buffer;
if this.send_offset > 0 {
ready!(io.as_mut().poll_ready(cx))?;
trace!("flush: sending {} bytes", this.send_offset);
io.as_mut().start_send(&frame_buf)?;
this.send_offset = 0;
}
io.as_mut().poll_flush(cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>{
ready!(self.as_mut().poll_flush(cx))?;
Pin::new(&mut self.io).poll_close(cx)
}
}