@pocketrts/lockstep 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -4
- package/dist/lockstep.d.ts +43 -6
- package/dist/lockstep.js +73 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ const ls = new Lockstep({
|
|
|
70
70
|
|------|------|
|
|
71
71
|
| `send(to, msg)` | Deliver `msg` reliably and in order to member `to` or to all other members (`'all'`). Messages are plain JSON. It may throw for a turn (for example over a size limit): the scheduler logs it and carries on. |
|
|
72
72
|
| `dropped(id, reason)` | The host's verdict reached this peer: close the link to `id` and remove it from your member list. Called once per dropped member on every remaining peer. |
|
|
73
|
-
| `selfDropped(reason)` | This peer is out of the match. Called once; the scheduler is closed afterwards. Show the reason and leave. |
|
|
73
|
+
| `selfDropped(reason)` | This peer is out of the match (dropped by the host, isolated, or hidden or frozen too long). Called once; the scheduler is closed afterwards. Show the reason and leave. |
|
|
74
74
|
| `dropHost()` | The host was silent for `silentMs`, or proved gone after someone else claimed to be host: ask the connection layer to drop the host. The connection layer then picks the new host and you call `hostChanged`. |
|
|
75
75
|
| `status()` | Something the UI may show changed: `phase`, `waiting`, `migrating`, `loaded`. |
|
|
76
76
|
|
|
@@ -188,11 +188,12 @@ Games turn these codes into their own text.
|
|
|
188
188
|
| `stall` | `DropReason` | It blocked everyone and made no progress for `stallMs`. |
|
|
189
189
|
| `load` | `DropReason` | It was not ready within `loadMs`. |
|
|
190
190
|
| `isolated` | `SelfDropReason` only | This peer became host, but no other member reported within `migrateMs` (it cannot tell "everyone else left at once" from "I am cut off", so it leaves). Two-peer matches are not affected. |
|
|
191
|
-
| `hidden` | `SelfDropReason` only | The page stayed in the background for `silentMs` or longer. |
|
|
191
|
+
| `hidden` | `SelfDropReason` only | The page stayed in the background for `silentMs` or longer, or its `update` stopped for the freeze limit (see "Freezes" below) while it was hidden. |
|
|
192
|
+
| `frozen` | `SelfDropReason` only | This peer was out of touch for the freeze limit (see "Freezes" below) with the page not hidden: the others may already have replaced or dropped it, so it leaves instead of playing on alone. |
|
|
192
193
|
|
|
193
194
|
## Parameters
|
|
194
195
|
|
|
195
|
-
Every peer in a match must use the same values
|
|
196
|
+
Every peer in a match must use the same values, except `freezeMarginMs`. Defaults are Pocket War's. `lockstepOptions()` and the `Lockstep` constructor throw a `RangeError` when `turnTicks`, `turnMs`, `lead`, `keepTurns`, `lagTurns`, `checkTurns`, `maxFrameSteps` or `turnBytes` is not a positive finite integer, when any other option is not a non-negative finite number, or when `keepTurns` is not greater than `lead`.
|
|
196
197
|
|
|
197
198
|
| Option | Default | Meaning |
|
|
198
199
|
|--------|---------|---------|
|
|
@@ -205,11 +206,12 @@ Every peer in a match must use the same values. Defaults are Pocket War's. `lock
|
|
|
205
206
|
| `waitKeepMs` | 1500 | Members that blocked this recently stay in `waiting`. |
|
|
206
207
|
| `waitBeatMs` | 500 | Wait-list broadcast interval while halted (also the host heartbeat). |
|
|
207
208
|
| `stallMs` | 10000 | A blocker without progress this long is dropped (`stall`). |
|
|
208
|
-
| `silentMs` | 10000 | Host silence before members drop the host; background time before `hidden
|
|
209
|
+
| `silentMs` | 10000 | Host silence before members drop the host; background time before `hidden`; with `freezeMarginMs`, the freeze limit. |
|
|
209
210
|
| `staleMs` | 1500 | How long a rival host claim is held while our host may still speak. |
|
|
210
211
|
| `migrateMs` | 5000 | Time a new host waits for member reports. |
|
|
211
212
|
| `checkTurns` | 10 | Checksums compared at the end of turn n when (n + 1) is a multiple of this. |
|
|
212
213
|
| `frozenMs` | 2000 | A gap between `update` calls above this means this peer was frozen; timers are not charged for it. |
|
|
214
|
+
| `freezeMarginMs` | 2000 | Margin below `silentMs` for the freeze limit (see "Freezes"). Unlike the others, it need not match across peers. |
|
|
213
215
|
| `loadMs` | 60000 | Loading deadline; peers not ready are dropped (`load`). |
|
|
214
216
|
| `loadBeatMs` | 500 | Loading-status broadcast interval (also the host heartbeat). |
|
|
215
217
|
| `turnBytes` | 40000 | Command bytes per turn; the rest waits. Keep it below your transport's message limit (64 KB in `@pocketrts/net`). |
|
|
@@ -226,6 +228,18 @@ Every peer in a match must use the same values. Defaults are Pocket War's. `lock
|
|
|
226
228
|
- The host's verdicts travel inside turns (`drop`), so every peer applies them at the same point in the command stream, together with the `dropCommands` inserted in that turn.
|
|
227
229
|
- When the host goes, the connection layer picks the next host (Pocket War: the lowest remaining id). The new host asks everyone for their last turn, fills the gaps, announces the old host's drop, and continues. A peer that only lost its own link to the host is told `stale` by the others and leaves.
|
|
228
230
|
|
|
231
|
+
## Freezes
|
|
232
|
+
|
|
233
|
+
A page can stop running (blocked main thread, long garbage collection, a hidden tab) long enough for the others to replace or drop it. Such a peer would otherwise carry on alone once it thaws, and the match would split into two that each believe the other left. So a peer that was out of touch for the freeze limit leaves with `frozen` (or `hidden`, when `visibility(true)` was called meanwhile).
|
|
234
|
+
|
|
235
|
+
- Limit: `silentMs` − `freezeMarginMs`, but never below the host's longest normal gap between broadcasts plus `frozenMs`. 8 s with the defaults.
|
|
236
|
+
- Host: leaves when the time since its last broadcast to everyone reaches the limit, while loading or running (also as a new host during migration). It does not leave in `over` or when it is the only member left.
|
|
237
|
+
- Member: leaves only while running, when the time since its last `update()` (above `frozenMs`) reaches the limit. A freeze while loading is not judged (the connection layer's timeout is relaxed then and `loadMs` rules), except that a `memberLost` / `hostChanged` report right after such a freeze counts it.
|
|
238
|
+
- Checked in `update()`, at the start of `memberLost()` and `hostChanged()`, and on the host at the start of `receive()`, since after a freeze those can run before the first `update()`. A member does not check in `receive()`, so a queued `kicked` still gives its real reason.
|
|
239
|
+
- Keep calling `visibility()` so a hidden page reports `hidden`; a game that drives `update()` only from the render loop is still covered on the host, which counts from its last broadcast.
|
|
240
|
+
- It guarantees no split only while the connection layer's link timeout in a match is at least `silentMs` and `silentMs` − `freezeMarginMs` is above the floor. A freeze between the limit and `silentMs` ends that peer's match although the others might not have replaced it yet; this is accepted.
|
|
241
|
+
- The wire format is unchanged, so peers with and without this behaviour can share a match; only the ones that have it protect themselves.
|
|
242
|
+
|
|
229
243
|
## Limitations
|
|
230
244
|
|
|
231
245
|
- Determinism is your job: the scheduler only guarantees that every peer runs the same commands in the same slot. Use checksums to catch divergence; a diverging peer is dropped, it is never resynchronized.
|
package/dist/lockstep.d.ts
CHANGED
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
* host's drop (and its `dropCommands`) goes into the first new turn. If a peer claims to be the new host while the
|
|
22
22
|
* old host is still alive for others (only the claimant lost the old host), they answer `stale` and the claimant
|
|
23
23
|
* drops itself.
|
|
24
|
+
* - Freezes: a peer out of touch long enough that the others may already have replaced or dropped it (a page frozen
|
|
25
|
+
* or hidden for about `silentMs`) leaves on its own instead of playing on alone next to them.
|
|
24
26
|
*/
|
|
25
27
|
/** Tunable parameters. Defaults are `DEFAULT_LOCKSTEP_OPTIONS`. */
|
|
26
28
|
export interface LockstepOptions {
|
|
@@ -43,8 +45,8 @@ export interface LockstepOptions {
|
|
|
43
45
|
/** A blocking member with no progress for this long (ms) is dropped. */
|
|
44
46
|
stallMs: number;
|
|
45
47
|
/**
|
|
46
|
-
* A member that hears nothing from the host for this long (ms) treats the host as gone; a page hidden
|
|
47
|
-
*
|
|
48
|
+
* A member that hears nothing from the host for this long (ms) treats the host as gone; a page hidden this long
|
|
49
|
+
* drops itself (a freeze or hidden time noticed by `update` drops it sooner, see `freezeMarginMs`).
|
|
48
50
|
*/
|
|
49
51
|
silentMs: number;
|
|
50
52
|
/** Someone claims to be the new host while ours is still known: wait this long (ms) to see if ours still talks. */
|
|
@@ -55,6 +57,15 @@ export interface LockstepOptions {
|
|
|
55
57
|
checkTurns: number;
|
|
56
58
|
/** Two `update` calls further apart than this (ms) mean this peer was frozen. */
|
|
57
59
|
frozenMs: number;
|
|
60
|
+
/**
|
|
61
|
+
* Safety margin (ms) for leaving after a freeze: a peer out of touch so long that the others may already have
|
|
62
|
+
* replaced or dropped it leaves on its own (`frozen`, or `hidden`) once that lasted `silentMs` minus this margin.
|
|
63
|
+
* The margin covers the gap between the host's messages and network delay. The limit never goes below the host's
|
|
64
|
+
* longest normal gap between broadcasts plus `frozenMs`. Need not match across peers. No split is guaranteed only
|
|
65
|
+
* while the connection layer's link timeout in a match is at least `silentMs` and `silentMs - freezeMarginMs` is
|
|
66
|
+
* above that floor.
|
|
67
|
+
*/
|
|
68
|
+
freezeMarginMs: number;
|
|
58
69
|
/** Time (ms) to wait for everyone to load at the start; peers not ready by then are dropped. */
|
|
59
70
|
loadMs: number;
|
|
60
71
|
/** Interval (ms) of the load status broadcast during loading (doubles as the host heartbeat). */
|
|
@@ -78,9 +89,10 @@ export type DropReason = 'left' | 'desync' | 'stall' | 'load';
|
|
|
78
89
|
export declare const DROP_REASONS: readonly DropReason[];
|
|
79
90
|
/**
|
|
80
91
|
* Why this peer left the match: a `DropReason` announced by the host, `isolated` (became host but no member reported),
|
|
81
|
-
*
|
|
92
|
+
* `hidden` (the page stayed in the background long enough that the others may have dropped it), or `frozen` (the page
|
|
93
|
+
* froze in the foreground that long).
|
|
82
94
|
*/
|
|
83
|
-
export type SelfDropReason = DropReason | 'isolated' | 'hidden';
|
|
95
|
+
export type SelfDropReason = DropReason | 'isolated' | 'hidden' | 'frozen';
|
|
84
96
|
/** One turn: `c` holds [issuing member, its sequence number, command data] in host order; `drop` lists dropped members. */
|
|
85
97
|
export interface Turn {
|
|
86
98
|
n: number;
|
|
@@ -160,7 +172,7 @@ export interface LockstepHooks {
|
|
|
160
172
|
send(to: number | 'all', msg: LockstepMsg): void;
|
|
161
173
|
/** The host dropped a member (a turn's `drop`): close the link to it and remove it from the member list. */
|
|
162
174
|
dropped(id: number, reason: DropReason): void;
|
|
163
|
-
/** This peer is out: dropped by the host, isolated, or hidden too long. Called once; the scheduler is closed. */
|
|
175
|
+
/** This peer is out: dropped by the host, isolated, or hidden or frozen too long. Called once; the scheduler is closed. */
|
|
164
176
|
selfDropped(reason: SelfDropReason): void;
|
|
165
177
|
/** The host was silent too long or proved gone: ask the connection layer to treat it as gone (then call `hostChanged`). */
|
|
166
178
|
dropHost(): void;
|
|
@@ -220,7 +232,13 @@ export declare class Lockstep {
|
|
|
220
232
|
private selfReady;
|
|
221
233
|
private closed;
|
|
222
234
|
private hiddenAt;
|
|
235
|
+
/** The page was hidden at some point since the last `update` (a freeze it caused counts as `hidden`). */
|
|
236
|
+
private hiddenSinceUpdate;
|
|
223
237
|
private lastUpdate;
|
|
238
|
+
/** The last freeze `update` noticed (any phase): its length and when it ended. */
|
|
239
|
+
private lastFreeze;
|
|
240
|
+
/** When this peer last sent to everyone (as host: what members measure its silence from). */
|
|
241
|
+
private lastSentAll;
|
|
224
242
|
private nextTurn;
|
|
225
243
|
private inbox;
|
|
226
244
|
/** Highest sequence number accepted (into the inbox or a turn) per member. */
|
|
@@ -281,8 +299,25 @@ export declare class Lockstep {
|
|
|
281
299
|
/** Call every frame (or on a timer). */
|
|
282
300
|
update(): void;
|
|
283
301
|
private hostUpdate;
|
|
302
|
+
/**
|
|
303
|
+
* Leave (`frozen`, or `hidden` when the page was hidden meanwhile) when this peer was out of touch long enough that
|
|
304
|
+
* the others may already have dropped it: carrying on could split the match into two that each believe the other
|
|
305
|
+
* left. Checked in `update`, before acting on a connection layer report, and on the host before handling a message:
|
|
306
|
+
* after a freeze those can be handled before the first `update` (and a host's reply would make it look fresh).
|
|
307
|
+
* - The host is replaced when members hear nothing from it for `silentMs` (or the connection layer's link to it
|
|
308
|
+
* times out, assumed no sooner), so its silence counts from its last broadcast. Unless frozen it broadcasts at
|
|
309
|
+
* least every `loadBeatMs` or so; this also covers a host frozen before its first `update`.
|
|
310
|
+
* - A member is dropped when the host's link to it times out; only a running member's freeze is judged (while
|
|
311
|
+
* loading the link timeout is relaxed and `loadMs` rules), by the time since its last `update`. A connection
|
|
312
|
+
* layer report (`report`) shortly after a freeze also counts that freeze when it ended before the match started:
|
|
313
|
+
* the host may have started meanwhile, restored its link timeout and dropped this member.
|
|
314
|
+
*/
|
|
315
|
+
private leaveIfOutOfTouch;
|
|
284
316
|
private thaw;
|
|
285
|
-
/**
|
|
317
|
+
/**
|
|
318
|
+
* The page went to the background / came back: away for `silentMs` or longer drops this peer (`hidden`). When the
|
|
319
|
+
* game's `update` stopped meanwhile, `update` may drop it sooner (see `freezeMarginMs`).
|
|
320
|
+
*/
|
|
286
321
|
visibility(hidden: boolean): void;
|
|
287
322
|
/** This peer's match is over (results screen): stop ordering and stop checking the host's silence. */
|
|
288
323
|
finish(): void;
|
|
@@ -310,6 +345,8 @@ export declare class Lockstep {
|
|
|
310
345
|
private reportTo;
|
|
311
346
|
/** New host: once all reports are in (or timed out), resend turns and continue ordering. */
|
|
312
347
|
private finishMigration;
|
|
348
|
+
/** Send to everyone and note when (members measure the host's silence from its messages). */
|
|
349
|
+
private sendAll;
|
|
313
350
|
private setWaiting;
|
|
314
351
|
private die;
|
|
315
352
|
}
|
package/dist/lockstep.js
CHANGED
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
* host's drop (and its `dropCommands`) goes into the first new turn. If a peer claims to be the new host while the
|
|
22
22
|
* old host is still alive for others (only the claimant lost the old host), they answer `stale` and the claimant
|
|
23
23
|
* drops itself.
|
|
24
|
+
* - Freezes: a peer out of touch long enough that the others may already have replaced or dropped it (a page frozen
|
|
25
|
+
* or hidden for about `silentMs`) leaves on its own instead of playing on alone next to them.
|
|
24
26
|
*/
|
|
25
27
|
export const DEFAULT_LOCKSTEP_OPTIONS = Object.freeze({
|
|
26
28
|
turnTicks: 3,
|
|
@@ -37,6 +39,7 @@ export const DEFAULT_LOCKSTEP_OPTIONS = Object.freeze({
|
|
|
37
39
|
migrateMs: 5000,
|
|
38
40
|
checkTurns: 10,
|
|
39
41
|
frozenMs: 2000,
|
|
42
|
+
freezeMarginMs: 2000,
|
|
40
43
|
loadMs: 60_000,
|
|
41
44
|
loadBeatMs: 500,
|
|
42
45
|
turnBytes: 40_000,
|
|
@@ -108,7 +111,13 @@ export class Lockstep {
|
|
|
108
111
|
selfReady = false;
|
|
109
112
|
closed = false;
|
|
110
113
|
hiddenAt = -1;
|
|
114
|
+
/** The page was hidden at some point since the last `update` (a freeze it caused counts as `hidden`). */
|
|
115
|
+
hiddenSinceUpdate = false;
|
|
111
116
|
lastUpdate = -1;
|
|
117
|
+
/** The last freeze `update` noticed (any phase): its length and when it ended. */
|
|
118
|
+
lastFreeze = { ms: 0, end: -Infinity };
|
|
119
|
+
/** When this peer last sent to everyone (as host: what members measure its silence from). */
|
|
120
|
+
lastSentAll;
|
|
112
121
|
// ---- host ----
|
|
113
122
|
nextTurn = 0;
|
|
114
123
|
inbox = [];
|
|
@@ -153,6 +162,7 @@ export class Lockstep {
|
|
|
153
162
|
this.members.add(m);
|
|
154
163
|
const now = cfg.now();
|
|
155
164
|
this.lastHost = now;
|
|
165
|
+
this.lastSentAll = now;
|
|
156
166
|
this.loadUntil = now + this.options.loadMs;
|
|
157
167
|
}
|
|
158
168
|
get isHost() {
|
|
@@ -242,7 +252,7 @@ export class Lockstep {
|
|
|
242
252
|
for (const m of this.members)
|
|
243
253
|
if (!this.loaded.has(m))
|
|
244
254
|
this.drop(m, 'load');
|
|
245
|
-
this.
|
|
255
|
+
this.sendAll({ k: 'go' });
|
|
246
256
|
this.startRunning(now);
|
|
247
257
|
for (const m of this.members)
|
|
248
258
|
this.progress.set(m, { n: -1, at: now });
|
|
@@ -257,10 +267,15 @@ export class Lockstep {
|
|
|
257
267
|
return;
|
|
258
268
|
const o = this.options;
|
|
259
269
|
const now = this.cfg.now();
|
|
270
|
+
if (this.leaveIfOutOfTouch(now))
|
|
271
|
+
return;
|
|
272
|
+
this.hiddenSinceUpdate = false;
|
|
260
273
|
// This peer was frozen (loading, background page, a long frame): take the backlog first, and do not count the
|
|
261
274
|
// frozen time against others' silence or progress
|
|
262
|
-
if (this.lastUpdate >= 0 && now - this.lastUpdate > o.frozenMs)
|
|
275
|
+
if (this.lastUpdate >= 0 && now - this.lastUpdate > o.frozenMs) {
|
|
276
|
+
this.lastFreeze = { ms: now - this.lastUpdate, end: now };
|
|
263
277
|
this.thaw(now);
|
|
278
|
+
}
|
|
264
279
|
this.lastUpdate = now;
|
|
265
280
|
if (this.claim && now - this.claim.at >= o.staleMs && now - this.lastHost >= o.staleMs) {
|
|
266
281
|
// Our host has said nothing since someone else claimed to be host: treat it as gone
|
|
@@ -288,9 +303,9 @@ export class Lockstep {
|
|
|
288
303
|
if (now >= this.migBeat) {
|
|
289
304
|
this.migBeat = now + o.loadBeatMs;
|
|
290
305
|
if (this.phase === 'loading')
|
|
291
|
-
this.
|
|
306
|
+
this.sendAll({ k: 'load', done: [...this.loaded] });
|
|
292
307
|
else
|
|
293
|
-
this.
|
|
308
|
+
this.sendAll({ k: 'wait', ids: [...this.mig.left] });
|
|
294
309
|
}
|
|
295
310
|
if (now >= this.mig.until) {
|
|
296
311
|
// Not a single report: from here "everyone else dropped at once" and "we lost the network / froze and were
|
|
@@ -310,7 +325,7 @@ export class Lockstep {
|
|
|
310
325
|
return this.go(now);
|
|
311
326
|
if (now >= this.loadBeat) {
|
|
312
327
|
this.loadBeat = now + o.loadBeatMs;
|
|
313
|
-
this.
|
|
328
|
+
this.sendAll({ k: 'load', done: [...this.loaded] });
|
|
314
329
|
}
|
|
315
330
|
return;
|
|
316
331
|
}
|
|
@@ -331,7 +346,7 @@ export class Lockstep {
|
|
|
331
346
|
if (changed || (show.length && now - this.waitSent >= o.waitBeatMs)) {
|
|
332
347
|
this.waitSent = now;
|
|
333
348
|
this.setWaiting(show);
|
|
334
|
-
this.
|
|
349
|
+
this.sendAll({ k: 'wait', ids: show });
|
|
335
350
|
}
|
|
336
351
|
if (blockers.length) {
|
|
337
352
|
this.halted = true;
|
|
@@ -356,6 +371,38 @@ export class Lockstep {
|
|
|
356
371
|
this.beatAt += o.turnMs;
|
|
357
372
|
}
|
|
358
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Leave (`frozen`, or `hidden` when the page was hidden meanwhile) when this peer was out of touch long enough that
|
|
376
|
+
* the others may already have dropped it: carrying on could split the match into two that each believe the other
|
|
377
|
+
* left. Checked in `update`, before acting on a connection layer report, and on the host before handling a message:
|
|
378
|
+
* after a freeze those can be handled before the first `update` (and a host's reply would make it look fresh).
|
|
379
|
+
* - The host is replaced when members hear nothing from it for `silentMs` (or the connection layer's link to it
|
|
380
|
+
* times out, assumed no sooner), so its silence counts from its last broadcast. Unless frozen it broadcasts at
|
|
381
|
+
* least every `loadBeatMs` or so; this also covers a host frozen before its first `update`.
|
|
382
|
+
* - A member is dropped when the host's link to it times out; only a running member's freeze is judged (while
|
|
383
|
+
* loading the link timeout is relaxed and `loadMs` rules), by the time since its last `update`. A connection
|
|
384
|
+
* layer report (`report`) shortly after a freeze also counts that freeze when it ended before the match started:
|
|
385
|
+
* the host may have started meanwhile, restored its link timeout and dropped this member.
|
|
386
|
+
*/
|
|
387
|
+
leaveIfOutOfTouch(now, report = false) {
|
|
388
|
+
if (this.closed || this.phase === 'over' || this.members.size < 2)
|
|
389
|
+
return false;
|
|
390
|
+
const o = this.options;
|
|
391
|
+
// Never below the host's longest normal silence: a halt shows the wait list only after waitShowMs, and a frame
|
|
392
|
+
// up to frozenMs long does not count as a freeze
|
|
393
|
+
const floor = Math.max(o.loadBeatMs, o.waitBeatMs, o.waitShowMs + o.turnMs) + o.frozenMs;
|
|
394
|
+
const limit = Math.max(o.silentMs - o.freezeMarginMs, floor);
|
|
395
|
+
let frozenFor = this.lastUpdate >= 0 ? now - this.lastUpdate : 0;
|
|
396
|
+
if (report && now - this.lastFreeze.end <= o.frozenMs)
|
|
397
|
+
frozenFor = Math.max(frozenFor, this.lastFreeze.ms);
|
|
398
|
+
const out = this.isHost
|
|
399
|
+
? now - this.lastSentAll >= limit
|
|
400
|
+
: this.phase === 'running' && frozenFor > o.frozenMs && frozenFor >= limit;
|
|
401
|
+
if (!out)
|
|
402
|
+
return false;
|
|
403
|
+
this.die(this.hiddenSinceUpdate || this.hiddenAt >= 0 ? 'hidden' : 'frozen');
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
359
406
|
thaw(now) {
|
|
360
407
|
this.lastHost = Math.max(this.lastHost, now);
|
|
361
408
|
for (const p of this.progress.values())
|
|
@@ -366,11 +413,15 @@ export class Lockstep {
|
|
|
366
413
|
this.mig.until = Math.max(this.mig.until, now + this.options.migrateMs);
|
|
367
414
|
this.lastCheck = now;
|
|
368
415
|
}
|
|
369
|
-
/**
|
|
416
|
+
/**
|
|
417
|
+
* The page went to the background / came back: away for `silentMs` or longer drops this peer (`hidden`). When the
|
|
418
|
+
* game's `update` stopped meanwhile, `update` may drop it sooner (see `freezeMarginMs`).
|
|
419
|
+
*/
|
|
370
420
|
visibility(hidden) {
|
|
371
421
|
const now = this.cfg.now();
|
|
372
422
|
if (hidden) {
|
|
373
423
|
this.hiddenAt = now;
|
|
424
|
+
this.hiddenSinceUpdate = true;
|
|
374
425
|
return;
|
|
375
426
|
}
|
|
376
427
|
const away = this.hiddenAt >= 0 ? now - this.hiddenAt : 0;
|
|
@@ -409,7 +460,7 @@ export class Lockstep {
|
|
|
409
460
|
if (this.drops.length)
|
|
410
461
|
t.drop = this.drops.splice(0);
|
|
411
462
|
try {
|
|
412
|
-
this.
|
|
463
|
+
this.sendAll({ k: 'turn', t });
|
|
413
464
|
}
|
|
414
465
|
catch (e) {
|
|
415
466
|
// A failed send (for example over the connection layer's message limit) must not throw into the main loop;
|
|
@@ -498,6 +549,9 @@ export class Lockstep {
|
|
|
498
549
|
receive(from, m) {
|
|
499
550
|
if (this.closed || typeof m !== 'object' || m === null)
|
|
500
551
|
return;
|
|
552
|
+
// A thawed host could answer queued messages with a broadcast; a member reads its queued verdict (kicked) first
|
|
553
|
+
if (this.isHost && this.leaveIfOutOfTouch(this.cfg.now()))
|
|
554
|
+
return;
|
|
501
555
|
if (from === this.host)
|
|
502
556
|
this.lastHost = this.cfg.now();
|
|
503
557
|
if (this.claim && from === this.host && !this.isHost) {
|
|
@@ -621,6 +675,8 @@ export class Lockstep {
|
|
|
621
675
|
}
|
|
622
676
|
/** The connection layer lost a member (host changes go through `hostChanged`): the host drops it, members await the verdict. */
|
|
623
677
|
memberLost(id) {
|
|
678
|
+
if (this.leaveIfOutOfTouch(this.cfg.now(), true))
|
|
679
|
+
return;
|
|
624
680
|
if (this.isHost) {
|
|
625
681
|
this.drop(id, 'left');
|
|
626
682
|
if (this.mig)
|
|
@@ -636,6 +692,8 @@ export class Lockstep {
|
|
|
636
692
|
hostChanged(id) {
|
|
637
693
|
if (this.closed || id === this.host)
|
|
638
694
|
return;
|
|
695
|
+
if (this.leaveIfOutOfTouch(this.cfg.now(), true))
|
|
696
|
+
return;
|
|
639
697
|
const old = this.host;
|
|
640
698
|
this.host = id;
|
|
641
699
|
// The old host left but its drop is not in a turn yet: remember it, whoever becomes host announces it (none is
|
|
@@ -665,7 +723,7 @@ export class Lockstep {
|
|
|
665
723
|
// Migration while loading: members say again whether they are ready when they report
|
|
666
724
|
this.loaded = new Set(this.selfReady ? [this.self] : []);
|
|
667
725
|
this.nextTurn = this.recv + 1;
|
|
668
|
-
this.
|
|
726
|
+
this.sendAll({ k: 'mig', last: this.recv });
|
|
669
727
|
// Members gone but unannounced (the old host and earlier hosts that dropped together): announce them with
|
|
670
728
|
// their drop commands in the first new turn (sent after migration completes)
|
|
671
729
|
for (const gone of this.unannounced) {
|
|
@@ -741,10 +799,15 @@ export class Lockstep {
|
|
|
741
799
|
this.migrating = false;
|
|
742
800
|
// Migration heartbeats carried a wait list (members yet to report): clear it explicitly so members stop showing it
|
|
743
801
|
this.waiting = [];
|
|
744
|
-
this.
|
|
802
|
+
this.sendAll({ k: 'wait', ids: [] });
|
|
745
803
|
this.hooks.status();
|
|
746
804
|
}
|
|
747
805
|
// ------------------------------------------------------------------ misc
|
|
806
|
+
/** Send to everyone and note when (members measure the host's silence from its messages). */
|
|
807
|
+
sendAll(msg) {
|
|
808
|
+
this.lastSentAll = this.cfg.now();
|
|
809
|
+
this.hooks.send('all', msg);
|
|
810
|
+
}
|
|
748
811
|
setWaiting(ids) {
|
|
749
812
|
if (ids.length === this.waiting.length && ids.every((v, i) => v === this.waiting[i]))
|
|
750
813
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pocketrts/lockstep",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Deterministic lockstep turn scheduler for small peer-to-peer games: host-ordered turns, catch-up, checksums and host migration",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Junjie Bai <bjj19851010@gmail.com>",
|