@@ -22,16 +22,18 @@ use std::future::Future;
2222use std:: sync:: Arc ;
2323
2424use chrono:: Utc ;
25- use tracing:: { debug, info, warn} ;
25+ use tokio:: task:: JoinHandle ;
26+ use tracing:: { debug, info, trace, warn} ;
2627
27- use crate :: state:: SharedState ;
28+ use crate :: error:: Report ;
29+ use crate :: state:: { SharedState , Status } ;
2830use crate :: transport:: TransportError ;
2931use crate :: Timestamp ;
3032use crate :: {
3133 client:: RecvError ,
3234 event:: DeviceEvent ,
3335 store:: wrapper:: StoreWrapper ,
34- transport:: { Connection , Disconnect , Publish , Receive , Reconnect , Register } ,
36+ transport:: { Connection , Publish , Receive , Reconnect } ,
3537 Error ,
3638} ;
3739
8486 connection : C ,
8587 sender : C :: Sender ,
8688 state : Arc < SharedState > ,
89+ resend : Option < JoinHandle < ( ) > > ,
8790}
8891
8992impl < C > DeviceConnection < C >
@@ -103,6 +106,7 @@ where
103106 state,
104107 connection,
105108 sender,
109+ resend : None ,
106110 }
107111 }
108112
@@ -130,19 +134,61 @@ where
130134 } ) ,
131135 }
132136 }
137+
138+ /// Keeps polling connection events
139+ pub ( super ) async fn poll ( & mut self ) -> Result < Status , TransportError >
140+ where
141+ C : Receive + Reconnect ,
142+ C :: Sender : Publish + ' static ,
143+ {
144+ let Some ( event) = self . connection . next_event ( ) . await ? else {
145+ trace ! ( "disconnected" ) ;
146+
147+ self . state . status . set_connected ( false ) ;
148+
149+ // This will check if the connection was closed
150+ return Ok ( self . state . status . connection ( ) ) ;
151+ } ;
152+
153+ let event = self
154+ . handle_event ( & event. interface , & event. path , event. payload )
155+ . await
156+ . map ( |data| DeviceEvent {
157+ interface : event. interface ,
158+ path : event. path ,
159+ data,
160+ } ) ?;
161+
162+ self . tx . send ( Ok ( event) ) . map_err ( |err| {
163+ debug ! ( error = %Report :: new( err) , "disconnected" ) ;
164+
165+ TransportError :: Transport ( Error :: Disconnected )
166+ } ) ?;
167+
168+ Ok ( self . state . status . connection ( ) )
169+ }
133170}
134171
135172impl < C > EventLoop for DeviceConnection < C >
136173where
137- C : Connection + Reconnect + Receive + Send + Sync + ' static ,
138- C :: Sender : Send + Register + Publish + Disconnect + ' static ,
174+ C : Connection + Reconnect + Receive + ' static ,
175+ C :: Sender : Publish + ' static ,
139176{
140177 async fn handle_events ( mut self ) -> Result < ( ) , crate :: Error > {
141178 self . init_stored_retention ( ) . await ?;
142179
180+ // We are connected and all the stored packet have been sent
181+ self . state . status . set_connected ( true ) ;
182+
143183 loop {
144- let opt = match self . connection . next_event ( ) . await {
145- Ok ( opt) => opt,
184+ match self . poll ( ) . await {
185+ Ok ( Status :: Connected ) => { }
186+ Ok ( Status :: Disconnected ) => {
187+ self . reconnect_and_resend ( ) . await ?;
188+ }
189+ Ok ( Status :: Closed ) => {
190+ break ;
191+ }
146192 Err ( TransportError :: Transport ( err) ) => {
147193 return Err ( err) ;
148194 }
@@ -152,45 +198,8 @@ where
152198 . send_async ( Err ( recv_err) )
153199 . await
154200 . map_err ( |_| Error :: Disconnected ) ?;
155-
156- continue ;
157201 }
158- } ;
159-
160- let Some ( event_data) = opt else {
161- if self . state . status . is_closed ( ) {
162- debug ! ( "connection closed" ) ;
163-
164- break ;
165- }
166-
167- debug ! ( "reconnecting" ) ;
168-
169- self . state . status . set_connected ( false ) ;
170-
171- let interfaces = self . state . interfaces . read ( ) . await ;
172-
173- self . connection . reconnect ( & interfaces) . await ?;
174-
175- Self :: resend_volatile_publishes (
176- & self . state . volatile_store ,
177- & mut self . sender ,
178- & self . state . status ,
179- )
180- . await ?;
181- Self :: resend_stored_publishes (
182- & mut self . store ,
183- & mut self . sender ,
184- & self . state . status ,
185- )
186- . await ?;
187-
188- self . state . status . set_connected ( true ) ;
189-
190- continue ;
191- } ;
192-
193- self . handle_connection_event ( event_data) . await ?
202+ }
194203 }
195204
196205 info ! ( "connection closed successfully" ) ;
0 commit comments