@@ -7,6 +7,7 @@ FakeLoopbackNetworkModel.js
77
88import { clamp } from "/src/engine/utils/index.js" ;
99import { asPositiveNumber } from '../../../_shared/numberUtils.js' ;
10+ import { createLatencyModel } from "/samples/phase-13/_shared/latencyModel.js" ;
1011
1112const MAX_TRACE_EVENTS = 80 ;
1213
@@ -43,7 +44,12 @@ export default class FakeLoopbackNetworkModel {
4344
4445 this . autoPacketTimerSeconds = 0 ;
4546 this . autoPacketIntervalSeconds = 0.65 ;
46- this . lastAckTimerSeconds = 0 ;
47+ this . inFlightPackets = [ ] ;
48+ this . latencyModel = createLatencyModel ( {
49+ baseRttMs : 30 ,
50+ jitterMs : 4 ,
51+ minOneWayMs : 8
52+ } ) ;
4753
4854 this . traceEvents = [ ] ;
4955
@@ -92,7 +98,7 @@ export default class FakeLoopbackNetworkModel {
9298 if ( normalizedNext === "disconnected" ) {
9399 this . pendingPackets = 0 ;
94100 this . replicationBacklog = 0 ;
95- this . lastAckTimerSeconds = 0 ;
101+ this . inFlightPackets = [ ] ;
96102 }
97103 }
98104
@@ -122,11 +128,20 @@ export default class FakeLoopbackNetworkModel {
122128 this . sentPackets += 1 ;
123129 this . pendingPackets += 1 ;
124130 this . replicationTick += 1 ;
131+ const oneWayDelayMs = this . latencyModel . sampleOneWayDelayMs ( this . elapsedSeconds , sequence ) ;
132+ this . inFlightPackets . push ( {
133+ sequence,
134+ label,
135+ deliverAtSeconds : this . elapsedSeconds + ( oneWayDelayMs / 1000 ) ,
136+ oneWayDelayMs
137+ } ) ;
138+ this . inFlightPackets . sort ( ( left , right ) => left . deliverAtSeconds - right . deliverAtSeconds ) ;
125139
126140 this . pushTrace ( "PACKET_SENT" , {
127141 label,
128142 sequence,
129- pendingPackets : this . pendingPackets
143+ pendingPackets : this . pendingPackets ,
144+ oneWayDelayMs
130145 } ) ;
131146
132147 return true ;
@@ -162,37 +177,32 @@ export default class FakeLoopbackNetworkModel {
162177 this . jitterMs = 0 ;
163178 return ;
164179 }
165-
166- const waveform = Math . sin ( this . elapsedSeconds * 1.8 ) ;
167- const harmonic = Math . sin ( this . elapsedSeconds * 0.45 ) ;
168- this . rttMs = Math . round ( 30 + ( waveform * 7 ) + ( harmonic * 3 ) ) ;
169- this . rttMs = clamp ( this . rttMs , 16 , 70 ) ;
170-
171- this . jitterMs = Math . round ( Math . abs ( Math . cos ( this . elapsedSeconds * 2.2 ) ) * 4 ) ;
172- this . jitterMs = clamp ( this . jitterMs , 1 , 8 ) ;
180+ const snapshot = this . latencyModel . sampleSnapshot ( this . elapsedSeconds , this . nextSequence ) ;
181+ this . rttMs = snapshot . rttMs ;
182+ this . jitterMs = snapshot . jitterMs ;
173183 }
174184
175- updateAcks ( dtSeconds ) {
185+ updateAcks ( ) {
176186 if ( this . phase !== "connected" ) {
177187 return ;
178188 }
179-
180- this . lastAckTimerSeconds += dtSeconds ;
181- const ackInterval = 0.22 + ( this . jitterMs / 1000 ) ;
182-
183- if ( this . pendingPackets > 0 && this . lastAckTimerSeconds >= ackInterval ) {
184- this . lastAckTimerSeconds = 0 ;
185- this . pendingPackets -= 1 ;
186- this . ackedSequence += 1 ;
189+ while ( this . inFlightPackets . length > 0 && this . inFlightPackets [ 0 ] . deliverAtSeconds <= this . elapsedSeconds ) {
190+ const delivered = this . inFlightPackets . shift ( ) ;
191+ if ( ! delivered ) break ;
192+ if ( this . pendingPackets > 0 ) {
193+ this . pendingPackets -= 1 ;
194+ }
195+ this . ackedSequence = Math . max ( this . ackedSequence , Number ( delivered . sequence ) || 0 ) ;
187196 this . receivedPackets += 1 ;
188197
189198 this . pushTrace ( "PACKET_ACKED" , {
190199 sequence : this . ackedSequence ,
191- pendingPackets : this . pendingPackets
200+ pendingPackets : this . pendingPackets ,
201+ oneWayDelayMs : Number ( delivered . oneWayDelayMs || 0 )
192202 } ) ;
193203 }
194204
195- this . replicationBacklog = Math . max ( 0 , this . pendingPackets - 1 ) ;
205+ this . replicationBacklog = Math . max ( 0 , this . inFlightPackets . length ) ;
196206 }
197207
198208 update ( dtSeconds , options = { } ) {
@@ -206,7 +216,7 @@ export default class FakeLoopbackNetworkModel {
206216
207217 this . updatePhaseProgress ( safeDt ) ;
208218 this . updateLatencySnapshot ( ) ;
209- this . updateAcks ( safeDt ) ;
219+ this . updateAcks ( ) ;
210220 }
211221
212222 getSnapshot ( ) {
0 commit comments