pub struct Builder { /* private fields */ }Expand description
Builder for creating a P2P endpoint with custom configuration.
The Builder uses the builder pattern to provide a fluent API for configuring
all aspects of a P2P endpoint before creating it.
§Examples
§Basic Configuration
use rustp2p::Builder;
use std::net::Ipv4Addr;
let endpoint = Builder::new()
.node_id(Ipv4Addr::new(10, 0, 0, 1).into())
.udp_port(8080)
.build()
.await?;§Full Configuration
use rustp2p::{Builder, PeerNodeAddress, GroupCode};
use rustp2p::cipher::Algorithm;
use std::net::Ipv4Addr;
use std::str::FromStr;
let endpoint = Builder::new()
.node_id(Ipv4Addr::new(10, 0, 0, 1).into())
.udp_port(8080)
.tcp_port(8080)
.peers(vec![
PeerNodeAddress::from_str("udp://192.168.1.100:9090").unwrap()
])
.group_code(GroupCode::try_from("12345").unwrap())
.encryption(Algorithm::AesGcm("password".to_string()))
.build()
.await?;Implementations§
Source§impl Builder
impl Builder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder with default settings.
§Examples
use rustp2p::Builder;
let builder = Builder::new();Sourcepub fn from_config(config: Config) -> Self
pub fn from_config(config: Config) -> Self
Sourcepub fn peers(self, peers: Vec<PeerNodeAddress>) -> Self
pub fn peers(self, peers: Vec<PeerNodeAddress>) -> Self
Sets the initial list of peers to connect to.
These peers will be contacted when the endpoint starts to help with peer discovery and NAT traversal.
§Arguments
peers- A vector of peer addresses
§Examples
use rustp2p::{Builder, PeerNodeAddress};
use std::str::FromStr;
let builder = Builder::new()
.peers(vec![
PeerNodeAddress::from_str("udp://192.168.1.100:9090").unwrap(),
PeerNodeAddress::from_str("tcp://10.0.0.1:8080").unwrap(),
]);Sourcepub fn group_code(self, group_code: GroupCode) -> Self
pub fn group_code(self, group_code: GroupCode) -> Self
Sets the group code for this endpoint.
Endpoints with different group codes will not be able to communicate. This allows you to create isolated P2P networks.
§Arguments
group_code- A unique group identifier
§Examples
use rustp2p::Builder;
use rustp2p::GroupCode;
let builder = Builder::new()
.group_code(GroupCode::try_from("12345").unwrap());Sourcepub fn encryption(self, encryption: Algorithm) -> Self
pub fn encryption(self, encryption: Algorithm) -> Self
Sets the encryption algorithm for this endpoint.
When encryption is enabled, all communication will be encrypted with the specified algorithm and password. All peers must use the same encryption settings to communicate.
§Arguments
encryption- The encryption algorithm and password
§Examples
use rustp2p::{Builder, cipher::Algorithm};
let builder = Builder::new()
.encryption(Algorithm::AesGcm("my-secret-password".to_string()));Sourcepub fn node_id(self, node_id: NodeID) -> Self
pub fn node_id(self, node_id: NodeID) -> Self
Sets the node ID for this endpoint (required).
Each peer in the network must have a unique node ID. The node ID is used to identify and route messages to the correct destination.
§Arguments
node_id- A unique identifier for this node
§Examples
use rustp2p::Builder;
use std::net::Ipv4Addr;
let builder = Builder::new()
.node_id(Ipv4Addr::new(10, 0, 0, 1).into());Sourcepub fn interceptor<I: DataInterceptor + 'static>(self, interceptor: I) -> Self
pub fn interceptor<I: DataInterceptor + 'static>(self, interceptor: I) -> Self
Sets a custom data interceptor.
The interceptor can inspect and modify data before it is processed.
§Arguments
interceptor- An implementation of theDataInterceptortrait
Sourcepub fn punching_policy<I: PunchingPolicy + 'static>(
self,
punching_policy: I,
) -> Self
pub fn punching_policy<I: PunchingPolicy + 'static>( self, punching_policy: I, ) -> Self
Sets a custom punching policy.
The punching policy determines how NAT traversal is attempted.
§Arguments
punching_policy- An implementation of thePunchingPolicytrait
Sourcepub fn load_balance(self, load_balance: LoadBalance) -> Self
pub fn load_balance(self, load_balance: LoadBalance) -> Self
Sets the load balancing strategy.
Determines how traffic is distributed across multiple available routes.
§Arguments
load_balance- The load balancing strategy
Sourcepub fn bind_interface(self, interface: LocalInterface) -> Self
pub fn bind_interface(self, interface: LocalInterface) -> Self
Sourcepub async fn build(self) -> Result<EndPoint>
pub async fn build(self) -> Result<EndPoint>
Builds and returns the configured endpoint.
This method consumes the builder and creates an EndPoint with all
the specified configuration options.
§Errors
Returns an error if:
- The node ID was not set
- Unable to bind to the specified ports
- Other configuration errors
§Examples
use rustp2p::Builder;
use std::net::Ipv4Addr;
let endpoint = Builder::new()
.node_id(Ipv4Addr::new(10, 0, 0, 1).into())
.udp_port(8080)
.build()
.await?;