diff --git a/rust/src/messages.rs b/rust/src/messages.rs index 5ae6bd603b..46ada2f2a3 100644 --- a/rust/src/messages.rs +++ b/rust/src/messages.rs @@ -730,6 +730,86 @@ impl Request for RouterCreateWebRtcTransportRequest { type Response = WebRtcTransportData; fn into_bytes(self, id: u32, handler_id: Self::HandlerId) -> Vec { + 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, + ) -> Result> { + 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 { + 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, diff --git a/rust/src/router.rs b/rust/src/router.rs index 21ac35e13e..4c1925e603 100644 --- a/rust/src/router.rs +++ b/rust/src/router.rs @@ -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, @@ -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 @@ -639,6 +672,7 @@ impl Router { }, ) .await?; + */ let transport = WebRtcTransport::new( transport_id, diff --git a/rust/src/router/webrtc_transport/tests.rs b/rust/src/router/webrtc_transport/tests.rs index 4e3cca5725..844555be4e 100644 --- a/rust/src/router/webrtc_transport/tests.rs +++ b/rust/src/router/webrtc_transport/tests.rs @@ -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; @@ -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; @@ -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); @@ -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;