aboutsummaryrefslogtreecommitdiff
path: root/src/netapp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/netapp.rs')
-rw-r--r--src/netapp.rs34
1 files changed, 19 insertions, 15 deletions
diff --git a/src/netapp.rs b/src/netapp.rs
index 5847f98..86f14fe 100644
--- a/src/netapp.rs
+++ b/src/netapp.rs
@@ -24,12 +24,16 @@ use crate::util::*;
type DynMsg = Box<dyn Any + Send + Sync + 'static>;
+type OnConnectHandler = Box<dyn Fn(NodeID, SocketAddr, bool) + Send + Sync>;
+type OnDisconnectHandler = Box<dyn Fn(NodeID, bool) + Send + Sync>;
+
+pub(crate) type LocalHandler = Box<dyn Fn(DynMsg) -> Pin<Box<dyn Future<Output = DynMsg> + Sync + Send>> + Sync + Send>;
+pub(crate) type NetHandler = Box<
+ dyn Fn(NodeID, Bytes) -> Pin<Box<dyn Future<Output = Vec<u8>> + Sync + Send>> + Sync + Send>;
+
pub(crate) struct Handler {
- pub(crate) local_handler:
- Box<dyn Fn(DynMsg) -> Pin<Box<dyn Future<Output = DynMsg> + Sync + Send>> + Sync + Send>,
- pub(crate) net_handler: Box<
- dyn Fn(NodeID, Bytes) -> Pin<Box<dyn Future<Output = Vec<u8>> + Sync + Send>> + Sync + Send,
- >,
+ pub(crate) local_handler: LocalHandler,
+ pub(crate) net_handler: NetHandler,
}
/// NetApp is the main class that handles incoming and outgoing connections.
@@ -38,9 +42,9 @@ pub(crate) struct Handler {
/// an outgoing connection, or to ourself. On the server side, these messages are
/// processed by the handlers that have been defined using `add_msg_handler()`.
///
-/// NetApp can be used in a stand-alone fashion or together with a peering strategy.
-/// If using it alone, you will want to set `on_connect` and `on_disconnect` events
-/// in order to manage information about the current peer list.
+/// NetApp can be used in a stand-alone fashion or together with a peering strategy.
+/// If using it alone, you will want to set `on_connect` and `on_disconnect` events
+/// in order to manage information about the current peer list.
///
/// It is generally not necessary to use NetApp stand-alone, as the provided full mesh
/// and RPS peering strategies take care of the most common use cases.
@@ -58,8 +62,8 @@ pub struct NetApp {
client_conns: RwLock<HashMap<NodeID, Arc<ClientConn>>>,
pub(crate) msg_handlers: ArcSwap<HashMap<MessageKind, Arc<Handler>>>,
- on_connected_handler: ArcSwapOption<Box<dyn Fn(NodeID, SocketAddr, bool) + Send + Sync>>,
- on_disconnected_handler: ArcSwapOption<Box<dyn Fn(NodeID, bool) + Send + Sync>>,
+ on_connected_handler: ArcSwapOption<OnConnectHandler>,
+ on_disconnected_handler: ArcSwapOption<OnDisconnectHandler>,
}
struct ListenParams {
@@ -90,7 +94,7 @@ where
hex::encode(remote),
(end_time - begin_time).as_millis()
);
- rmp_to_vec_all_named(&res).unwrap_or(vec![])
+ rmp_to_vec_all_named(&res).unwrap_or_default()
}
async fn local_handler_aux<M, F, R>(handler: Arc<F>, remote: NodeID, msg: DynMsg) -> DynMsg
@@ -128,7 +132,7 @@ impl NetApp {
let netapp2 = netapp.clone();
netapp.add_msg_handler::<HelloMessage, _, _>(move |from: NodeID, msg: HelloMessage| {
netapp2.handle_hello_message(from, msg);
- async { () }
+ async { }
});
netapp
@@ -175,7 +179,7 @@ impl NetApp {
fun
});
- let self_id = self.id.clone();
+ let self_id = self.id;
let local_handler = Box::new(move |msg: DynMsg| {
let fun: Pin<Box<dyn Future<Output = DynMsg> + Sync + Send>> =
Box::pin(local_handler_aux(handler.clone(), self_id, msg));
@@ -248,7 +252,7 @@ impl NetApp {
let socket = TcpStream::connect(ip).await?;
info!("Connected to {}, negotiating handshake...", ip);
- ClientConn::init(self, socket, id.clone()).await?;
+ ClientConn::init(self, socket, id).await?;
Ok(())
}
@@ -315,7 +319,7 @@ impl NetApp {
fn handle_hello_message(&self, id: NodeID, msg: HelloMessage) {
if let Some(h) = self.on_connected_handler.load().as_ref() {
if let Some(c) = self.server_conns.read().unwrap().get(&id) {
- let remote_ip = msg.server_addr.unwrap_or(c.remote_addr.ip());
+ let remote_ip = msg.server_addr.unwrap_or_else(|| c.remote_addr.ip());
let remote_addr = SocketAddr::new(remote_ip, msg.server_port);
h(id, remote_addr, true);
}