[][src]Struct qp2p::QuicP2p

pub struct QuicP2p { /* fields omitted */ }

Main QuicP2p instance to communicate with QuicP2p using an async API

Implementations

impl QuicP2p[src]

pub fn new() -> Result<Self>[src]

Construct QuicP2p with the default config and bootstrap cache enabled

Example

use qp2p::QuicP2p;

let quic_p2p = QuicP2p::new().expect("Error initializing QuicP2p");

pub fn with_config(
    cfg: Option<Config>,
    bootstrap_nodes: &[SocketAddr],
    use_bootstrap_cache: bool
) -> Result<Self>
[src]

Construct QuicP2p with supplied parameters, ready to be used. If config is not specified it'll call Config::read_or_construct_default()

bootstrap_nodes: takes bootstrap nodes from the user.

In addition to bootstrap nodes provided, optionally use the nodes found in the bootstrap cache file (if such a file exists) or disable this feature.

Example

use qp2p::{QuicP2p, Config};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

let mut config = Config::default();
config.ip = Some(IpAddr::V4(Ipv4Addr::LOCALHOST));
config.port = Some(3000);
let hcc = &["127.0.0.1:8080".parse().unwrap()];
let quic_p2p = QuicP2p::with_config(Some(config), hcc, true).expect("Error initializing QuicP2p");

pub async fn bootstrap<'_>(&'_ self) -> Result<(Endpoint, Connection)>[src]

Bootstrap to the network.

Bootstrap concept is different from "connect" in several ways: bootstrap() will try to connect to all peers which are specified in the config (hard_coded_contacts) or were previously cached. Once a connection with a peer succeeds, a Connection for such peer will be returned and all other connections will be dropped.

Example

use qp2p::{QuicP2p, Config, Error};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

#[tokio::main]
async fn main() -> Result<(), Error> {

    let mut config = Config::default();
    config.ip = Some(IpAddr::V4(Ipv4Addr::LOCALHOST));
    config.port = Some(3000);
    let mut quic_p2p = QuicP2p::with_config(Some(config.clone()), &[], true)?;
    let endpoint = quic_p2p.new_endpoint()?;
    let peer_addr = endpoint.our_endpoint()?;

    config.port = Some(3001);
    let mut quic_p2p = QuicP2p::with_config(Some(config), &[peer_addr], true)?;
    let (endpoint, connection) = quic_p2p.bootstrap().await?;
    Ok(())
}

pub async fn connect_to<'_, '_>(
    &'_ self,
    node_addr: &'_ SocketAddr
) -> Result<(Endpoint, Connection)>
[src]

Connect to the given peer and return the Endpoint created along with the Connection object if it succeeds, which can then be used to send messages to the connected peer.

Example

use qp2p::{QuicP2p, Config, Error};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

#[tokio::main]
async fn main() -> Result<(), Error> {

    let mut config = Config::default();
    config.ip = Some(IpAddr::V4(Ipv4Addr::LOCALHOST));
    let mut quic_p2p = QuicP2p::with_config(Some(config.clone()), Default::default(), true)?;
    let peer_1 = quic_p2p.new_endpoint()?;
    let peer1_addr = peer_1.our_endpoint()?;

    let (peer_2, connection) = quic_p2p.connect_to(&peer1_addr).await?;
    Ok(())
}

pub fn new_endpoint(&self) -> Result<Endpoint>[src]

Create a new Endpoint which can be used to connect to peers and send messages to them, as well as listen to messages incoming from other peers.

Example

use qp2p::{QuicP2p, Config, Error};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
#[tokio::main]
async fn main() -> Result<(), Error> {

    let mut config = Config::default();
    config.ip = Some(IpAddr::V4(Ipv4Addr::LOCALHOST));
    let mut quic_p2p = QuicP2p::with_config(Some(config.clone()), Default::default(), true)?;
    let endpoint = quic_p2p.new_endpoint()?;
    Ok(())
}

Trait Implementations

impl Clone for QuicP2p[src]

Auto Trait Implementations

impl !RefUnwindSafe for QuicP2p

impl Send for QuicP2p

impl Sync for QuicP2p

impl Unpin for QuicP2p

impl !UnwindSafe for QuicP2p

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,