Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update modules structure and other minor improvements for Rust version #558

Merged
merged 9 commits into from
May 19, 2021
69 changes: 31 additions & 38 deletions rust/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn media_codecs() -> Vec<RtpCodecCapability> {
preferred_payload_type: None,
clock_rate: NonZeroU32::new(48000).unwrap(),
channels: NonZeroU8::new(2).unwrap(),
parameters: RtpCodecParametersParameters::from([("useinbandfec", 1u32.into())]),
parameters: RtpCodecParametersParameters::from([("useinbandfec", 1_u32.into())]),
rtcp_feedback: vec![RtcpFeedback::TransportCc],
},
RtpCodecCapability::Video {
Expand Down Expand Up @@ -55,8 +55,9 @@ struct TransportOptions {
ice_parameters: IceParameters,
}

#[derive(Serialize)]
#[derive(Serialize, Message)]
#[serde(tag = "action")]
#[rtype(result = "()")]
enum ServerMessage {
#[serde(rename_all = "camelCase")]
Init {
Expand All @@ -79,12 +80,9 @@ enum ServerMessage {
},
}

impl Message for ServerMessage {
type Result = ();
}

#[derive(Deserialize)]
#[derive(Deserialize, Message)]
#[serde(tag = "action")]
#[rtype(result = "()")]
enum ClientMessage {
#[serde(rename_all = "camelCase")]
Init { rtp_capabilities: RtpCapabilities },
Expand All @@ -103,20 +101,14 @@ enum ClientMessage {
ConsumerResume { id: ConsumerId },
}

impl Message for ClientMessage {
type Result = ();
}

#[derive(Message)]
#[rtype(result = "()")]
enum InternalMessage {
SaveProducer(Producer),
SaveConsumer(Consumer),
Stop,
}

impl Message for InternalMessage {
type Result = ();
}

struct Transports {
consumer: WebRtcTransport,
producer: WebRtcTransport,
Expand Down Expand Up @@ -331,28 +323,29 @@ impl Handler<ClientMessage> for EchoServer {
}
});
}
ClientMessage::ConsumerResume { id } => match self.consumers.get(&id).cloned() {
Some(consumer) => actix::spawn(async move {
match consumer.resume().await {
Ok(_) => {
println!(
"Successfully resumed {:?} consumer {}",
consumer.kind(),
consumer.id(),
);
}
Err(error) => {
println!(
"Failed to resume {:?} consumer {}: {}",
consumer.kind(),
consumer.id(),
error,
);
ClientMessage::ConsumerResume { id } => {
if let Some(consumer) = self.consumers.get(&id).cloned() {
actix::spawn(async move {
match consumer.resume().await {
Ok(_) => {
println!(
"Successfully resumed {:?} consumer {}",
consumer.kind(),
consumer.id(),
);
}
Err(error) => {
println!(
"Failed to resume {:?} consumer {}: {}",
consumer.kind(),
consumer.id(),
error,
);
}
}
}
}),
None => {}
},
})
}
}
}
}
}
Expand Down Expand Up @@ -385,7 +378,7 @@ impl Handler<InternalMessage> for EchoServer {
}

async fn ws_index(
r: HttpRequest,
request: HttpRequest,
worker_manager: Data<WorkerManager>,
stream: Payload,
) -> Result<HttpResponse, Error> {
Expand All @@ -395,7 +388,7 @@ async fn ws_index(
HttpResponse::InternalServerError().finish()
})?;

ws::start(echo_server, &r, stream)
ws::start(echo_server, &request, stream)
}

#[actix_web::main]
Expand Down
92 changes: 46 additions & 46 deletions rust/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,21 @@ impl<'de> Deserialize<'de> for DtlsFingerprint {
where
V: MapAccess<'de>,
{
fn parse_as_bytes(input: &str, output: &mut [u8]) -> Result<(), String> {
for (i, v) in output.iter_mut().enumerate() {
*v = u8::from_str_radix(&input[i * 3..(i * 3) + 2], 16).map_err(
|error| {
format!(
"Failed to parse value {} as series of hex bytes: {}",
input, error,
)
},
)?;
}

Ok(())
}

let mut algorithm = None::<Cow<'_, str>>;
let mut value = None::<Cow<'_, str>>;
while let Some(key) = map.next_key()? {
Expand All @@ -583,95 +598,80 @@ impl<'de> Deserialize<'de> for DtlsFingerprint {
let algorithm = algorithm.ok_or_else(|| de::Error::missing_field("algorithm"))?;
let value = value.ok_or_else(|| de::Error::missing_field("value"))?;

fn parse_as_bytes(input: &str, output: &mut [u8]) -> Result<(), String> {
for (i, v) in output.iter_mut().enumerate() {
*v = u8::from_str_radix(&input[i * 3..(i * 3) + 2], 16).map_err(
|error| {
format!(
"Failed to parse value {} as series of hex bytes: {}",
input, error,
)
},
)?;
}

Ok(())
}

match algorithm.as_ref() {
"sha-1" => {
if value.len() != (20 * 3 - 1) {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-1",
))
} else {
let mut value_result = [0u8; 20];
if value.len() == (20 * 3 - 1) {
let mut value_result = [0_u8; 20];
parse_as_bytes(value.as_ref(), &mut value_result)
.map_err(de::Error::custom)?;

Ok(DtlsFingerprint::Sha1 {
value: value_result,
})
} else {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-1",
))
}
}
"sha-224" => {
if value.len() != (28 * 3 - 1) {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-224",
))
} else {
let mut value_result = [0u8; 28];
if value.len() == (28 * 3 - 1) {
let mut value_result = [0_u8; 28];
parse_as_bytes(value.as_ref(), &mut value_result)
.map_err(de::Error::custom)?;

Ok(DtlsFingerprint::Sha224 {
value: value_result,
})
} else {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-224",
))
}
}
"sha-256" => {
if value.len() != (32 * 3 - 1) {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-256",
))
} else {
let mut value_result = [0u8; 32];
if value.len() == (32 * 3 - 1) {
let mut value_result = [0_u8; 32];
parse_as_bytes(value.as_ref(), &mut value_result)
.map_err(de::Error::custom)?;

Ok(DtlsFingerprint::Sha256 {
value: value_result,
})
} else {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-256",
))
}
}
"sha-384" => {
if value.len() != (48 * 3 - 1) {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-384",
))
} else {
let mut value_result = [0u8; 48];
if value.len() == (48 * 3 - 1) {
let mut value_result = [0_u8; 48];
parse_as_bytes(value.as_ref(), &mut value_result)
.map_err(de::Error::custom)?;

Ok(DtlsFingerprint::Sha384 {
value: value_result,
})
} else {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-384",
))
}
}
"sha-512" => {
if value.len() != (64 * 3 - 1) {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-512",
))
} else {
let mut value_result = [0u8; 64];
if value.len() == (64 * 3 - 1) {
let mut value_result = [0_u8; 64];
parse_as_bytes(value.as_ref(), &mut value_result)
.map_err(de::Error::custom)?;

Ok(DtlsFingerprint::Sha512 {
value: value_result,
})
} else {
Err(de::Error::custom(
"Value doesn't have correct length for SHA-512",
))
}
}
algorithm => Err(de::Error::unknown_variant(
Expand Down Expand Up @@ -749,7 +749,7 @@ impl WebRtcMessage {
WebRtcMessage::String(string) => (51_u32, Bytes::from(string)),
WebRtcMessage::Binary(binary) => (53_u32, binary),
WebRtcMessage::EmptyString => (56_u32, Bytes::from_static(b" ")),
WebRtcMessage::EmptyBinary => (57_u32, Bytes::from(vec![0u8])),
WebRtcMessage::EmptyBinary => (57_u32, Bytes::from(vec![0_u8])),
}
}
}
Loading