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
use std::io;
use std::sync::Arc;
use std::net::SocketAddr;
use std::marker::PhantomData;
use BindClient;
use tokio_core::reactor::Handle;
use tokio_core::net::{TcpStream, TcpStreamNew};
use futures::{Future, Poll, Async};
pub struct TcpClient<Kind, P> {
_kind: PhantomData<Kind>,
proto: Arc<P>,
}
pub struct Connect<Kind, P> {
_kind: PhantomData<Kind>,
proto: Arc<P>,
socket: TcpStreamNew,
handle: Handle,
}
impl<Kind, P> Future for Connect<Kind, P> where P: BindClient<Kind, TcpStream> {
type Item = P::BindClient;
type Error = io::Error;
fn poll(&mut self) -> Poll<P::BindClient, io::Error> {
let socket = try_ready!(self.socket.poll());
Ok(Async::Ready(self.proto.bind_client(&self.handle, socket)))
}
}
impl<Kind, P> TcpClient<Kind, P> where P: BindClient<Kind, TcpStream> {
pub fn new(protocol: P) -> TcpClient<Kind, P> {
TcpClient {
_kind: PhantomData,
proto: Arc::new(protocol)
}
}
pub fn connect(&self, addr: &SocketAddr, handle: &Handle) -> Connect<Kind, P> {
Connect {
_kind: PhantomData,
proto: self.proto.clone(),
socket: TcpStream::connect(addr, handle),
handle: handle.clone(),
}
}
}