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
use super::*;
use std::io::Write;
use tokio_io::{ AsyncRead, AsyncWrite };
use futures::{Async, Future, Poll};
use common::Stream;
macro_rules! try_async {
( $e:expr ) => {
match $e {
Ok(n) => n,
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock =>
return Ok(Async::NotReady),
Err(e) => return Err(e)
}
}
}
impl<IO: AsyncRead + AsyncWrite> Future for Connect<IO> {
type Item = TlsStream<IO, ClientSession>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl<IO: AsyncRead + AsyncWrite> Future for Accept<IO> {
type Item = TlsStream<IO, ServerSession>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl<IO, S> Future for MidHandshake<IO, S>
where
IO: io::Read + io::Write,
S: Session
{
type Item = TlsStream<IO, S>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
{
let stream = self.inner.as_mut().unwrap();
let (io, session) = stream.get_mut();
let mut stream = Stream::new(session, io);
if stream.session.is_handshaking() {
try_async!(stream.complete_io());
}
if stream.session.wants_write() {
try_async!(stream.complete_io());
}
}
Ok(Async::Ready(self.inner.take().unwrap()))
}
}
impl<IO, S> AsyncRead for TlsStream<IO, S>
where
IO: AsyncRead + AsyncWrite,
S: Session
{
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
false
}
}
impl<IO, S> AsyncWrite for TlsStream<IO, S>
where
IO: AsyncRead + AsyncWrite,
S: Session
{
fn shutdown(&mut self) -> Poll<(), io::Error> {
if !self.is_shutdown {
self.session.send_close_notify();
self.is_shutdown = true;
}
{
let mut stream = Stream::new(&mut self.session, &mut self.io);
try_async!(stream.flush());
}
self.io.shutdown()
}
}