1use std::time::Duration;
7use std::collections::HashMap;
8use thiserror::Error;
9
10use crate::nat_traversal::{
11 NatTraversalEndpoint,
12 PeerId,
13 NatTraversalError,
14};
15
16
17pub mod config;
19pub use config::P2PConfig;
20pub use config::P2PConfigBuilder;
21pub use config::ConfigError;
22
23pub struct P2PNode {
25 endpoint: NatTraversalEndpoint,
27 config: P2PConfig,
29 connections: HashMap<PeerId, P2PConnection>,
31 events: Vec<P2PEvent>,
33}
34
35pub struct P2PConnection {
37 peer_id: PeerId,
39 state: ConnectionState,
41 stats: ConnectionStats,
43}
44
45enum ConnectionState {
47 Connecting,
48 Connected,
49 Disconnecting,
50 Disconnected,
51}
52
53pub struct ConnectionStats {
55 rtt: Duration,
57 bytes_sent: u64,
59 bytes_received: u64,
61 packets_sent: u64,
63 packets_received: u64,
65}
66
67pub enum P2PEvent {
69 Connected {
71 peer_id: PeerId,
72 },
73 Disconnected {
75 peer_id: PeerId,
76 reason: Option<String>,
77 },
78 Data {
80 peer_id: PeerId,
81 data: Vec<u8>,
82 },
83 Error {
85 peer_id: Option<PeerId>,
86 error: P2PError,
87 },
88}
89
90#[derive(Debug, Error)]
92pub enum P2PError {
93 #[error("Connection error: {0}")]
94 Connection(String),
95
96 #[error("Authentication error: {0}")]
97 Authentication(String),
98
99 #[error("NAT traversal error: {0}")]
100 NatTraversal(#[from] NatTraversalError),
101
102 #[error("Configuration error: {0}")]
103 Configuration(String),
104
105 #[error("Timeout: {0}")]
106 Timeout(String),
107}
108
109