netget/server/
connection.rs1use std::net::SocketAddr;
4use tokio::net::TcpStream;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct ConnectionId(u32);
9
10impl ConnectionId {
11 pub fn new(id: u32) -> Self {
14 Self(id)
15 }
16
17 pub fn as_u32(&self) -> u32 {
19 self.0
20 }
21
22 #[deprecated(note = "Use as_u32() instead - IDs are now u32")]
24 pub fn as_u64(&self) -> u64 {
25 self.0 as u64
26 }
27
28 pub fn from_string(s: &str) -> Option<Self> {
30 let s = s.trim();
31 let id_str = s.strip_prefix("conn-").unwrap_or(s);
32 id_str.parse::<u32>().ok().map(Self)
33 }
34}
35
36impl std::fmt::Display for ConnectionId {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 write!(f, "conn-{}", self.0)
39 }
40}
41
42pub struct Connection {
44 pub id: ConnectionId,
46 pub remote_addr: SocketAddr,
48 pub local_addr: SocketAddr,
50 pub stream: TcpStream,
52 pub bytes_sent: u64,
54 pub bytes_received: u64,
56}
57
58impl Connection {
59 pub fn new_with_id(
62 id: ConnectionId,
63 stream: TcpStream,
64 remote_addr: SocketAddr,
65 local_addr: SocketAddr,
66 ) -> Self {
67 Self {
68 id,
69 remote_addr,
70 local_addr,
71 stream,
72 bytes_sent: 0,
73 bytes_received: 0,
74 }
75 }
76
77 pub fn add_bytes_sent(&mut self, count: u64) {
79 self.bytes_sent += count;
80 }
81
82 pub fn add_bytes_received(&mut self, count: u64) {
84 self.bytes_received += count;
85 }
86}