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
#[cfg(test)]
mod message_test;
pub mod message_channel_ack;
pub mod message_channel_open;
pub mod message_type;
use message_channel_ack::*;
use message_channel_open::*;
use message_type::*;
use crate::error::Error;
use util::marshal::*;
use bytes::{Buf, BufMut};
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum Message {
DataChannelAck(DataChannelAck),
DataChannelOpen(DataChannelOpen),
}
impl MarshalSize for Message {
fn marshal_size(&self) -> usize {
match self {
Message::DataChannelAck(m) => m.marshal_size() + MESSAGE_TYPE_LEN,
Message::DataChannelOpen(m) => m.marshal_size() + MESSAGE_TYPE_LEN,
}
}
}
impl Marshal for Message {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize, util::Error> {
let mut bytes_written = 0;
let n = self.message_type().marshal_to(buf)?;
buf = &mut buf[n..];
bytes_written += n;
bytes_written += match self {
Message::DataChannelAck(_) => 0,
Message::DataChannelOpen(open) => open.marshal_to(buf)?,
};
Ok(bytes_written)
}
}
impl Unmarshal for Message {
fn unmarshal<B>(buf: &mut B) -> Result<Self, util::Error>
where
Self: Sized,
B: Buf,
{
if buf.remaining() < MESSAGE_TYPE_LEN {
return Err(Error::UnexpectedEndOfBuffer {
expected: MESSAGE_TYPE_LEN,
actual: buf.remaining(),
}
.into());
}
match MessageType::unmarshal(buf)? {
MessageType::DataChannelAck => Ok(Self::DataChannelAck(DataChannelAck {})),
MessageType::DataChannelOpen => {
Ok(Self::DataChannelOpen(DataChannelOpen::unmarshal(buf)?))
}
}
}
}
impl Message {
pub fn message_type(&self) -> MessageType {
match self {
Self::DataChannelAck(_) => MessageType::DataChannelAck,
Self::DataChannelOpen(_) => MessageType::DataChannelOpen,
}
}
}