Struct connection::Connection
source · pub struct Connection { /* private fields */ }
Expand description
A TCP connection that can be used to send and receive serializable values
Implementations§
source§impl Connection
impl Connection
sourcepub async fn dial<A: ToSocketAddrs>(
addr: A
) -> Result<Connection, ConnectionError>
pub async fn dial<A: ToSocketAddrs>( addr: A ) -> Result<Connection, ConnectionError>
Connect to a socket address and return a new connection with the default buffer capacity
Examples
use connection::{Connection};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let mut conn = Connection::dial("127.0.0.1:8080").await?;
// Send a message
conn.write(&"Hello, world!").await?;
Ok(())
}
sourcepub async fn dial_with_capacity<A: ToSocketAddrs>(
addr: A,
capacity: usize
) -> Result<Connection, ConnectionError>
pub async fn dial_with_capacity<A: ToSocketAddrs>( addr: A, capacity: usize ) -> Result<Connection, ConnectionError>
Connect to a socket address and return a new connection with a custom buffer capacity
Examples
use connection::{Connection};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let buffer_size = 4096;
let mut conn = Connection::dial_with_capacity("127.0.0.1:8080", buffer_size).await?;
// Send a message
conn.write(&"Hello, world!").await?;
Ok(())
}
sourcepub fn new(stream: TcpStream) -> Self
pub fn new(stream: TcpStream) -> Self
Create a new connection with the default buffer capacity
Examples
use connection::{Connection};
use tokio::net::TcpStream;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let stream = TcpStream::connect("127.0.0.1:8080").await?;
let mut conn = Connection::new(stream);
// Send a message
conn.write(&"Hello, world!").await?;
Ok(())
}
sourcepub fn new_with_capacity(stream: TcpStream, capacity: usize) -> Self
pub fn new_with_capacity(stream: TcpStream, capacity: usize) -> Self
Create a new connection with a custom buffer capacity
Examples
use connection::{Connection};
use tokio::net::TcpStream;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let stream = TcpStream::connect("127.0.0.1:8080").await?;
let buffer_size = 4096;
let mut conn = Connection::new_with_capacity(stream, buffer_size);
// Send a message
conn.write(&"Hello, world!").await?;
Ok(())
}
sourcepub async fn write<T: Serialize>(
&mut self,
value: &T
) -> Result<(), ConnectionError>
pub async fn write<T: Serialize>( &mut self, value: &T ) -> Result<(), ConnectionError>
Write a serializable value into the stream
Examples
use serde::{Serialize, Deserialize};
use connection::{Connection};
use std::error::Error;
#[derive(Serialize, Deserialize)]
struct Message { id: usize, text: String }
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let mut conn = Connection::dial("127.0.0.1:8080").await?;
// Create a message
let message = Message { id: 1, text: "Hello, world!".to_string() };
// Send a message
conn.write::<Message>(&message).await?;
Ok(())
}
sourcepub async fn read<T: DeserializeOwned>(
&mut self
) -> Result<Option<T>, ConnectionError>
pub async fn read<T: DeserializeOwned>( &mut self ) -> Result<Option<T>, ConnectionError>
Reads from the socket until a complete message is received, or an error occurs
Examples
use serde::{Serialize, Deserialize};
use connection::{Connection};
use std::error::Error;
#[derive(Serialize, Deserialize)]
struct Message { id: usize, text: String }
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let mut conn = Connection::dial("127.0.0.1:8080").await?;
// Read a message
let message: Message = conn.read::<Message>().await?.unwrap();
Ok(())
}