1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Core connection trait and state management.
use crate::error::Result;
use async_trait::async_trait;
use std::fmt;
/// Trait for KNX/IP connections
#[async_trait]
pub trait Connection: Send + Sync {
/// Send raw frame data
async fn send(&self, frame: &[u8]) -> Result<()>;
/// Receive raw frame data
async fn recv(&self) -> Result<Vec<u8>>;
/// Close the connection
async fn close(&self) -> Result<()>;
/// Get current connection state
fn state(&self) -> ConnectionState;
/// Get connection statistics
fn stats(&self) -> ConnectionStats;
/// Get reference to underlying concrete type (for downcasting)
fn as_any(&self) -> &dyn std::any::Any;
}
/// Connection state enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
/// Connection is being established
Connecting,
/// Connection is active and ready
Connected,
/// Connection is being closed
Disconnecting,
/// Connection is closed
Disconnected,
/// Connection failed
Failed,
}
impl fmt::Display for ConnectionState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConnectionState::Connecting => write!(f, "connecting"),
ConnectionState::Connected => write!(f, "connected"),
ConnectionState::Disconnecting => write!(f, "disconnecting"),
ConnectionState::Disconnected => write!(f, "disconnected"),
ConnectionState::Failed => write!(f, "failed"),
}
}
}
/// Connection statistics
#[derive(Debug, Clone, Default)]
pub struct ConnectionStats {
/// Number of frames sent
pub frames_sent: u64,
/// Number of frames received
pub frames_received: u64,
/// Number of send errors
pub send_errors: u64,
/// Number of receive errors
pub recv_errors: u64,
/// Connection uptime in seconds
pub uptime_seconds: u64,
/// Last error message
pub last_error: Option<String>,
}