Struct Endpoint

Source
pub struct Endpoint(/* private fields */);
Expand description

IPC endpoint.

Implementations§

Source§

impl Endpoint

Source

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}
Source

pub fn security_attributes( self, security_attributes: SecurityAttributes, ) -> Self

Set security attributes for the connection

Source

pub fn path(&self) -> &Path

Returns the path of the endpoint.

Source

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}
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more