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
use std::{
    collections::VecDeque,
    time::{Duration, Instant},
};

use crate::{
    connection::ConnectionSettings,
    packet::*,
    protocol::time::{TimeBase, Timer},
};

#[derive(Debug)]
pub struct Output {
    remote_sockid: SocketId,
    time_base: TimeBase,
    packets: VecDeque<Packet>,
    keepalive: Timer,
}

impl Output {
    pub fn new(settings: &ConnectionSettings) -> Self {
        Self {
            remote_sockid: settings.remote_sockid,
            time_base: TimeBase::new(settings.socket_start_time),
            packets: VecDeque::new(),
            keepalive: Timer::new(settings.socket_start_time, Duration::from_secs(1)),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.packets.is_empty()
    }

    pub fn send_control(&mut self, now: Instant, control: ControlTypes) {
        self.keepalive.reset(now);
        self.packets.push_back(Packet::Control(ControlPacket {
            timestamp: self.time_base.timestamp_from(now),
            dest_sockid: self.remote_sockid,
            control_type: control,
        }));
    }

    pub fn send_data(&mut self, now: Instant, data: DataPacket) {
        self.keepalive.reset(now);
        self.packets.push_back(Packet::Data(data));
    }

    pub fn ensure_alive(&mut self, now: Instant) {
        if self.keepalive.check_expired(now).is_some() {
            self.send_control(now, ControlTypes::KeepAlive)
        }
    }

    pub fn pop_packet(&mut self) -> Option<Packet> {
        self.packets.pop_front()
    }
}