asyncio/local/
stream.rs

1use prelude::{Protocol, Endpoint};
2use ffi::{AF_UNIX, SOCK_STREAM};
3use stream_socket::StreamSocket;
4use socket_listener::{SocketListener};
5use local::{LocalProtocol, LocalEndpoint};
6
7use std::fmt;
8use std::mem;
9
10/// The stream-oriented UNIX domain protocol.
11///
12/// # Example
13/// Create a server and client sockets.
14///
15/// ```rust,no_run
16/// use asyncio::{IoContext, Endpoint};
17/// use asyncio::local::{LocalStream, LocalStreamEndpoint, LocalStreamSocket, LocalStreamListener};
18///
19/// let ctx = &IoContext::new().unwrap();
20/// let ep = LocalStreamEndpoint::new("example.sock").unwrap();
21///
22/// let sv = LocalStreamListener::new(ctx, LocalStream).unwrap();
23/// sv.bind(&ep).unwrap();
24/// sv.listen().unwrap();
25///
26/// let cl = LocalStreamSocket::new(ctx, ep.protocol()).unwrap();
27/// cl.connect(&ep).unwrap();
28/// ```
29#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
30pub struct LocalStream;
31
32impl Protocol for LocalStream {
33    type Endpoint = LocalEndpoint<Self>;
34
35    fn family_type(&self) -> i32 {
36        AF_UNIX
37    }
38
39    fn socket_type(&self) -> i32 {
40        SOCK_STREAM
41    }
42
43    fn protocol_type(&self) -> i32 {
44        0
45    }
46
47    unsafe fn uninitialized(&self) -> Self::Endpoint {
48        mem::uninitialized()
49    }
50}
51
52impl LocalProtocol for LocalStream {
53    type Socket = StreamSocket<Self>;
54}
55
56impl Endpoint<LocalStream> for LocalEndpoint<LocalStream> {
57    fn protocol(&self) -> LocalStream {
58        LocalStream
59    }
60}
61
62impl fmt::Debug for LocalStream {
63    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64        write!(f, "LocalStream")
65    }
66}
67
68impl fmt::Debug for LocalEndpoint<LocalStream> {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        write!(f, "LocalEndpoint(Stream:\"{}\")", self)
71    }
72}
73
74/// The stream-oriented UNIX domain endpoint type
75pub type LocalStreamEndpoint = LocalEndpoint<LocalStream>;
76
77/// The stream-oriented UNIX domain socket type.
78pub type LocalStreamSocket = StreamSocket<LocalStream>;
79
80/// The stream-oriented UNIX domain listener type.
81pub type LocalStreamListener = SocketListener<LocalStream, StreamSocket<LocalStream>>;
82
83#[test]
84fn test_stream() {
85    assert!(LocalStream == LocalStream);
86}
87
88#[test]
89fn test_getsockname_local() {
90    use core::IoContext;
91    use local::*;
92
93    use std::fs;
94
95    let ctx = &IoContext::new().unwrap();
96    let ep = LocalStreamEndpoint::new("/tmp/asio_foo.sock").unwrap();
97    let soc = LocalStreamSocket::new(ctx, ep.protocol()).unwrap();
98    let _ = fs::remove_file(ep.path());
99    soc.bind(&ep).unwrap();
100    assert_eq!(soc.local_endpoint().unwrap(), ep);
101    let _ = fs::remove_file(ep.path());
102}
103
104#[test]
105fn test_format() {
106    use core::IoContext;
107
108    let ctx = &IoContext::new().unwrap();
109    println!("{:?}", LocalStream);
110    println!("{:?}", LocalStreamEndpoint::new("foo/bar").unwrap());
111    println!("{:?}", LocalStreamSocket::new(ctx, LocalStream).unwrap());
112}