1#![allow(missing_docs)]
8
9use std::collections::HashMap;
15use std::time::Duration;
16use thiserror::Error;
17
18use crate::nat_traversal::{NatTraversalEndpoint, NatTraversalError, PeerId};
19
20pub mod config;
22pub use config::ConfigError;
23pub use config::P2PConfig;
24pub use config::P2PConfigBuilder;
25
26pub struct P2PNode {
28 #[allow(dead_code)]
30 endpoint: NatTraversalEndpoint,
31 #[allow(dead_code)]
33 config: P2PConfig,
34 #[allow(dead_code)]
36 connections: HashMap<PeerId, P2PConnection>,
37 #[allow(dead_code)]
39 events: Vec<P2PEvent>,
40}
41
42pub struct P2PConnection {
44 #[allow(dead_code)]
46 peer_id: PeerId,
47 #[allow(dead_code)]
49 state: ConnectionState,
50 #[allow(dead_code)]
52 stats: ConnectionStats,
53}
54
55enum ConnectionState {
57 #[allow(dead_code)]
58 Connecting,
59 #[allow(dead_code)]
60 Connected,
61 #[allow(dead_code)]
62 Disconnecting,
63 #[allow(dead_code)]
64 Disconnected,
65}
66
67pub struct ConnectionStats {
69 #[allow(dead_code)]
71 rtt: Duration,
72 #[allow(dead_code)]
74 bytes_sent: u64,
75 #[allow(dead_code)]
77 bytes_received: u64,
78 #[allow(dead_code)]
80 packets_sent: u64,
81 #[allow(dead_code)]
83 packets_received: u64,
84}
85
86pub enum P2PEvent {
88 Connected { peer_id: PeerId },
90 Disconnected {
92 peer_id: PeerId,
93 reason: Option<String>,
94 },
95 Data { peer_id: PeerId, data: Vec<u8> },
97 Error {
99 peer_id: Option<PeerId>,
101 error: P2PError,
103 },
104}
105
106#[derive(Debug, Error)]
108pub enum P2PError {
109 #[error("Connection error: {0}")]
111 Connection(String),
112
113 #[error("Authentication error: {0}")]
114 Authentication(String),
115
116 #[error("NAT traversal error: {0}")]
117 NatTraversal(#[from] NatTraversalError),
118
119 #[error("Configuration error: {0}")]
120 Configuration(String),
121
122 #[error("Timeout: {0}")]
123 Timeout(String),
124}
125
126