@@ -34,6 +34,11 @@ pub trait TargetLocator: Send + Sync {
3434 async fn locate ( & self , uri : & rsip:: Uri ) -> Result < SipAddr > ;
3535}
3636
37+ #[ async_trait]
38+ pub trait TransportEventInspector : Send + Sync {
39+ async fn handle ( & self , event : & TransportEvent ) ;
40+ }
41+
3742pub struct EndpointOption {
3843 pub t1 : Duration ,
3944 pub t4 : Duration ,
@@ -104,8 +109,9 @@ pub struct EndpointInner {
104109 incoming_receiver : Mutex < Option < TransactionReceiver > > ,
105110 cancel_token : CancellationToken ,
106111 timer_interval : Duration ,
107- pub ( super ) inspector : Option < Box < dyn MessageInspector > > ,
112+ pub ( super ) message_inspector : Option < Box < dyn MessageInspector > > ,
108113 pub ( super ) locator : Option < Box < dyn TargetLocator > > ,
114+ pub ( super ) transport_inspector : Option < Box < dyn TransportEventInspector > > ,
109115 pub option : EndpointOption ,
110116}
111117pub type EndpointInnerRef = Arc < EndpointInner > ;
@@ -135,8 +141,9 @@ pub struct EndpointBuilder {
135141 cancel_token : Option < CancellationToken > ,
136142 timer_interval : Option < Duration > ,
137143 option : Option < EndpointOption > ,
138- inspector : Option < Box < dyn MessageInspector > > ,
144+ message_inspector : Option < Box < dyn MessageInspector > > ,
139145 target_locator : Option < Box < dyn TargetLocator > > ,
146+ transport_inspector : Option < Box < dyn TransportEventInspector > > ,
140147}
141148
142149/// SIP Endpoint
@@ -202,8 +209,9 @@ impl EndpointInner {
202209 timer_interval : Option < Duration > ,
203210 allows : Vec < rsip:: Method > ,
204211 option : Option < EndpointOption > ,
205- inspector : Option < Box < dyn MessageInspector > > ,
212+ message_inspector : Option < Box < dyn MessageInspector > > ,
206213 locator : Option < Box < dyn TargetLocator > > ,
214+ transport_inspector : Option < Box < dyn TransportEventInspector > > ,
207215 ) -> Arc < Self > {
208216 let ( incoming_sender, incoming_receiver) = unbounded_channel ( ) ;
209217 Arc :: new ( EndpointInner {
@@ -219,8 +227,9 @@ impl EndpointInner {
219227 incoming_sender,
220228 incoming_receiver : Mutex :: new ( Some ( incoming_receiver) ) ,
221229 option : option. unwrap_or_default ( ) ,
222- inspector ,
230+ message_inspector ,
223231 locator,
232+ transport_inspector,
224233 } )
225234 }
226235
@@ -257,6 +266,10 @@ impl EndpointInner {
257266 } ;
258267
259268 while let Some ( event) = transport_rx. recv ( ) . await {
269+ if let Some ( transport_inspector) = & self . transport_inspector {
270+ transport_inspector. handle ( & event) . await ;
271+ }
272+
260273 match event {
261274 TransportEvent :: Incoming ( msg, connection, from) => {
262275 match self . on_received_message ( msg, connection) . await {
@@ -400,7 +413,7 @@ impl EndpointInner {
400413 }
401414 } ;
402415
403- let msg = if let Some ( inspector) = & self . inspector {
416+ let msg = if let Some ( inspector) = & self . message_inspector {
404417 inspector. after_received ( msg)
405418 } else {
406419 msg
@@ -429,7 +442,7 @@ impl EndpointInner {
429442 rsip:: StatusCode :: CallTransactionDoesNotExist ,
430443 None ,
431444 ) ;
432- let resp = if let Some ( ref inspector) = self . inspector {
445+ let resp = if let Some ( ref inspector) = self . message_inspector {
433446 inspector. before_send ( resp. into ( ) )
434447 } else {
435448 resp. into ( )
@@ -558,8 +571,9 @@ impl EndpointBuilder {
558571 cancel_token : None ,
559572 timer_interval : None ,
560573 option : None ,
561- inspector : None ,
574+ message_inspector : None ,
562575 target_locator : None ,
576+ transport_inspector : None ,
563577 }
564578 }
565579 pub fn with_option ( & mut self , option : EndpointOption ) -> & mut Self {
@@ -590,13 +604,22 @@ impl EndpointBuilder {
590604 self
591605 }
592606 pub fn with_inspector ( & mut self , inspector : Box < dyn MessageInspector > ) -> & mut Self {
593- self . inspector = Some ( inspector) ;
607+ self . message_inspector = Some ( inspector) ;
594608 self
595609 }
596610 pub fn with_target_locator ( & mut self , locator : Box < dyn TargetLocator > ) -> & mut Self {
597611 self . target_locator = Some ( locator) ;
598612 self
599613 }
614+
615+ pub fn with_transport_inspector (
616+ & mut self ,
617+ inspector : Box < dyn TransportEventInspector > ,
618+ ) -> & mut Self {
619+ self . transport_inspector = Some ( inspector) ;
620+ self
621+ }
622+
600623 pub fn build ( & mut self ) -> Endpoint {
601624 let cancel_token = self . cancel_token . take ( ) . unwrap_or_default ( ) ;
602625
@@ -609,8 +632,9 @@ impl EndpointBuilder {
609632 let user_agent = self . user_agent . to_owned ( ) ;
610633 let timer_interval = self . timer_interval . to_owned ( ) ;
611634 let option = self . option . take ( ) ;
612- let inspector = self . inspector . take ( ) ;
635+ let message_inspector = self . message_inspector . take ( ) ;
613636 let locator = self . target_locator . take ( ) ;
637+ let transport_inspector = self . transport_inspector . take ( ) ;
614638
615639 let core = EndpointInner :: new (
616640 user_agent,
@@ -619,8 +643,9 @@ impl EndpointBuilder {
619643 timer_interval,
620644 allows,
621645 option,
622- inspector ,
646+ message_inspector ,
623647 locator,
648+ transport_inspector,
624649 ) ;
625650
626651 Endpoint { inner : core }
0 commit comments