karyon_p2p/lib.rs
1//! A lightweight, extensible, and customizable peer-to-peer (p2p) network stack.
2//!
3//! # Example
4//! ```
5//! use std::sync::Arc;
6//!
7//! use easy_parallel::Parallel;
8//! use smol::{future, Executor};
9//!
10//! use karyon_p2p::{Backend, Config, PeerID, keypair::{KeyPair, KeyPairType}};
11//!
12//! // Generate a new keypair for the peer
13//! let key_pair = KeyPair::generate(&KeyPairType::Ed25519);
14//!
15//! // Create the configuration for the backend.
16//! let mut config = Config::default();
17//!
18//! // Create a new Executor
19//! let ex = Arc::new(Executor::new());
20//!
21//! // Create a new Backend
22//! let backend = Backend::new(&key_pair, config, ex.clone().into());
23//!
24//! let task = async {
25//! // Run the backend
26//! backend.run()
27//! .await
28//! .expect("start the backend");
29//!
30//! // ....
31//!
32//! // Shutdown the backend
33//! backend.shutdown().await;
34//! };
35//!
36//! future::block_on(ex.run(task));
37//!
38//! ```
39//!
40mod backend;
41mod codec;
42mod config;
43mod conn_queue;
44mod connection;
45mod connector;
46mod discovery;
47mod error;
48mod listener;
49mod message;
50mod peer;
51mod peer_pool;
52mod protocols;
53mod routing_table;
54mod slots;
55mod tls_config;
56mod version;
57
58/// Responsible for network and system monitoring.
59/// [`Read More`](./monitor/struct.Monitor.html)
60pub mod monitor;
61/// Defines the protocol trait.
62/// [`Read More`](./protocol/trait.Protocol.html)
63pub mod protocol;
64
65pub use backend::Backend;
66pub use config::Config;
67pub use peer::{Peer, PeerID};
68pub use version::Version;
69
70pub mod endpoint {
71 pub use karyon_net::{Addr, Endpoint, Port};
72}
73
74pub mod keypair {
75 pub use karyon_core::crypto::{KeyPair, KeyPairType, PublicKey, SecretKey};
76}
77
78pub use error::{Error, Result};
79
80type ListenerRef = karyon_net::Listener<message::NetMsg, Error>;
81type ConnRef = karyon_net::Conn<message::NetMsg, Error>;