1use std::collections::HashMap;
7use std::time::Duration;
8use thiserror::Error;
9
10use crate::nat_traversal::{NatTraversalEndpoint, NatTraversalError, PeerId};
11
12pub mod config;
14pub use config::ConfigError;
15pub use config::P2PConfig;
16pub use config::P2PConfigBuilder;
17
18pub struct P2PNode {
20 endpoint: NatTraversalEndpoint,
22 config: P2PConfig,
24 connections: HashMap<PeerId, P2PConnection>,
26 events: Vec<P2PEvent>,
28}
29
30pub struct P2PConnection {
32 peer_id: PeerId,
34 state: ConnectionState,
36 stats: ConnectionStats,
38}
39
40enum ConnectionState {
42 Connecting,
43 Connected,
44 Disconnecting,
45 Disconnected,
46}
47
48pub struct ConnectionStats {
50 rtt: Duration,
52 bytes_sent: u64,
54 bytes_received: u64,
56 packets_sent: u64,
58 packets_received: u64,
60}
61
62pub enum P2PEvent {
64 Connected { peer_id: PeerId },
66 Disconnected {
68 peer_id: PeerId,
69 reason: Option<String>,
70 },
71 Data { peer_id: PeerId, data: Vec<u8> },
73 Error {
75 peer_id: Option<PeerId>,
77 error: P2PError,
79 },
80}
81
82#[derive(Debug, Error)]
84pub enum P2PError {
85 #[error("Connection error: {0}")]
87 Connection(String),
88
89 #[error("Authentication error: {0}")]
90 Authentication(String),
91
92 #[error("NAT traversal error: {0}")]
93 NatTraversal(#[from] NatTraversalError),
94
95 #[error("Configuration error: {0}")]
96 Configuration(String),
97
98 #[error("Timeout: {0}")]
99 Timeout(String),
100}
101
102