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
impl Connection
Sourcepub fn split(self) -> (OwnedReader, OwnedWriter)
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}Sourcepub fn remote(&self) -> RemoteAddr
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}Sourcepub async fn recv<T>(&mut self) -> Option<T>where
T: DeserializeOwned,
pub async fn recv<T>(&mut self) -> Option<T>where
T: DeserializeOwned,
Receive a message from the connection.
Sourcepub fn write<T>(&mut self, message: &T)where
T: Serialize,
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§
Auto Trait Implementations§
impl Freeze for Connection
impl RefUnwindSafe for Connection
impl Send for Connection
impl Sync for Connection
impl Unpin for Connection
impl UnwindSafe for Connection
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