Skip to main content

srt/
connection.rs

1use std::{
2    net::SocketAddr,
3    time::{Duration, Instant},
4};
5
6use crate::protocol::handshake::Handshake;
7use crate::{SeqNumber, SocketID};
8
9#[derive(Clone, Debug)]
10pub struct Connection {
11    pub settings: ConnectionSettings,
12    pub handshake: Handshake,
13}
14
15#[derive(Debug, Clone, Copy)]
16pub struct ConnectionSettings {
17    /// The remote socket to send & receive to
18    pub remote: SocketAddr,
19
20    /// The socket id of the UDT entity on the other side
21    pub remote_sockid: SocketID,
22
23    /// The local UDT socket id
24    pub local_sockid: SocketID,
25
26    /// The time that this socket started at, used to develop timestamps
27    pub socket_start_time: Instant,
28
29    /// The first sequence number
30    pub init_seq_num: SeqNumber,
31
32    /// The maximum packet size
33    pub max_packet_size: u32,
34
35    /// The maxiumum flow size
36    pub max_flow_size: u32,
37
38    /// The TSBPD latency configured by the user.
39    /// Not necessarily the actual decided on latency, which
40    /// is the max of both side's respective latencies.
41    pub tsbpd_latency: Duration,
42}
43
44impl ConnectionSettings {
45    /// Timestamp in us
46    pub fn get_timestamp(&self, at: Instant) -> i32 {
47        let elapsed = at - self.socket_start_time;
48
49        elapsed.as_micros() as i32 // TODO: handle overflow here
50    }
51
52    /// Timestamp in us
53    pub fn get_timestamp_now(&self) -> i32 {
54        self.get_timestamp(Instant::now())
55    }
56}