sheave_core/handshake/
version.rs

1/// Bytes to indicate Flash Player version/Flash Media Server version.
2///
3/// This is used for indicating whether doing handshake with HMAC-SHA256 digest/signature.
4/// If you do handshake with HMAC-SHA256 as a client, set major version and above 9.
5/// If you do it as a server, set major version and above 3.
6/// If otherwise, set major version below 9/3, or you can set 0.
7///
8/// Because of handshake specification, note any value above `0xff` cannot set as a version. Such as a last byte of Flash Player version.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct Version(u8, u8, u8, u8);
11
12impl Version {
13    /// Bytes meant not to use HMAC-SHA256.
14    pub const UNSIGNED: Self = Self(0, 0, 0, 0);
15    /// The latest version of Flash Player.
16    pub const LATEST_CLIENT: Self = Self(32, 0, 0, 0);
17    /// The latest version of Flash Media Server.
18    pub const LATEST_SERVER: Self = Self(5, 0, 17, 0);
19
20    /// Gets a number of major version either Flash Player or Flash Media Server.
21    ///
22    /// # Examples
23    ///
24    /// ```rust
25    /// use sheave_core::handshake::Version;
26    ///
27    /// assert_eq!(0, Version::UNSIGNED.get_major_version());
28    /// assert_eq!(32, Version::LATEST_CLIENT.get_major_version());
29    /// assert_eq!(5, Version::LATEST_SERVER.get_major_version())
30    /// ```
31    ///
32    /// The well-known RTMP applications check just this version to decide whether they do handshake with HMAC-SHA256.
33    pub fn get_major_version(&self) -> u8 {
34        self.0
35    }
36}
37
38impl From<[u8; 4]> for Version {
39    fn from(version_bytes: [u8; 4]) -> Self {
40        Self(version_bytes[0], version_bytes[1], version_bytes[2], version_bytes[3])
41    }
42}
43
44impl From<Version> for [u8; 4] {
45    fn from(version: Version) -> Self {
46        let mut version_bytes: [u8; 4] = [0; 4];
47        version_bytes[0] = version.0;
48        version_bytes[1] = version.1;
49        version_bytes[2] = version.2;
50        version_bytes[3] = version.3;
51        version_bytes
52    }
53}