Skip to content

Commit

Permalink
move data error to shared
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Dec 19, 2023
1 parent 6b2742d commit 7fe005b
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 118 deletions.
4 changes: 2 additions & 2 deletions data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ description = "DataChannel in Rust"
license = "MIT/Apache-2.0"

[dependencies]
util = { version = "0.7.0", package = "webrtc-util", default-features = false, features = ["conn", "marshal"] }
sctp = { version = "0.8.0", package = "webrtc-sctp" }
shared = { path = "../shared", package = "shared", default-features = false, features = ["marshal"] }
sctp = { path = "../sctp", package = "sctp" }

tokio = { version = "1.19", features = ["full"] }
bytes = "1"
Expand Down
4 changes: 2 additions & 2 deletions data/src/data_channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use bytes::{Buf, Bytes};
use sctp::association::Association;
use sctp::chunk::chunk_payload_data::PayloadProtocolIdentifier;
use sctp::stream::*;
use shared::marshal::*;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use util::marshal::*;

use crate::error::{Error, Result};
use crate::message::message_channel_ack::*;
use crate::message::message_channel_open::*;
use crate::message::*;
use shared::error::{Error, Result};

const RECEIVE_MTU: usize = 8192;

Expand Down
72 changes: 0 additions & 72 deletions data/src/error.rs

This file was deleted.

5 changes: 1 addition & 4 deletions data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![warn(rust_2018_idioms)]
#![allow(dead_code)]

pub mod data_channel;
mod error;
//pub mod data_channel;
pub mod message;

pub use error::Error;
2 changes: 1 addition & 1 deletion data/src/message/message_channel_ack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

type Result<T> = std::result::Result<T, util::Error>;
type Result<T> = std::result::Result<T, shared::error::Error>;

/// The data-part of an data-channel ACK message without the message type.
///
Expand Down
37 changes: 15 additions & 22 deletions data/src/message/message_channel_open.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::*;
use crate::error::Error;

type Result<T> = std::result::Result<T, util::Error>;
use shared::error::{Error, Result};

const CHANNEL_TYPE_RELIABLE: u8 = 0x00;
const CHANNEL_TYPE_RELIABLE_UNORDERED: u8 = 0x80;
Expand Down Expand Up @@ -65,8 +63,7 @@ impl Marshal for ChannelType {
return Err(Error::UnexpectedEndOfBuffer {
expected: required_len,
actual: buf.remaining_mut(),
}
.into());
});
}

let byte = match self {
Expand Down Expand Up @@ -95,8 +92,7 @@ impl Unmarshal for ChannelType {
return Err(Error::UnexpectedEndOfBuffer {
expected: required_len,
actual: buf.remaining(),
}
.into());
});
}

let b0 = buf.get_u8();
Expand All @@ -112,7 +108,7 @@ impl Unmarshal for ChannelType {
CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED_UNORDERED => {
Ok(Self::PartialReliableTimedUnordered)
}
_ => Err(Error::InvalidChannelType(b0).into()),
_ => Err(Error::InvalidChannelType(b0)),
}
}
}
Expand Down Expand Up @@ -167,8 +163,7 @@ impl Marshal for DataChannelOpen {
return Err(Error::UnexpectedEndOfBuffer {
expected: required_len,
actual: buf.remaining_mut(),
}
.into());
});
}

let n = self.channel_type.marshal_to(buf)?;
Expand All @@ -193,8 +188,7 @@ impl Unmarshal for DataChannelOpen {
return Err(Error::UnexpectedEndOfBuffer {
expected: required_len,
actual: buf.remaining(),
}
.into());
});
}

let channel_type = ChannelType::unmarshal(buf)?;
Expand All @@ -208,8 +202,7 @@ impl Unmarshal for DataChannelOpen {
return Err(Error::UnexpectedEndOfBuffer {
expected: required_len,
actual: buf.remaining(),
}
.into());
});
}

let mut label = vec![0; label_len];
Expand Down Expand Up @@ -249,7 +242,7 @@ mod tests {
match ChannelType::unmarshal(&mut bytes) {
Ok(_) => panic!("expected Error, but got Ok"),
Err(err) => {
if let Some(&Error::InvalidChannelType(0x11)) = err.downcast_ref::<Error>() {
if Error::InvalidChannelType(0x11) == err {
return Ok(());
}
panic!(
Expand All @@ -267,10 +260,10 @@ mod tests {
match ChannelType::unmarshal(&mut bytes) {
Ok(_) => panic!("expected Error, but got Ok"),
Err(err) => {
if let Some(&Error::UnexpectedEndOfBuffer {
if (Error::UnexpectedEndOfBuffer {
expected: 1,
actual: 0,
}) = err.downcast_ref::<Error>()
}) == err
{
return Ok(());
}
Expand Down Expand Up @@ -341,7 +334,7 @@ mod tests {
match DataChannelOpen::unmarshal(&mut bytes) {
Ok(_) => panic!("expected Error, but got Ok"),
Err(err) => {
if let Some(&Error::InvalidChannelType(0x11)) = err.downcast_ref::<Error>() {
if Error::InvalidChannelType(0x11) == err {
return Ok(());
}
panic!(
Expand All @@ -359,10 +352,10 @@ mod tests {
match DataChannelOpen::unmarshal(&mut bytes) {
Ok(_) => panic!("expected Error, but got Ok"),
Err(err) => {
if let Some(&Error::UnexpectedEndOfBuffer {
if (Error::UnexpectedEndOfBuffer {
expected: 11,
actual: 5,
}) = err.downcast_ref::<Error>()
}) == err
{
return Ok(());
}
Expand All @@ -387,10 +380,10 @@ mod tests {
match DataChannelOpen::unmarshal(&mut bytes) {
Ok(_) => panic!("expected Error, but got Ok"),
Err(err) => {
if let Some(&Error::UnexpectedEndOfBuffer {
if (Error::UnexpectedEndOfBuffer {
expected: 13,
actual: 0,
}) = err.downcast_ref::<Error>()
}) == err
{
return Ok(());
}
Expand Down
2 changes: 1 addition & 1 deletion data/src/message/message_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bytes::{Bytes, BytesMut};

use super::*;
use crate::error::Result;
use shared::error::Result;

#[test]
fn test_message_unmarshal_open_success() {
Expand Down
11 changes: 4 additions & 7 deletions data/src/message/message_type.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use super::*;
use crate::error::Error;
use shared::error::{Error, Result};

// The first byte in a `Message` that specifies its type:
pub(crate) const MESSAGE_TYPE_ACK: u8 = 0x02;
pub(crate) const MESSAGE_TYPE_OPEN: u8 = 0x03;
pub(crate) const MESSAGE_TYPE_LEN: usize = 1;

type Result<T> = std::result::Result<T, util::Error>;

// A parsed DataChannel message
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum MessageType {
Expand Down Expand Up @@ -44,16 +42,15 @@ impl Unmarshal for MessageType {
return Err(Error::UnexpectedEndOfBuffer {
expected: required_len,
actual: buf.remaining(),
}
.into());
});
}

let b = buf.get_u8();

match b {
MESSAGE_TYPE_ACK => Ok(Self::DataChannelAck),
MESSAGE_TYPE_OPEN => Ok(Self::DataChannelOpen),
_ => Err(Error::InvalidMessageType(b).into()),
_ => Err(Error::InvalidMessageType(b)),
}
}
}
Expand Down Expand Up @@ -89,7 +86,7 @@ mod tests {
match MessageType::unmarshal(&mut bytes) {
Ok(_) => panic!("expected Error, but got Ok"),
Err(err) => {
if let Some(&Error::InvalidMessageType(0x01)) = err.downcast_ref::<Error>() {
if Error::InvalidMessageType(0x01) == err {
return Ok(());
}
panic!(
Expand Down
12 changes: 5 additions & 7 deletions data/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use bytes::{Buf, BufMut};
use message_channel_ack::*;
use message_channel_open::*;
use message_type::*;
use util::marshal::*;

use crate::error::Error;
use shared::error::{Error, Result};
use shared::marshal::*;

/// A parsed DataChannel message
#[derive(Eq, PartialEq, Clone, Debug)]
Expand All @@ -30,7 +29,7 @@ impl MarshalSize for Message {
}

impl Marshal for Message {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize, util::Error> {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
let mut bytes_written = 0;
let n = self.message_type().marshal_to(buf)?;
buf = &mut buf[n..];
Expand All @@ -44,7 +43,7 @@ impl Marshal for Message {
}

impl Unmarshal for Message {
fn unmarshal<B>(buf: &mut B) -> Result<Self, util::Error>
fn unmarshal<B>(buf: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
Expand All @@ -53,8 +52,7 @@ impl Unmarshal for Message {
return Err(Error::UnexpectedEndOfBuffer {
expected: MESSAGE_TYPE_LEN,
actual: buf.remaining(),
}
.into());
});
}

match MessageType::unmarshal(buf)? {
Expand Down
12 changes: 12 additions & 0 deletions shared/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,18 @@ pub enum Error {
#[error("Max Data Channel ID")]
ErrMaxDataChannelID,

//Data
#[error(
"DataChannel message is not long enough to determine type: (expected: {expected}, actual: {actual})"
)]
UnexpectedEndOfBuffer { expected: usize, actual: usize },
#[error("Unknown MessageType {0}")]
InvalidMessageType(u8),
#[error("Unknown ChannelType {0}")]
InvalidChannelType(u8),
#[error("Unknown PayloadProtocolIdentifier {0}")]
InvalidPayloadProtocolIdentifier(u8),

//#[error("mpsc send: {0}")]
//MpscSend(String),
#[error("aes gcm: {0}")]
Expand Down

0 comments on commit 7fe005b

Please sign in to comment.