@@ -6,7 +6,7 @@ use enet_sys::{
66 ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT ,
77} ;
88
9- use crate :: { Address , EnetKeepAlive , Error , Event , EventKind , Peer , PeerID } ;
9+ use crate :: { Address , EnetKeepAlive , Error , Event , Peer , PeerID } ;
1010
1111#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
1212/// Represents a bandwidth limit or unlimited.
@@ -60,7 +60,6 @@ impl BandwidthLimit {
6060/// transmission.
6161pub struct Host < T > {
6262 inner : * mut ENetHost ,
63- disconnect_drop : Option < PeerID > ,
6463 _keep_alive : Arc < EnetKeepAlive > ,
6564 _peer_data : PhantomData < * const T > ,
6665}
@@ -71,7 +70,6 @@ impl<T> Host<T> {
7170
7271 Host {
7372 inner,
74- disconnect_drop : None ,
7573 _keep_alive,
7674 _peer_data : PhantomData ,
7775 }
@@ -134,33 +132,41 @@ impl<T> Host<T> {
134132 unsafe { ( * self . inner ) . peerCount }
135133 }
136134
137- /// Returns a mutable reference to a peer at the index , None if the index is invalid.
135+ /// Returns a mutable reference to a peer at the given PeerID , None if the index is invalid.
138136 pub fn peer_mut ( & mut self , idx : PeerID ) -> Option < & mut Peer < T > > {
139- if idx . 0 >= self . peer_count ( ) {
137+ if ! ( 0 .. self . peer_count ( ) as isize ) . contains ( & idx . index ) {
140138 return None ;
141139 }
142140
143- Some ( Peer :: new_mut ( unsafe {
144- & mut * ( ( * self . inner ) . peers . offset ( idx. 0 as isize ) )
145- } ) )
141+ let peer = Peer :: new_mut ( unsafe { & mut * ( ( * self . inner ) . peers . offset ( idx. index ) ) } ) ;
142+ if peer. generation ( ) != idx. generation {
143+ return None ;
144+ }
145+
146+ Some ( peer)
146147 }
147148
148- /// Returns a reference to a peer at the index , None if the index is invalid.
149+ /// Returns a reference to a peer at the given PeerID , None if the index is invalid.
149150 pub fn peer ( & self , idx : PeerID ) -> Option < & Peer < T > > {
150- if idx . 0 >= self . peer_count ( ) {
151+ if ! ( 0 .. self . peer_count ( ) as isize ) . contains ( & idx . index ) {
151152 return None ;
152153 }
153154
154- Some ( Peer :: new ( unsafe {
155- & * ( ( * self . inner ) . peers . offset ( idx. 0 as isize ) )
156- } ) )
155+ let peer = Peer :: new ( unsafe { & * ( ( * self . inner ) . peers . offset ( idx. index ) ) } ) ;
156+ if peer. generation ( ) != idx. generation {
157+ return None ;
158+ }
159+
160+ Some ( peer)
157161 }
158162
159163 pub ( crate ) unsafe fn peer_id ( & self , peer : * mut ENetPeer ) -> PeerID {
160- PeerID (
161- ( peer as enet_sys:: size_t - ( * self . inner ) . peers as enet_sys:: size_t )
162- / std:: mem:: size_of :: < ENetPeer > ( ) as enet_sys:: size_t ,
163- )
164+ let index = peer. offset_from ( ( * self . inner ) . peers ) ;
165+ let peer = Peer :: < T > :: new_mut ( & mut * peer) ;
166+ PeerID {
167+ index,
168+ generation : peer. generation ( ) ,
169+ }
164170 }
165171
166172 /// Returns an iterator over all peers connected to this `Host`.
@@ -187,39 +193,16 @@ impl<T> Host<T> {
187193 peers. iter ( ) . map ( |peer| Peer :: new ( & * peer) )
188194 }
189195
190- fn drop_disconnected ( & mut self ) {
191- // Seemingly, the lifetime of an ENetPeer ends when the Disconnect event is received.
192- // However, this is *not really clear* in the ENet docs!
193- // It looks like the Peer *might* live longer, but not shorter, so it should be safe
194- // to destroy the associated data (if any) here.
195- if let Some ( idx) = self . disconnect_drop . take ( ) {
196- self . peer_mut ( idx)
197- . expect ( "Invalid PeerID in disconnect_drop in enet::Host" )
198- . set_data ( None ) ;
199- }
200- }
201-
202- fn process_event ( & mut self , sys_event : ENetEvent ) -> Option < Event > {
203- self . drop_disconnected ( ) ;
204-
205- let event = Event :: from_sys_event ( sys_event, self ) ;
206- if let Some ( Event {
207- peer_id,
208- kind : EventKind :: Disconnect { .. } ,
209- } ) = event
210- {
211- self . disconnect_drop = Some ( peer_id) ;
212- }
213-
214- event
196+ fn process_event ( & ' _ mut self , sys_event : ENetEvent ) -> Option < Event < ' _ , T > > {
197+ Event :: from_sys_event ( sys_event, self )
215198 }
216199
217200 /// Maintains this host and delivers an event if available.
218201 ///
219202 /// This should be called regularly for ENet to work properly with good performance.
220203 ///
221204 /// The function won't block if `timeout` is less than 1ms.
222- pub fn service ( & mut self , timeout : Duration ) -> Result < Option < Event > , Error > {
205+ pub fn service ( & ' _ mut self , timeout : Duration ) -> Result < Option < Event < ' _ , T > > , Error > {
223206 // ENetEvent is Copy (aka has no Drop impl), so we don't have to make sure we `mem::forget` it later on
224207 let mut sys_event = MaybeUninit :: uninit ( ) ;
225208
@@ -244,7 +227,7 @@ impl<T> Host<T> {
244227
245228 /// Checks for any queued events on this `Host` and dispatches one if
246229 /// available
247- pub fn check_events ( & mut self ) -> Result < Option < Event > , Error > {
230+ pub fn check_events ( & ' _ mut self ) -> Result < Option < Event < ' _ , T > > , Error > {
248231 // ENetEvent is Copy (aka has no Drop impl), so we don't have to make sure we
249232 // `mem::forget` it later on
250233 let mut sys_event = MaybeUninit :: uninit ( ) ;
@@ -298,7 +281,7 @@ impl<T> Drop for Host<T> {
298281 /// Call the corresponding ENet cleanup-function(s).
299282 fn drop ( & mut self ) {
300283 for peer in self . peers_mut ( ) {
301- peer. set_data ( None ) ;
284+ peer. drop_raw_data ( ) ;
302285 }
303286
304287 unsafe {
0 commit comments