Struct msgpacknet::CloseGuard
[−]
[src]
pub struct CloseGuard<M: Message, N: NodeId, I: InitMessage>(_);
A guard that closes the node when dropped.
Implementation details require the node to exist in multiple reference counted copies. Therefore this wrapper is needed to implement the drop behavior. Except for this drop behavior this struct can be used like the node struct that it encapsulates.
Methods from Deref<Target=Node<M, N, I>>
fn set_connection_timeout(&self, dur: Duration)
Set the connection timeout
Whenever a connection is idle longer than this timeout, it will be closed. The default value is 60 seconds.
Note: Changes to this value only affect connections created after the change.
fn connection_timeout(&self) -> Duration
The connection timeout
fn set_stats_halflife_time(&self, dur: Duration)
Set the statistics half-life time
This value is used as a parameter for the rolling average statistics of sending and receiving rates. Shorter durations react faster to changes and longer durations result in smoother behavior. The default value is 60 seconds.
Note: Changes to this value only affect connections created after the change.
fn stats_halflife_time(&self) -> Duration
The statistics half-life time
fn set_init_message(&self, init: I)
Set the initialization message
This message is sent as a first message on all new connections and must identify the node.
fn init_message(&self) -> I
The initialization message
fn listen<A: ToSocketAddrs>(&self, addr: A) -> Result<SocketAddr, Error<N>>
Listen on the specified address
This method will open a new node listening to the given address. A dedicated thread will be started to handle incoming connections. The node can listen on multiple addresses. The result of this method, if successful, is the actual address used.
Note: A port of 0
has the special meaning of taking a random free port. If the host part
of the address is "0.0.0.0"
or "[::0]"
the socket will be opened listening on
all available IPv4 or IPv6 addresses.
fn listen_defaults(&self) -> Result<(), Error<N>>
Listen on the addresses "0.0.0.0:0"
and "[::0]:0"
This will open sockets for IPv4 and IPv6 (see
listen(...)
for details).
Note: This method will always open two new sockets, regardless of whether sockets are already open.
fn addresses(&self) -> Vec<SocketAddr>
The addresses of this node
fn receive(&self) -> Event<M, N, I>
Receive an event
This method returns the next event received. If events are queued, the oldest queued event will be returned. Otherwise, the call will block until an event is received.
If the node has been closed, Event::Closed
will be returned immediately.
fn node_id(&self) -> N
The id of this node
fn is_connected(&self, id: &N) -> bool
Whether this node is connected to the given node
fn connection_count(&self) -> usize
The number of fully established connections
fn stats(&self) -> NodeStats<N>
The statistics of this node
fn send(&self, dst: &N, msg: &M) -> Result<(), Error<N>>
Send a message
This method sends a message to the given destination. The message must be encodable using
serde
and the node must be connected to the destination.
It is possible to send messages to the node itself by using its address.
If no connection to the destination exists, an error is returned.
fn connect_request<A: ToSocketAddrs>(&self, addr: A) -> Result<ConnectionRequest<M, N, I>, Error<N>>
Open a connection and return the request
This method opens a connection, exchanges initialization messages with the peer and returns
a connection request. This request object can then be used to inspect the node id and the
initialization message and then decide whether to accept()
or reject()
the connection.
This method blocks until the underlying connection has been established and the
initialization messages have been exchanged.
For more details see connect(...)
.
fn connect<A: ToSocketAddrs>(&self, addr: A) -> Result<N, Error<N>>
Open a connection and accept it automatically
This method opens a connection, exchanges initialization messages with the peer, automatically accepts the peer and returns its node_id. This method blocks until the underlying connection has been established and the initialization messages have been exchanged.
Note: This connection will automatically be closed when it becomes idle for longer than the specified timeout.
Note 2: It is possible to connect the node to itself. However, messages will just be short-circuited and the connection will not be used and closed after the timeout.
fn lookup_connection<A: ToSocketAddrs>(&self, addr: A) -> Option<N>
Returns the node id for an address if connected
This method retrns the node id for an address if a connection to this address exists.