1010using Backdash . Synchronizing ;
1111using Backdash . Synchronizing . Input ;
1212using Backdash . Synchronizing . State ;
13- using Timer = System . Timers . Timer ;
1413
1514namespace Backdash . Network ;
1615
@@ -27,11 +26,7 @@ sealed class PeerConnection<TInput> : IDisposable where TInput : unmanaged
2726 readonly ProtocolInputBuffer < TInput > inputBuffer ;
2827 readonly ChecksumStore checksumStore ;
2928
30- readonly Timer qualityReportTimer ;
31- readonly Timer networkStatsTimer ;
32- readonly Timer keepAliveTimer ;
33- readonly Timer resendInputsTimer ;
34- readonly Timer consistencyCheckTimer ;
29+ readonly ConnectionTimers timers ;
3530 readonly bool disconnectCheckEnabled ;
3631 long startedAt ;
3732
@@ -58,24 +53,16 @@ ChecksumStore checksumStore
5853 this . outbox = outbox ;
5954 this . inputBuffer = inputBuffer ;
6055 this . checksumStore = checksumStore ;
56+ timers = new ( options ) ;
6157 disconnectCheckEnabled = options . IsDisconnectTimeoutEnabled ( ) ;
6258
63- keepAliveTimer = new ( options . KeepAliveInterval ) ;
64- keepAliveTimer . Elapsed += OnKeepAliveTick ;
59+ timers . KeepAlive . Elapsed += OnKeepAliveTick ;
60+ timers . ResendInputs . Elapsed += OnResendInputs ;
61+ timers . QualityReport . Elapsed += OnQualityReportTick ;
62+ timers . NetworkStats . Elapsed += OnNetworkStatsTick ;
63+ timers . ConsistencyCheck . Elapsed += OnConsistencyCheck ;
6564
66- resendInputsTimer = new ( options . ResendInputInterval ) ;
67- resendInputsTimer . Elapsed += OnResendInputs ;
68-
69- qualityReportTimer = new ( options . QualityReportInterval ) ;
70- qualityReportTimer . Elapsed += OnQualityReportTick ;
71-
72- networkStatsTimer = new ( options . NetworkPackageStatsInterval ) ;
73- networkStatsTimer . Elapsed += OnNetworkStatsTick ;
74-
75- consistencyCheckTimer = new ( options . ConsistencyCheckInterval ) ;
76- consistencyCheckTimer . Elapsed += OnConsistencyCheck ;
77-
78- state . StoppingToken . Register ( StopTimers ) ;
65+ state . StoppingToken . Register ( timers . Stop ) ;
7966 }
8067
8168 public void Dispose ( )
@@ -84,46 +71,14 @@ public void Dispose()
8471 if ( ! state . StoppingTokenSource . IsCancellationRequested )
8572 state . StoppingTokenSource . Cancel ( ) ;
8673
87- StopTimers ( ) ;
74+ timers . Stop ( ) ;
8875 DispatchDisconnectEvent ( ) ;
89-
90- keepAliveTimer . Elapsed -= OnKeepAliveTick ;
91- resendInputsTimer . Elapsed -= OnKeepAliveTick ;
92- qualityReportTimer . Elapsed -= OnQualityReportTick ;
93- networkStatsTimer . Elapsed -= OnNetworkStatsTick ;
94- consistencyCheckTimer . Elapsed -= OnConsistencyCheck ;
95-
96- keepAliveTimer . Dispose ( ) ;
97- resendInputsTimer . Dispose ( ) ;
98- qualityReportTimer . Dispose ( ) ;
99- networkStatsTimer . Dispose ( ) ;
100- consistencyCheckTimer . Dispose ( ) ;
101- }
102-
103- void StartTimers ( )
104- {
105- keepAliveTimer . Start ( ) ;
106- qualityReportTimer . Start ( ) ;
107- resendInputsTimer . Start ( ) ;
108-
109- if ( options . IsNetworkPackageStatsEnabled ( ) )
110- networkStatsTimer . Start ( ) ;
111-
112- if ( options . IsConsistencyCheckEnabled ( ) )
113- consistencyCheckTimer . Start ( ) ;
114- }
115-
116- void StopTimers ( )
117- {
118- keepAliveTimer . Stop ( ) ;
119- qualityReportTimer . Stop ( ) ;
120- resendInputsTimer . Stop ( ) ;
121-
122- if ( options . IsNetworkPackageStatsEnabled ( ) )
123- networkStatsTimer . Stop ( ) ;
124-
125- if ( options . IsConsistencyCheckEnabled ( ) )
126- consistencyCheckTimer . Stop ( ) ;
76+ timers . KeepAlive . Elapsed -= OnKeepAliveTick ;
77+ timers . ResendInputs . Elapsed -= OnKeepAliveTick ;
78+ timers . QualityReport . Elapsed -= OnQualityReportTick ;
79+ timers . NetworkStats . Elapsed -= OnNetworkStatsTick ;
80+ timers . ConsistencyCheck . Elapsed -= OnConsistencyCheck ;
81+ timers . Dispose ( ) ;
12782 }
12883
12984 public void Disconnect ( )
@@ -149,7 +104,7 @@ public void Disconnect()
149104 public void Start ( )
150105 {
151106 if ( startedAt is 0 )
152- StartTimers ( ) ;
107+ timers . Start ( ) ;
153108
154109 startedAt = Stopwatch . GetTimestamp ( ) ;
155110 }
@@ -260,41 +215,34 @@ void CheckDisconnection()
260215 }
261216 }
262217
263- readonly object eventLocker = new ( ) ;
264218
265219 bool DispatchInterruptedEvent ( TimeSpan timeout )
266220 {
267- lock ( eventLocker )
268- {
269- if ( state . Connection is not { DisconnectNotifySent : false , DisconnectEventSent : false } )
270- return false ;
221+ if ( state . Connection is not { DisconnectNotifySent : false , DisconnectEventSent : false } )
222+ return false ;
271223
272- networkEventHandler . OnNetworkEvent ( state . Player , new ( PeerEvent . ConnectionInterrupted )
224+ networkEventHandler . OnNetworkEvent ( state . Player , new ( PeerEvent . ConnectionInterrupted )
225+ {
226+ ConnectionInterrupted = new ( )
273227 {
274- ConnectionInterrupted = new ( )
275- {
276- DisconnectTimeout = timeout ,
277- } ,
278- } ) ;
228+ DisconnectTimeout = timeout ,
229+ } ,
230+ } ) ;
279231
280- state . Connection . DisconnectNotifySent = true ;
232+ state . Connection . DisconnectNotifySent = true ;
281233
282- return true ;
283- }
234+ return true ;
284235 }
285236
286237 bool DispatchDisconnectEvent ( )
287238 {
288- lock ( eventLocker )
289- {
290- if ( state . Connection . DisconnectEventSent )
291- return false ;
239+ if ( state . Connection . DisconnectEventSent )
240+ return false ;
292241
293- state . Connection . DisconnectEventSent = true ;
294- networkEventHandler . OnNetworkEvent ( PeerEvent . Disconnected , state . Player ) ;
242+ state . Connection . DisconnectEventSent = true ;
243+ networkEventHandler . OnNetworkEvent ( PeerEvent . Disconnected , state . Player ) ;
295244
296- return true ;
297- }
245+ return true ;
298246 }
299247
300248 void OnKeepAliveTick ( object ? sender , ElapsedEventArgs e )
0 commit comments