pub struct Socket<T: IdentityCanonicalizer> { /* private fields */ }Expand description
A socket represents an endpoint and other state correspondent uses to connect to peers.
Implementations§
Source§impl<T: IdentityCanonicalizer> Socket<T>
impl<T: IdentityCanonicalizer> Socket<T>
Sourcepub fn close(&self, error_code: VarInt, reason: &[u8])
pub fn close(&self, error_code: VarInt, reason: &[u8])
Close the socket, shutting down all in-progress operations
Sourcepub fn endpoint(&self) -> &Endpoint
pub fn endpoint(&self) -> &Endpoint
Access the internal endpoint object.
Examples found in repository?
examples/chat.rs (line 149)
120fn read_from_stdin(
121 process_id: u32,
122 socket: Socket<ProcessIdCanonicalizer>,
123 connection_set: SharedConnectionSet,
124) {
125 use std::io::BufRead;
126 let stdin = std::io::stdin();
127 let mut lines = stdin.lock().lines();
128 show_prompt(process_id);
129 while let Some(Ok(line)) = lines.next() {
130 let current_peers: Vec<Connection> = {
131 let current_peers = connection_set.lock().unwrap();
132 // Clone the connections in the set so the mutex can
133 // be released early
134 current_peers.values().cloned().collect()
135 };
136 for conn in current_peers {
137 let line = line.clone();
138 // Use tokio::spawn so messages are sent to peers concurrently
139 tokio::spawn(async move {
140 let mut stream = conn.open_uni().await.ok()?;
141 stream.write_all(line.as_bytes()).await.ok()?;
142 stream.finish().await.ok()?;
143 Some(())
144 });
145 }
146 show_prompt(process_id);
147 }
148 // We're done with input, so shutdown the socket
149 socket.endpoint().close(0u8.into(), b"");
150}Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Socket<T>
impl<T> !RefUnwindSafe for Socket<T>
impl<T> Send for Socket<T>
impl<T> Sync for Socket<T>
impl<T> Unpin for Socket<T>
impl<T> !UnwindSafe for Socket<T>
Blanket Implementations§
Source§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
Source§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
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