1pub mod async_net;
2#[cfg(target_os = "windows")]
3pub mod listener;
4#[cfg(target_os = "windows")]
5pub mod socket;
6#[cfg(target_os = "windows")]
7pub mod stream;
8#[cfg(target_os = "windows")]
9mod util;
10
11#[cfg(target_os = "windows")]
12pub use listener::*;
13#[cfg(target_os = "windows")]
14pub use socket::*;
15#[cfg(not(target_os = "windows"))]
16pub use std::os::unix::net::{UnixListener, UnixStream};
17#[cfg(target_os = "windows")]
18pub use stream::*;
19
20#[cfg(test)]
21mod tests {
22 use std::io::{Read, Write};
23
24 use smol::io::{AsyncReadExt, AsyncWriteExt};
25
26 const SERVER_MESSAGE: &str = "Connection closed";
27 const CLIENT_MESSAGE: &str = "Hello, server!";
28 const BUFFER_SIZE: usize = 32;
29
30 #[test]
31 fn test_windows_listener() -> std::io::Result<()> {
32 use crate::{UnixListener, UnixStream};
33
34 let temp = tempfile::tempdir()?;
35 let socket = temp.path().join("socket.sock");
36 let listener = UnixListener::bind(&socket)?;
37
38 // Server
39 let server = std::thread::spawn(move || {
40 let (mut stream, _) = listener.accept().unwrap();
41
42 // Read data from the client
43 let mut buffer = [0; BUFFER_SIZE];
44 let bytes_read = stream.read(&mut buffer).unwrap();
45 let string = String::from_utf8_lossy(&buffer[..bytes_read]);
46 assert_eq!(string, CLIENT_MESSAGE);
47
48 // Send a message back to the client
49 stream.write_all(SERVER_MESSAGE.as_bytes()).unwrap();
50 });
51
52 // Client
53 let mut client = UnixStream::connect(&socket)?;
54
55 // Send data to the server
56 client.write_all(CLIENT_MESSAGE.as_bytes())?;
57 let mut buffer = [0; BUFFER_SIZE];
58
59 // Read the response from the server
60 let bytes_read = client.read(&mut buffer)?;
61 let string = String::from_utf8_lossy(&buffer[..bytes_read]);
62 assert_eq!(string, SERVER_MESSAGE);
63 client.flush()?;
64
65 server.join().unwrap();
66 Ok(())
67 }
68
69 #[test]
70 fn test_unix_listener() -> std::io::Result<()> {
71 use crate::async_net::{UnixListener, UnixStream};
72
73 smol::block_on(async {
74 let temp = tempfile::tempdir()?;
75 let socket = temp.path().join("socket.sock");
76 let listener = UnixListener::bind(&socket)?;
77
78 // Server
79 let server = smol::spawn(async move {
80 let (mut stream, _) = listener.accept().await.unwrap();
81
82 // Read data from the client
83 let mut buffer = [0; BUFFER_SIZE];
84 let bytes_read = stream.read(&mut buffer).await.unwrap();
85 let string = String::from_utf8_lossy(&buffer[..bytes_read]);
86 assert_eq!(string, CLIENT_MESSAGE);
87
88 // Send a message back to the client
89 stream.write_all(SERVER_MESSAGE.as_bytes()).await.unwrap();
90 });
91
92 // Client
93 let mut client = UnixStream::connect(&socket).await?;
94 client.write_all(CLIENT_MESSAGE.as_bytes()).await?;
95
96 // Read the response from the server
97 let mut buffer = [0; BUFFER_SIZE];
98 let bytes_read = client.read(&mut buffer).await?;
99 let string = String::from_utf8_lossy(&buffer[..bytes_read]);
100 assert_eq!(string, "Connection closed");
101 client.flush().await?;
102
103 server.await;
104 Ok(())
105 })
106 }
107}