pub struct Endpoint(/* private fields */);Expand description
IPC endpoint.
Implementations§
Source§impl Endpoint
impl Endpoint
Sourcepub fn incoming(self) -> Result<IpcStream>
pub fn incoming(self) -> Result<IpcStream>
Stream of incoming connections
Examples found in repository?
examples/server.rs (line 8)
5async fn run_server(path: String) {
6 let endpoint = Endpoint::new(ServerId::new(path), OnConflict::Overwrite).unwrap();
7
8 let incoming = endpoint.incoming().expect("failed to open new socket");
9 futures_util::pin_mut!(incoming);
10
11 while let Some(result) = incoming.next().await {
12 match result {
13 Ok(stream) => {
14 let (mut reader, mut writer) = split(stream);
15
16 tokio::spawn(async move {
17 loop {
18 let mut buf = [0u8; 4];
19
20 if reader.read_exact(&mut buf).await.is_err() {
21 println!("Closing socket");
22 break;
23 }
24 if let Ok("ping") = std::str::from_utf8(&buf[..]) {
25 println!("RECEIVED: PING");
26 writer
27 .write_all(b"pong")
28 .await
29 .expect("unable to write to socket");
30 println!("SEND: PONG");
31 }
32 }
33 });
34 }
35 _ => unreachable!("ideally"),
36 }
37 }
38}Sourcepub fn security_attributes(
self,
security_attributes: SecurityAttributes,
) -> Self
pub fn security_attributes( self, security_attributes: SecurityAttributes, ) -> Self
Set security attributes for the connection
Sourcepub async fn connect<P>(path: P) -> Result<Connection>where
P: IntoIpcPath,
pub async fn connect<P>(path: P) -> Result<Connection>where
P: IntoIpcPath,
Make new connection using the provided path and running event pool.
Examples found in repository?
examples/client.rs (line 10)
5async fn main() {
6 let path = std::env::args()
7 .nth(1)
8 .expect("Run it with server path to connect as argument");
9
10 let mut client = Endpoint::connect(ServerId::new(path))
11 .await
12 .expect("Failed to connect client.");
13
14 loop {
15 let mut buf = [0u8; 4];
16 println!("SEND: PING");
17 client
18 .write_all(b"ping")
19 .await
20 .expect("Unable to write message to client");
21 client
22 .read_exact(&mut buf[..])
23 .await
24 .expect("Unable to read buffer");
25 if let Ok("pong") = std::str::from_utf8(&buf[..]) {
26 println!("RECEIVED: PONG");
27 } else {
28 break;
29 }
30
31 tokio::time::sleep(std::time::Duration::from_secs(2)).await;
32 }
33}Sourcepub fn new<P>(path: P, on_conflict: OnConflict) -> Result<Self>where
P: IntoIpcPath,
pub fn new<P>(path: P, on_conflict: OnConflict) -> Result<Self>where
P: IntoIpcPath,
New IPC endpoint at the given path
Examples found in repository?
examples/server.rs (line 6)
5async fn run_server(path: String) {
6 let endpoint = Endpoint::new(ServerId::new(path), OnConflict::Overwrite).unwrap();
7
8 let incoming = endpoint.incoming().expect("failed to open new socket");
9 futures_util::pin_mut!(incoming);
10
11 while let Some(result) = incoming.next().await {
12 match result {
13 Ok(stream) => {
14 let (mut reader, mut writer) = split(stream);
15
16 tokio::spawn(async move {
17 loop {
18 let mut buf = [0u8; 4];
19
20 if reader.read_exact(&mut buf).await.is_err() {
21 println!("Closing socket");
22 break;
23 }
24 if let Ok("ping") = std::str::from_utf8(&buf[..]) {
25 println!("RECEIVED: PING");
26 writer
27 .write_all(b"pong")
28 .await
29 .expect("unable to write to socket");
30 println!("SEND: PONG");
31 }
32 }
33 });
34 }
35 _ => unreachable!("ideally"),
36 }
37 }
38}Auto Trait Implementations§
impl Freeze for Endpoint
impl RefUnwindSafe for Endpoint
impl Send for Endpoint
impl Sync for Endpoint
impl Unpin for Endpoint
impl UnwindSafe for Endpoint
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more