Skip to content

Commit

Permalink
Rust: add missing request
Browse files Browse the repository at this point in the history
Bonus: fix test.
  • Loading branch information
jmillan committed Oct 10, 2023
1 parent a8cf41d commit e43ff81
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 5 deletions.
80 changes: 80 additions & 0 deletions rust/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,86 @@ impl Request for RouterCreateWebRtcTransportRequest {
type Response = WebRtcTransportData;

fn into_bytes(self, id: u32, handler_id: Self::HandlerId) -> Vec<u8> {
let RouterCreateWebrtcTransportListen::Individual { listen_infos: _ } = self.data.listen
else {
panic!("RouterCreateWebrtcTransportListen variant must be Individual");
};

let mut builder = Builder::new();
let data = router::CreateWebRtcTransportRequest::create(
&mut builder,
self.data.transport_id.to_string(),
self.data.to_fbs(),
);
let request_body =
request::Body::create_router_create_web_rtc_transport_request(&mut builder, data);
let request = request::Request::create(
&mut builder,
id,
Self::METHOD,
handler_id.to_string(),
Some(request_body),
);
let message_body = message::Body::create_request(&mut builder, request);
let message = message::Message::create(&mut builder, message::Type::Request, message_body);

builder.finish(message, None).to_vec()
}

fn convert_response(
response: Option<response::Body>,
) -> Result<Self::Response, Box<dyn Error>> {
let Some(response::Body::WebRtcTransportDumpResponse(data)) = response else {
panic!("Wrong message from worker: {response:?}");
};

Ok(WebRtcTransportData {
ice_role: IceRole::from_fbs(data.ice_role),
ice_parameters: IceParameters::from_fbs(*data.ice_parameters),
ice_candidates: data
.ice_candidates
.iter()
.map(IceCandidate::from_fbs)
.collect(),
ice_state: Mutex::new(IceState::from_fbs(data.ice_state)),
ice_selected_tuple: Mutex::new(
data.ice_selected_tuple
.map(|tuple| TransportTuple::from_fbs(tuple.as_ref())),
),
dtls_parameters: Mutex::new(DtlsParameters::from_fbs(*data.dtls_parameters)),
dtls_state: Mutex::new(DtlsState::from_fbs(data.dtls_state)),
dtls_remote_cert: Mutex::new(None),
sctp_parameters: data
.base
.sctp_parameters
.map(|parameters| SctpParameters::from_fbs(parameters.as_ref())),
sctp_state: Mutex::new(
data.base
.sctp_state
.map(|state| SctpState::from_fbs(&state)),
),
})
}
}

#[derive(Debug)]
pub(crate) struct RouterCreateWebRtcTransportWithServerRequest {
pub(crate) data: RouterCreateWebrtcTransportData,
}

impl Request for RouterCreateWebRtcTransportWithServerRequest {
const METHOD: request::Method = request::Method::RouterCreateWebrtctransportWithServer;
type HandlerId = RouterId;
type Response = WebRtcTransportData;

fn into_bytes(self, id: u32, handler_id: Self::HandlerId) -> Vec<u8> {
let RouterCreateWebrtcTransportListen::Server {
webrtc_server_id: _,
} = self.data.listen
else {
panic!("RouterCreateWebrtcTransportListen variant must be Server");
};

let mut builder = Builder::new();
let data = router::CreateWebRtcTransportRequest::create(
&mut builder,
Expand Down
36 changes: 35 additions & 1 deletion rust/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use crate::messages::{
RouterCreateDirectTransportRequest, RouterCreatePipeTransportData,
RouterCreatePipeTransportRequest, RouterCreatePlainTransportData,
RouterCreatePlainTransportRequest, RouterCreateWebRtcTransportRequest,
RouterCreateWebrtcTransportData, RouterDumpRequest,
RouterCreateWebRtcTransportWithServerRequest, RouterCreateWebrtcTransportData,
RouterDumpRequest,
};
use crate::pipe_transport::{
PipeTransport, PipeTransportOptions, PipeTransportRemoteParameters, WeakPipeTransport,
Expand Down Expand Up @@ -626,6 +627,38 @@ impl Router {

let _buffer_guard = self.inner.channel.buffer_messages_for(transport_id.into());

let data = match webrtc_transport_options.listen {
WebRtcTransportListen::Individual { listen_infos: _ } => {
self.inner
.channel
.request(
self.inner.id,
RouterCreateWebRtcTransportRequest {
data: RouterCreateWebrtcTransportData::from_options(
transport_id,
&webrtc_transport_options,
),
},
)
.await?
}
WebRtcTransportListen::Server { webrtc_server: _ } => {
self.inner
.channel
.request(
self.inner.id,
RouterCreateWebRtcTransportWithServerRequest {
data: RouterCreateWebrtcTransportData::from_options(
transport_id,
&webrtc_transport_options,
),
},
)
.await?
}
};

/*
let data = self
.inner
.channel
Expand All @@ -639,6 +672,7 @@ impl Router {
},
)
.await?;
*/

let transport = WebRtcTransport::new(
transport_id,
Expand Down
16 changes: 12 additions & 4 deletions rust/src/router/webrtc_transport/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::data_structures::{IceCandidateType, IceState, ListenInfo, Protocol};
use crate::data_structures::{
IceCandidateTcpType, IceCandidateType, IceState, ListenInfo, Protocol,
};
use crate::prelude::WebRtcTransport;
use crate::router::{NewTransport, Router, RouterOptions};
use crate::transport::Transport;
Expand Down Expand Up @@ -41,7 +43,6 @@ async fn init() -> (Worker, Router) {
}

#[test]
#[ignore]
fn create_with_webrtc_server_succeeds() {
future::block_on(async move {
let (worker, router) = init().await;
Expand Down Expand Up @@ -123,12 +124,20 @@ fn create_with_webrtc_server_succeeds() {

{
let ice_candidates = transport.ice_candidates();
assert_eq!(ice_candidates.len(), 1);
assert_eq!(ice_candidates.len(), 2);
assert_eq!(ice_candidates[0].ip, IpAddr::V4(Ipv4Addr::LOCALHOST));
assert_eq!(ice_candidates[0].protocol, Protocol::Udp);
assert_eq!(ice_candidates[0].port, port1);
assert_eq!(ice_candidates[0].r#type, IceCandidateType::Host);
assert_eq!(ice_candidates[0].tcp_type, None);
assert_eq!(ice_candidates[1].ip, IpAddr::V4(Ipv4Addr::LOCALHOST));
assert_eq!(ice_candidates[1].protocol, Protocol::Tcp);
assert_eq!(ice_candidates[1].port, port2);
assert_eq!(ice_candidates[1].r#type, IceCandidateType::Host);
assert_eq!(
ice_candidates[1].tcp_type,
Some(IceCandidateTcpType::Passive)
);
}

assert_eq!(transport.ice_state(), IceState::New);
Expand Down Expand Up @@ -251,7 +260,6 @@ fn router_close_event() {
}

#[test]
#[ignore]
fn webrtc_server_close_event() {
future::block_on(async move {
let (worker, router) = init().await;
Expand Down

0 comments on commit e43ff81

Please sign in to comment.