Struct ckb_network::ServiceBuilder
source · pub struct ServiceBuilder { /* private fields */ }
Expand description
Builder for Service
Implementations§
source§impl ServiceBuilder
impl ServiceBuilder
sourcepub fn new() -> ServiceBuilder
pub fn new() -> ServiceBuilder
New a default empty builder
sourcepub fn build<H>(self, handle: H) -> Service<H>where
H: ServiceHandle + Unpin,
pub fn build<H>(self, handle: H) -> Service<H>where
H: ServiceHandle + Unpin,
Combine the configuration of this builder with service handle to create a Service.
sourcepub fn insert_protocol(self, protocol: ProtocolMeta) -> ServiceBuilder
pub fn insert_protocol(self, protocol: ProtocolMeta) -> ServiceBuilder
Insert a custom protocol
sourcepub fn key_pair(self, key_pair: SecioKeyPair) -> ServiceBuilder
pub fn key_pair(self, key_pair: SecioKeyPair) -> ServiceBuilder
Enable encrypted communication mode.
If you do not need encrypted communication, you do not need to call this method
sourcepub fn forever(self, forever: bool) -> ServiceBuilder
pub fn forever(self, forever: bool) -> ServiceBuilder
When the service has no tasks, it will be turned off by default. If you do not want to close service, set it to true.
sourcepub fn timeout(self, timeout: Duration) -> ServiceBuilder
pub fn timeout(self, timeout: Duration) -> ServiceBuilder
Timeout for handshake and connect
Default 10 second
sourcepub fn yamux_config(self, config: Config) -> ServiceBuilder
pub fn yamux_config(self, config: Config) -> ServiceBuilder
Yamux config for service
Panic when max_frame_length < yamux_max_window_size
sourcepub fn max_frame_length(self, size: usize) -> ServiceBuilder
pub fn max_frame_length(self, size: usize) -> ServiceBuilder
Secio max frame length
Panic when max_frame_length < yamux_max_window_size
sourcepub fn set_channel_size(self, size: usize) -> ServiceBuilder
pub fn set_channel_size(self, size: usize) -> ServiceBuilder
Tentacle use lots of bound channel, default channel size is 128
sourcepub fn set_send_buffer_size(self, size: usize) -> ServiceBuilder
pub fn set_send_buffer_size(self, size: usize) -> ServiceBuilder
Set send buffer size, default is 24Mb
sourcepub fn set_recv_buffer_size(self, size: usize) -> ServiceBuilder
pub fn set_recv_buffer_size(self, size: usize) -> ServiceBuilder
Set receive buffer size, default is 24Mb
sourcepub fn keep_buffer(self, keep: bool) -> ServiceBuilder
pub fn keep_buffer(self, keep: bool) -> ServiceBuilder
If session is close by remote, did you want to keep unreceived message as more as possible default is false
sourcepub fn upnp(self, enable: bool) -> ServiceBuilder
pub fn upnp(self, enable: bool) -> ServiceBuilder
Whether to allow tentative registration upnp, default is disable(false)
upnp: https://en.wikipedia.org/wiki/Universal_Plug_and_Play
Upnp is a simple solution to nat penetration, which requires routing support for registration mapping.
The function provided here is that if the external ip of the query route is a public network, then an attempt is made to register the local listener port into the mapping so that it can receive the access request of the external network, and if the external ip of the route is not the public network, Then do nothing
sourcepub fn max_connection_number(self, number: usize) -> ServiceBuilder
pub fn max_connection_number(self, number: usize) -> ServiceBuilder
The limit of max open connection(file descriptors) If not limited, service will try to serve as many connections as possible until it exhausts system resources(os error), and then close the listener, no longer accepting new connection requests, and the established connections remain working
Default is 65535
sourcepub fn tcp_config<F>(self, f: F) -> ServiceBuilder
pub fn tcp_config<F>(self, f: F) -> ServiceBuilder
Users can make their own custom configuration for all tcp socket at the bottom of Tentacle according to their own needs, for example, use reuse port to try to build nat penetration
In this way, any actively connected outbound connection is potentially connectable. Through this setting, the device after NAT can have the opportunity to be connected to the public network.
TCP Hole Punching: http://bford.info/pub/net/p2pnat/ STUN: https://tools.ietf.org/html/rfc5389
for example, set all tcp bind to 127.0.0.1:1080
, set keepalive:
use socket2;
use tentacle::{service::TcpSocket, builder::ServiceBuilder};
#[cfg(unix)]
use std::os::unix::io::{FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
use std::net::SocketAddr;
let mut server = ServiceBuilder::new();
server.tcp_config(|socket: TcpSocket| {
let socket = unsafe {
#[cfg(unix)]
let socket = socket2::Socket::from_raw_fd(socket.into_raw_fd());
#[cfg(windows)]
let socket = socket2::Socket::from_raw_socket(socket.into_raw_socket());
socket
};
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
socket.set_reuse_port(true)?;
socket.set_reuse_address(true)?;
socket.bind(&"127.0.0.1:1080".parse::<SocketAddr>().unwrap().into())?;
socket.set_keepalive(true)?;
let socket = unsafe {
#[cfg(unix)]
let socket = TcpSocket::from_raw_fd(socket.into_raw_fd());
#[cfg(windows)]
let socket = TcpSocket::from_raw_socket(socket.into_raw_socket());
socket
};
Ok(socket)
});
§Note
User use listen(2)
or connect(2)
on this closure will cause abnormal behavior