2023-07-08 16:13:29 +00:00
|
|
|
use super::{Role, Version};
|
2023-08-23 22:28:27 +00:00
|
|
|
use crate::{
|
|
|
|
coding::{DecodeError, EncodeError},
|
|
|
|
VarInt,
|
|
|
|
};
|
2023-07-08 16:13:29 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
use crate::coding::{AsyncRead, AsyncWrite};
|
2023-07-08 16:13:29 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
/// Sent by the server in response to a client setup.
|
2023-07-08 16:13:29 +00:00
|
|
|
// NOTE: This is not a message type, but rather the control stream header.
|
|
|
|
// Proposal: https://github.com/moq-wg/moq-transport/issues/138
|
|
|
|
#[derive(Debug)]
|
2023-08-15 17:20:03 +00:00
|
|
|
pub struct Server {
|
2023-09-15 19:06:28 +00:00
|
|
|
/// The list of supported versions in preferred order.
|
2023-07-08 16:13:29 +00:00
|
|
|
pub version: Version,
|
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
/// Indicate if the server is a publisher, a subscriber, or both.
|
2023-07-08 16:13:29 +00:00
|
|
|
// Proposal: moq-wg/moq-transport#151
|
|
|
|
pub role: Role,
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:20:03 +00:00
|
|
|
impl Server {
|
2023-09-15 19:06:28 +00:00
|
|
|
/// Decode the server setup.
|
|
|
|
pub async fn decode<R: AsyncRead>(r: &mut R) -> Result<Self, DecodeError> {
|
2023-08-23 22:28:27 +00:00
|
|
|
let typ = VarInt::decode(r).await?;
|
|
|
|
if typ.into_inner() != 2 {
|
|
|
|
return Err(DecodeError::InvalidType(typ));
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:20:03 +00:00
|
|
|
let version = Version::decode(r).await?;
|
|
|
|
let role = Role::decode(r).await?;
|
2023-07-08 16:13:29 +00:00
|
|
|
|
|
|
|
Ok(Self { version, role })
|
|
|
|
}
|
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
/// Encode the server setup.
|
|
|
|
pub async fn encode<W: AsyncWrite>(&self, w: &mut W) -> Result<(), EncodeError> {
|
2023-08-23 22:28:27 +00:00
|
|
|
VarInt::from_u32(2).encode(w).await?;
|
2023-08-15 17:20:03 +00:00
|
|
|
self.version.encode(w).await?;
|
|
|
|
self.role.encode(w).await?;
|
2023-07-08 16:13:29 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|