@@ -22,12 +22,20 @@ use std::{collections::HashMap, fs, path::Path};
2222
2323const CONCURRENT_MESSAGES : usize = 1 ;
2424
25+ /// Content after last
26+ #[ derive( Debug , Clone ) ]
27+ pub struct Answer {
28+ /// Peer public key derived with BLAKE3.
29+ pub peer_id : String ,
30+ /// Serialized default session.
31+ pub session : Option < String > ,
32+ }
33+
2534/// Result possibilites for session connection.
2635#[ derive( Debug ) ]
27- pub enum Session {
28- Offer ( String ) ,
29- Answered ( String ) ,
30- Invalid ,
36+ pub enum SessionResult {
37+ IncomingOffer ( String ) ,
38+ Completed ( Answer ) ,
3139}
3240
3341/// Method to extract config.
@@ -127,7 +135,7 @@ impl Turms {
127135 async fn incoming_offer (
128136 & mut self ,
129137 session : RTCSessionDescription ,
130- ) -> Result < Session > {
138+ ) -> Result < SessionResult > {
131139 let mut webrtc = WebRTCManager :: init ( self . config . rtc . clone ( ) ) . await ?;
132140
133141 let _channel = webrtc. create_channel ( ) . await ?;
@@ -137,43 +145,52 @@ impl Turms {
137145 let id = Self :: extract_session_id ( & offer) ?;
138146
139147 self . queued_connection . insert ( id. to_string ( ) , webrtc) ;
140- Ok ( Session :: Offer ( serde_json:: to_string ( & offer) ?) )
148+ Ok ( SessionResult :: IncomingOffer ( serde_json:: to_string ( & offer) ?) )
141149 }
142150
143151 async fn incoming_answer (
144152 & mut self ,
145153 session : RTCSessionDescription ,
146- ) -> Result < Session > {
154+ ) -> Result < SessionResult > {
147155 let id = Self :: extract_session_id ( & session) ?;
148156 let webrtc = self
149157 . queued_connection
150158 . remove ( & id)
151159 . ok_or ( error:: Error :: MissingSessionId ) ?;
152160
153- if let Err ( err) = webrtc
154- . peer_connection
155- . set_remote_description ( session)
156- . await
161+ if let Err ( err) =
162+ webrtc. peer_connection . set_remote_description ( session) . await
157163 {
158164 // If error, re-insert WebRTC connection.
159165 self . queued_connection . insert ( id, webrtc) ;
160166 return Err ( err. into ( ) ) ;
161167 }
162168
163169 let peer_id = webrtc. peer_id . lock ( ) . await . to_string ( ) ;
164- self . peers_connection . insert ( peer_id. clone ( ) , webrtc) ;
170+ let session = webrtc
171+ . session
172+ . lock ( )
173+ . await
174+ . as_ref ( )
175+ . and_then ( |sess| serde_json:: to_string ( & sess. pickle ( ) ) . ok ( ) ) ;
176+ let answer = Answer {
177+ peer_id : peer_id. clone ( ) ,
178+ session,
179+ } ;
180+
181+ self . peers_connection . insert ( peer_id, webrtc) ;
165182
166- Ok ( Session :: Answered ( peer_id ) )
183+ Ok ( SessionResult :: Completed ( answer ) )
167184 }
168185
169186 /// Inits connection and create data channel.
170- pub async fn connect ( & mut self , session : & str ) -> Result < Session > {
187+ pub async fn connect ( & mut self , session : & str ) -> Result < SessionResult > {
171188 let session = to_session_description ( session) ?;
172189
173190 match session. sdp_type {
174191 RTCSdpType :: Offer => self . incoming_offer ( session) . await ,
175192 RTCSdpType :: Answer => self . incoming_answer ( session) . await ,
176- _ => Ok ( Session :: Invalid ) ,
193+ _ => Err ( webrtc :: Error :: ErrIncorrectSDPSemantics . into ( ) ) ,
177194 }
178195 }
179196}
0 commit comments