asyncio/generic/
stream.rs

1use prelude::{Protocol, SockAddr, Endpoint};
2use ffi::SOCK_STREAM;
3use stream_socket::StreamSocket;
4use socket_listener::SocketListener;
5use generic::GenericEndpoint;
6
7#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
8pub struct GenericStream {
9    family: i32,
10    protocol: i32,
11    capacity: usize,
12}
13
14impl Protocol for GenericStream {
15    type Endpoint = GenericEndpoint<Self>;
16
17    fn family_type(&self) -> i32 {
18        self.family
19    }
20
21    fn socket_type(&self) -> i32 {
22        SOCK_STREAM
23    }
24
25    fn protocol_type(&self) -> i32 {
26        self.protocol
27    }
28
29    unsafe fn uninitialized(&self) -> Self::Endpoint {
30        GenericEndpoint::default(self.capacity, self.protocol)
31    }
32}
33
34impl Endpoint<GenericStream> for GenericEndpoint<GenericStream> {
35    fn protocol(&self) -> GenericStream {
36        GenericStream {
37            family: self.as_ref().sa_family as i32,
38            protocol: self.protocol,
39            capacity: self.capacity(),
40        }
41    }
42}
43
44pub type GenericStreamEndpoint = GenericEndpoint<GenericStream>;
45
46pub type GenericStreamSocket = StreamSocket<GenericStream>;
47
48pub type GenericStreamListener = SocketListener<GenericStream, StreamSocket<GenericStream>>;
49
50
51#[test]
52fn test_generic_tcp() {
53    use core::IoContext;
54    use socket_base::ReuseAddr;
55    use ip::{IpAddrV4, TcpEndpoint};
56
57    let ctx = &IoContext::new().unwrap();
58    let ep = GenericStreamEndpoint::new(&TcpEndpoint::new(IpAddrV4::loopback(), 12345), 0);
59    let soc = GenericStreamListener::new(ctx, ep.protocol()).unwrap();
60    soc.set_option(ReuseAddr::new(true)).unwrap();
61    soc.bind(&ep).unwrap();
62    soc.listen().unwrap();
63}