@bloopjs/bloop 0.0.79 → 0.0.81
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/dist/bloop.d.ts.map +1 -1
- package/dist/mod.js +297 -83
- package/dist/mod.js.map +6 -7
- package/dist/sim.d.ts +45 -17
- package/dist/sim.d.ts.map +1 -1
- package/dist/system.d.ts +5 -0
- package/dist/system.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/bloop.ts +64 -3
- package/src/sim.ts +158 -26
- package/src/system.ts +8 -0
- package/dist/net.d.ts +0 -60
- package/dist/net.d.ts.map +0 -1
- package/src/net.ts +0 -138
package/src/net.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import type { EnginePointer, WasmEngine } from "@bloopjs/engine";
|
|
2
|
-
import { assert } from "./util";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Network API for packet management in multiplayer sessions.
|
|
6
|
-
*
|
|
7
|
-
* This class provides methods for:
|
|
8
|
-
* - Building outbound packets to send to peers
|
|
9
|
-
* - Processing received packets
|
|
10
|
-
* - Querying peer network state (seq/ack/unacked)
|
|
11
|
-
* - Managing peer connections
|
|
12
|
-
*
|
|
13
|
-
* Access via `sim.net` after initializing a session.
|
|
14
|
-
*/
|
|
15
|
-
export class Net {
|
|
16
|
-
#wasm: WasmEngine;
|
|
17
|
-
#memory: WebAssembly.Memory;
|
|
18
|
-
|
|
19
|
-
constructor(wasm: WasmEngine, memory: WebAssembly.Memory) {
|
|
20
|
-
this.#wasm = wasm;
|
|
21
|
-
this.#memory = memory;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Set the local peer ID for packet encoding.
|
|
26
|
-
* Call this after session init to identify which peer we are.
|
|
27
|
-
*/
|
|
28
|
-
setLocalPeer(peerId: number): void {
|
|
29
|
-
this.#wasm.session_set_local_peer(peerId);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Mark a peer as connected for packet management.
|
|
34
|
-
* Call this when a peer joins the session.
|
|
35
|
-
*/
|
|
36
|
-
connectPeer(peerId: number): void {
|
|
37
|
-
this.#wasm.session_peer_connect(peerId);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Mark a peer as disconnected.
|
|
42
|
-
* Call this when a peer leaves the session.
|
|
43
|
-
*/
|
|
44
|
-
disconnectPeer(peerId: number): void {
|
|
45
|
-
this.#wasm.session_peer_disconnect(peerId);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Get an outbound packet to send to a target peer.
|
|
50
|
-
* Returns a copy of the packet data (caller owns the returned buffer).
|
|
51
|
-
*
|
|
52
|
-
* The packet contains all unacked inputs encoded in wire format.
|
|
53
|
-
*
|
|
54
|
-
* @param targetPeer - Peer ID to send the packet to
|
|
55
|
-
* @returns Packet data to send, or null if no packet available
|
|
56
|
-
*/
|
|
57
|
-
getOutboundPacket(targetPeer: number): Uint8Array<ArrayBuffer> | null {
|
|
58
|
-
// Build the packet in engine memory
|
|
59
|
-
this.#wasm.build_outbound_packet(targetPeer);
|
|
60
|
-
|
|
61
|
-
const len = this.#wasm.get_outbound_packet_len();
|
|
62
|
-
if (len === 0) {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const ptr = this.#wasm.get_outbound_packet();
|
|
67
|
-
assert(ptr > 0, `Invalid outbound packet pointer: ${ptr}`);
|
|
68
|
-
|
|
69
|
-
// Copy from WASM memory
|
|
70
|
-
const memoryView = new Uint8Array(this.#memory.buffer, ptr, len);
|
|
71
|
-
const copy = new Uint8Array(len);
|
|
72
|
-
copy.set(memoryView);
|
|
73
|
-
|
|
74
|
-
return copy;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Process a received packet from a peer.
|
|
79
|
-
* This updates seq/ack state and stores events for rollback.
|
|
80
|
-
*
|
|
81
|
-
* @param data - Packet data received from the network
|
|
82
|
-
* @throws Error if packet is invalid
|
|
83
|
-
*/
|
|
84
|
-
receivePacket(data: Uint8Array): void {
|
|
85
|
-
if (data.length === 0) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Allocate memory and copy packet
|
|
90
|
-
const ptr = this.#wasm.alloc(data.byteLength);
|
|
91
|
-
assert(
|
|
92
|
-
ptr > 0,
|
|
93
|
-
`Failed to allocate ${data.byteLength} bytes for received packet`,
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
const memoryView = new Uint8Array(
|
|
97
|
-
this.#memory.buffer,
|
|
98
|
-
ptr,
|
|
99
|
-
data.byteLength,
|
|
100
|
-
);
|
|
101
|
-
memoryView.set(data);
|
|
102
|
-
|
|
103
|
-
// Process the packet
|
|
104
|
-
const result = this.#wasm.receive_packet(ptr, data.byteLength);
|
|
105
|
-
|
|
106
|
-
// Free the allocated memory
|
|
107
|
-
this.#wasm.free(ptr, data.byteLength);
|
|
108
|
-
|
|
109
|
-
// Check result
|
|
110
|
-
if (result !== 0) {
|
|
111
|
-
const errorMessages: Record<number, string> = {
|
|
112
|
-
1: "No active session",
|
|
113
|
-
2: "Buffer too small",
|
|
114
|
-
3: "Unsupported packet version",
|
|
115
|
-
4: "Invalid event count",
|
|
116
|
-
};
|
|
117
|
-
throw new Error(
|
|
118
|
-
errorMessages[result] ?? `Unknown packet error: ${result}`,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Get network state for a specific peer.
|
|
125
|
-
*
|
|
126
|
-
* @param peer - Peer ID to query
|
|
127
|
-
* @returns Object with seq and ack
|
|
128
|
-
*/
|
|
129
|
-
getPeerState(peer: number): {
|
|
130
|
-
seq: number;
|
|
131
|
-
ack: number;
|
|
132
|
-
} {
|
|
133
|
-
return {
|
|
134
|
-
seq: this.#wasm.get_peer_seq(peer),
|
|
135
|
-
ack: this.#wasm.get_peer_ack(peer),
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
}
|