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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use super::{CallbackFn, PartialStream, PartialStreamError};
use crate::packet::{ContinuePacket, Packet, PlayerScope, Scope, StartPacket};
use std::{
    collections::HashMap,
    time::{Duration, Instant},
};
use tracing::{debug, error, warn};

#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    #[error("got non-Player scope")]
    Thing,

    #[error("found continue packet before start")]
    Thing2,

    #[error(transparent)]
    PartialStream(#[from] PartialStreamError),

    #[error(transparent)]
    Io(#[from] std::io::Error),
}
type Result<T> = std::result::Result<T, StoreError>;

#[derive(Default)]
pub(crate) struct Store {
    pub(crate) event_handlers: Vec<CallbackFn>,
    streams: HashMap<u8, PartialStream>,
    cleanup_times: HashMap<u8, Instant>,
}

impl Store {
    const STREAM_TIMEOUT: Duration = Duration::from_secs(10);

    pub(crate) fn process_packet(&mut self, packet: Packet) -> Result<()> {
        debug!("process_packet {:?}", packet);

        let finished_stream = match packet {
            Packet::Start(StartPacket {
                stream_id,
                scope,
                data_length,
                data_part,
            }) => {
                if let Scope::Player(PlayerScope { player_id }) = scope {
                    debug!(stream_id, player_id, data_length, "new stream");

                    let mut stream = PartialStream {
                        player_id,
                        data_length,
                        data_buffer: Vec::with_capacity(data_length as usize),
                    };
                    stream.write_part(data_part)?;

                    if let Some(old_stream) = self.streams.remove(&stream_id) {
                        warn!("restarting stream {:?}", old_stream);
                    }
                    if stream.is_finished() {
                        Some(stream)
                    } else {
                        self.streams.insert(stream_id, stream);
                        self.cleanup_times
                            .insert(stream_id, Instant::now() + Self::STREAM_TIMEOUT);
                        None
                    }
                } else {
                    return Err(StoreError::Thing);
                }
            }

            Packet::Continue(ContinuePacket {
                stream_id,
                data_part,
            }) => {
                let is_finished = if let Some(stream) = self.streams.get_mut(&stream_id) {
                    stream.write_part(data_part)?;
                    debug!(
                        stream_id,
                        player_id = stream.player_id,
                        current_length = stream.data_buffer.len(),
                        data_length = stream.data_length,
                        "continue stream"
                    );

                    stream.is_finished()
                } else {
                    return Err(StoreError::Thing2);
                };

                if is_finished {
                    self.cleanup_times.remove(&stream_id);
                    Some(self.streams.remove(&stream_id).unwrap())
                } else {
                    None
                }
            }
        };

        if let Some(stream) = finished_stream {
            debug!("finished_stream");
            for f in &self.event_handlers {
                f(stream.player_id, &stream.data_buffer);
            }
        }

        Ok(())
    }

    pub(crate) fn tick(&mut self) {
        let now = Instant::now();
        let mut stream_ids_to_removes = self
            .cleanup_times
            .iter()
            .filter_map(|(stream_id, cleanup_time)| {
                if &now > cleanup_time {
                    Some(*stream_id)
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        for stream_id in stream_ids_to_removes.drain(..) {
            debug!(stream_id, "timed out, removing");
            self.cleanup_times.remove(&stream_id);
            self.streams.remove(&stream_id);
        }
    }
}