Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

A connection to a remote peer. You can establish a connection by either connecting to a peer via the crate::api::connect method or by accepting connections from a listener.

Implementations§

Source§

impl Connection

Source

pub fn split(self) -> (OwnedReader, OwnedWriter)

Split connection to owned instances of a reader and a writer.

Examples found in repository?
examples/broadcast.rs (line 119)
118async fn handle_client_connection(state: NodeStateRef, conn: api::Connection) {
119    let (mut reader, _writer) = conn.split();
120    let msg = reader.recv::<Message>().await.unwrap();
121    if let Message::Payload(id, payload) = msg {
122        state.borrow_mut().handle_message_from_client(id, payload);
123    } else {
124        panic!("unexpected.");
125    }
126}
127
128/// Handle the connection that is made from another node.
129async fn handle_connection(state: NodeStateRef, conn: api::Connection) {
130    let remote = conn.remote();
131    let (mut reader, writer) = conn.split();
132
133    // Insert the writer half of this connection into the state.
134    let b_conn: BroadcastConnection = writer.into();
135    state.borrow_mut().conns.insert(remote, b_conn);
136
137    while let Some(msg) = reader.recv::<Message>().await {
138        match msg {
139            Message::Payload(id, payload) => {
140                state.borrow_mut().handle_message(remote, id, payload);
141            },
142            Message::Advr(id) => {
143                state.borrow_mut().handle_advr(remote, id);
144            },
145            Message::Want(id) => {
146                state.borrow_mut().handle_want(remote, id);
147            },
148        }
149    }
150}
Source

pub fn remote(&self) -> RemoteAddr

Returns the address of the other end of this connection.

Examples found in repository?
examples/broadcast.rs (line 101)
98async fn listen_for_connections(n: usize, state: NodeStateRef) {
99    let mut listener = api::listen(80);
100    while let Some(conn) = listener.accept().await {
101        if n == *conn.remote() {
102            api::spawn(handle_client_connection(state.clone(), conn));
103        } else {
104            api::spawn(handle_connection(state.clone(), conn));
105        }
106    }
107}
108
109async fn make_connections(n: usize, state: NodeStateRef) {
110    let index = *api::RemoteAddr::whoami();
111    let connect_to = (index + 1) % n;
112    let addr = api::RemoteAddr::from_global_index(connect_to);
113    let conn = api::connect(addr, 80).await.expect("Could not connect.");
114    handle_connection(state, conn).await;
115}
116
117/// Handle the connection that is made from a client.
118async fn handle_client_connection(state: NodeStateRef, conn: api::Connection) {
119    let (mut reader, _writer) = conn.split();
120    let msg = reader.recv::<Message>().await.unwrap();
121    if let Message::Payload(id, payload) = msg {
122        state.borrow_mut().handle_message_from_client(id, payload);
123    } else {
124        panic!("unexpected.");
125    }
126}
127
128/// Handle the connection that is made from another node.
129async fn handle_connection(state: NodeStateRef, conn: api::Connection) {
130    let remote = conn.remote();
131    let (mut reader, writer) = conn.split();
132
133    // Insert the writer half of this connection into the state.
134    let b_conn: BroadcastConnection = writer.into();
135    state.borrow_mut().conns.insert(remote, b_conn);
136
137    while let Some(msg) = reader.recv::<Message>().await {
138        match msg {
139            Message::Payload(id, payload) => {
140                state.borrow_mut().handle_message(remote, id, payload);
141            },
142            Message::Advr(id) => {
143                state.borrow_mut().handle_advr(remote, id);
144            },
145            Message::Want(id) => {
146                state.borrow_mut().handle_want(remote, id);
147            },
148        }
149    }
150}
Source

pub fn is_closed(&self) -> bool

Returns true if the connection is closed.

Source

pub async fn recv<T>(&mut self) -> Option<T>

Receive a message from the connection.

Source

pub fn write<T>(&mut self, message: &T)
where T: Serialize,

Send a message through the connection.

Examples found in repository?
examples/broadcast.rs (line 164)
154async fn run_client(n: usize) {
155    let mut rng = ChaCha8Rng::from_seed([0; 32]);
156
157    for i in 0.. {
158        let index = rng.gen_range(0..n);
159        let addr = api::RemoteAddr::from_global_index(index);
160
161        let mut conn = api::connect(addr, 80).await.expect("Connection failed.");
162
163        let msg = format!("message {i}");
164        conn.write(&Message::Payload(i, msg.into()));
165
166        api::sleep(Duration::from_secs(5)).await;
167    }
168}

Trait Implementations§

Source§

impl Drop for Connection

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V