@@ -27,14 +27,14 @@ use crate::{
2727 P2pChannelEvent , P2pConnectionEvent , P2pEvent , PeerId ,
2828} ;
2929
30- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" ) ) ]
30+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" , not ( feature = "p2p-webrtc-cpp" ) ) ) ]
3131mod imports {
3232 pub use super :: webrtc_rs:: {
3333 build_api, certificate_from_pem_key, webrtc_signal_send, Api , RTCCertificate , RTCChannel ,
3434 RTCConnection , RTCConnectionState , RTCSignalingError ,
3535 } ;
3636}
37- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" ) ) ]
37+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" , not ( feature = "p2p-webrtc-rs" ) ) ) ]
3838mod imports {
3939 pub use super :: webrtc_cpp:: {
4040 build_api, certificate_from_pem_key, webrtc_signal_send, Api , RTCCertificate , RTCChannel ,
@@ -49,6 +49,15 @@ mod imports {
4949 } ;
5050}
5151
52+ // Fallback when both webrtc features are enabled - prefer webrtc-rs
53+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" , feature = "p2p-webrtc-cpp" ) ) ]
54+ mod imports {
55+ pub use super :: webrtc_rs:: {
56+ build_api, certificate_from_pem_key, webrtc_signal_send, Api , RTCCertificate , RTCChannel ,
57+ RTCConnection , RTCConnectionState , RTCSignalingError ,
58+ } ;
59+ }
60+
5261use imports:: * ;
5362pub use imports:: { webrtc_signal_send, RTCSignalingError } ;
5463
@@ -102,26 +111,39 @@ pub struct PeerState {
102111 pub abort : Aborter ,
103112}
104113
105- #[ derive( thiserror:: Error , derive_more :: From , Debug ) ]
114+ #[ derive( thiserror:: Error , Debug ) ]
106115pub ( super ) enum Error {
107- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" ) ) ]
116+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" , not ( feature = "p2p-webrtc-cpp" ) ) ) ]
108117 #[ error( "{0}" ) ]
109- Rtc ( :: webrtc:: Error ) ,
110- #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" ) ) ]
118+ RtcRs ( :: webrtc:: Error ) ,
119+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" , not ( feature = "p2p-webrtc-rs" ) ) ) ]
111120 #[ error( "{0}" ) ]
112- Rtc ( :: datachannel:: Error ) ,
121+ RtcCpp ( :: datachannel:: Error ) ,
113122 #[ cfg( target_arch = "wasm32" ) ]
114123 #[ error( "js error: {0:?}" ) ]
115124 RtcJs ( String ) ,
116125 #[ error( "signaling error: {0}" ) ]
117126 Signaling ( RTCSignalingError ) ,
118127 #[ error( "unexpected cmd received" ) ]
119128 UnexpectedCmd ,
120- #[ from( ignore) ]
121129 #[ error( "channel closed" ) ]
122130 ChannelClosed ,
123131}
124132
133+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" , not( feature = "p2p-webrtc-cpp" ) ) ) ]
134+ impl From < :: webrtc:: Error > for Error {
135+ fn from ( error : :: webrtc:: Error ) -> Self {
136+ Self :: RtcRs ( error)
137+ }
138+ }
139+
140+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" , not( feature = "p2p-webrtc-rs" ) ) ) ]
141+ impl From < :: datachannel:: Error > for Error {
142+ fn from ( error : :: datachannel:: Error ) -> Self {
143+ Self :: RtcCpp ( error)
144+ }
145+ }
146+
125147#[ cfg( target_arch = "wasm32" ) ]
126148impl From < wasm_bindgen:: JsValue > for Error {
127149 fn from ( value : wasm_bindgen:: JsValue ) -> Self {
@@ -376,13 +398,32 @@ async fn peer_start(
376398 // there is a link between peer identity and connection.
377399 let ( remote_auth_tx, remote_auth_rx) = oneshot:: channel :: < ConnectionAuthEncrypted > ( ) ;
378400 let mut remote_auth_tx = Some ( remote_auth_tx) ;
401+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" , not( feature = "p2p-webrtc-cpp" ) ) ) ]
402+ main_channel. on_message ( move |data| {
403+ if let Some ( tx) = remote_auth_tx. take ( ) {
404+ if let Ok ( auth) = data. try_into ( ) {
405+ let _ = tx. send ( auth) ;
406+ }
407+ }
408+ std:: future:: ready ( ( ) )
409+ } ) ;
410+
411+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" , not( feature = "p2p-webrtc-rs" ) ) ) ]
412+ main_channel. on_message ( move |data| {
413+ if let Some ( tx) = remote_auth_tx. take ( ) {
414+ if let Ok ( auth) = data. try_into ( ) {
415+ let _ = tx. send ( auth) ;
416+ }
417+ }
418+ } ) ;
419+
420+ #[ cfg( target_arch = "wasm32" ) ]
379421 main_channel. on_message ( move |data| {
380422 if let Some ( tx) = remote_auth_tx. take ( ) {
381423 if let Ok ( auth) = data. try_into ( ) {
382424 let _ = tx. send ( auth) ;
383425 }
384426 }
385- #[ cfg( not( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" ) ) ) ]
386427 std:: future:: ready ( ( ) )
387428 } ) ;
388429 let msg = match cmd_receiver. recv ( ) . await {
@@ -654,6 +695,34 @@ async fn peer_loop(
654695 let mut buf = Vec :: new ( ) ;
655696 let event_sender_clone = event_sender. clone ( ) ;
656697
698+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-rs" , not( feature = "p2p-webrtc-cpp" ) ) ) ]
699+ chan. on_message ( move |mut data| {
700+ while !data. is_empty ( ) {
701+ let res = match process_msg ( chan_id, & mut buf, & mut len, & mut data) {
702+ Ok ( None ) => continue ,
703+ Ok ( Some ( msg) ) => Ok ( msg) ,
704+ Err ( err) => Err ( err) ,
705+ } ;
706+ let _ =
707+ event_sender_clone ( P2pChannelEvent :: Received ( peer_id, res) . into ( ) ) ;
708+ }
709+ std:: future:: ready ( ( ) )
710+ } ) ;
711+
712+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" , not( feature = "p2p-webrtc-rs" ) ) ) ]
713+ chan. on_message ( move |mut data| {
714+ while !data. is_empty ( ) {
715+ let res = match process_msg ( chan_id, & mut buf, & mut len, & mut data) {
716+ Ok ( None ) => continue ,
717+ Ok ( Some ( msg) ) => Ok ( msg) ,
718+ Err ( err) => Err ( err) ,
719+ } ;
720+ let _ =
721+ event_sender_clone ( P2pChannelEvent :: Received ( peer_id, res) . into ( ) ) ;
722+ }
723+ } ) ;
724+
725+ #[ cfg( target_arch = "wasm32" ) ]
657726 chan. on_message ( move |mut data| {
658727 while !data. is_empty ( ) {
659728 let res = match process_msg ( chan_id, & mut buf, & mut len, & mut data) {
@@ -664,7 +733,6 @@ async fn peer_loop(
664733 let _ =
665734 event_sender_clone ( P2pChannelEvent :: Received ( peer_id, res) . into ( ) ) ;
666735 }
667- #[ cfg( not( all( not( target_arch = "wasm32" ) , feature = "p2p-webrtc-cpp" ) ) ) ]
668736 std:: future:: ready ( ( ) )
669737 } ) ;
670738
0 commit comments