Enum rtsp_types::Message
source · pub enum Message<Body> {
Request(Request<Body>),
Response(Response<Body>),
Data(Data<Body>),
}Expand description
Variants§
Request(Request<Body>)
Request message
Response(Response<Body>)
Response message
Data(Data<Body>)
Data message
Implementations§
source§impl<Body: AsRef<[u8]>> Message<Body>
impl<Body: AsRef<[u8]>> Message<Body>
sourcepub fn write<'b, W: Write + 'b>(&self, w: &'b mut W) -> Result<(), WriteError>
pub fn write<'b, W: Write + 'b>(&self, w: &'b mut W) -> Result<(), WriteError>
Serialize the message to any std::io::Write.
Resuming writing after std::io::ErrorKind::WouldBlock is not supported. Any previously
written data will have to be discarded for resuming.
§Serializing an RTSP message
let request = rtsp_types::Request::builder(
rtsp_types::Method::SetParameter,
rtsp_types::Version::V2_0
)
.request_uri(rtsp_types::Url::parse("rtsp://example.com/test").expect("Invalid URI"))
.header(rtsp_types::headers::CSEQ, "2")
.header(rtsp_types::headers::CONTENT_TYPE, "text/parameters")
.build(Vec::from(&b"barparam: barstuff"[..]));
let mut data = Vec::new();
request.write(&mut data).expect("Failed to serialize request");
assert_eq!(
data,
b"SET_PARAMETER rtsp://example.com/test RTSP/2.0\r\n\
Content-Length: 18\r\n\
Content-Type: text/parameters\r\n\
CSeq: 2\r\n\
\r\n\
barparam: barstuff",
);source§impl<'a, T: From<&'a [u8]>> Message<T>
impl<'a, T: From<&'a [u8]>> Message<T>
sourcepub fn parse<B: AsRef<[u8]> + 'a + ?Sized>(
buf: &'a B,
) -> Result<(Self, usize), ParseError>
pub fn parse<B: AsRef<[u8]> + 'a + ?Sized>( buf: &'a B, ) -> Result<(Self, usize), ParseError>
Try parse a message from a &[u8] and also return how many bytes were consumed.
The body type of the returned message can be any type that implements From<&[u8]>. This
includes Vec<u8> and &[u8] among others.
If parsing the message succeeds, in addition to the message itself also the number of bytes that were consumed from the input data are returned. This allows the caller to advance to the next message.
If parsing the message fails with ParseError::Incomplete
then the caller has to provide more data for successfully parsing the message.
Otherwise, if parsing the message fails with ParseError::Error
then the message can’t be parsed and the caller can try skipping over some data until
parsing succeeds again.
§Parsing an RTSP message
let data = b"OPTIONS * RTSP/2.0\r\n\
CSeq: 1\r\n\
Supported: play.basic, play.scale\r\n\
User-Agent: PhonyClient/1.2\r\n\
\r\n";
let (message, consumed): (rtsp_types::Message<Vec<u8>>, _) =
rtsp_types::Message::parse(data).expect("Failed to parse data");
assert_eq!(consumed, data.len());
match message {
rtsp_types::Message::Request(ref request) => {
assert_eq!(request.method(), rtsp_types::Method::Options);
},
_ => unreachable!(),
}