Skip to content

Commit

Permalink
Merge pull request #31 from SlimeVR/fix/tracker-emulation/server-search
Browse files Browse the repository at this point in the history
fix(tracker-emulation): server search
  • Loading branch information
TheDevMinerTV authored Jun 7, 2024
2 parents dc34a55 + d0ddac3 commit 26c8dc1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-carrots-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/tracker-emulation': patch
---

fixed that the module automatically searches for a server
5 changes: 5 additions & 0 deletions .changeset/shy-garlics-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/emulated-tracker-demo': patch
---

print full packets instead of just the packet types
5 changes: 5 additions & 0 deletions .changeset/smart-lemons-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/tracker-emulation': minor
---

added ability to disable the automatic server search
5 changes: 5 additions & 0 deletions .changeset/spotty-mangos-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/tracker-emulation': minor
---

added ability to stop searching for a server
6 changes: 4 additions & 2 deletions apps/emulated-tracker-demo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const main = async () => {
tracker.on('error', (err) => console.error(err));

tracker.on('searching-for-server', () => console.log('searching for server...'));
tracker.on('stopped-searching-for-server', () => console.log('stopped searching for server'));

tracker.on('connected-to-server', (ip, port) => console.log('connected to server', ip, port));
tracker.on('disconnected-from-server', (reason) => {
console.log('disconnected from server', reason);
Expand All @@ -32,9 +34,9 @@ const main = async () => {

tracker.on('server-feature-flags', (flags) => console.log('server feature flags', flags.getAllEnabled()));

tracker.on('incoming-packet', (packet) => console.log('unknown packet type', packet.type));
tracker.on('incoming-packet', (packet) => console.log('incoming packet', packet));
tracker.on('unknown-incoming-packet', (buf) => console.log('unknown packet', buf));
tracker.on('outgoing-packet', (packet) => console.log('outgoing packet', packet.type));
tracker.on('outgoing-packet', (packet) => console.log('outgoing packet', packet));

await tracker.init();

Expand Down
21 changes: 19 additions & 2 deletions packages/tracker-emulation/src/EmulatedTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ export class TimeoutError extends Error {
}

type DisconnectReason = TimeoutError | Error;
type SearchStopReason = 'manual' | 'found-server';

interface EmulatedTrackerEvents {
error: (error: Error) => void;
ready: (ip: string, port: number) => void;

'searching-for-server': () => void;
'stopped-searching-for-server': (reason: SearchStopReason) => void;

'connected-to-server': (serverIP: string, serverPort: number) => void;
'disconnected-from-server': (reason: DisconnectReason) => void;

Expand Down Expand Up @@ -90,7 +93,7 @@ export class EmulatedTracker extends (EventEmitter as {
private readonly serverDiscoveryIP = '255.255.255.255',
private readonly serverDiscoveryPort = 6969,
private readonly serverTimeout = 5000,
private readonly autoReconnect = true
private readonly autoSearchForServer = true
) {
super();

Expand All @@ -110,7 +113,7 @@ export class EmulatedTracker extends (EventEmitter as {
this.state = { status: 'disconnected' };
this.emit('disconnected-from-server', reason);

if (this.autoReconnect) {
if (this.autoSearchForServer) {
this.searchForServer();
}
}
Expand Down Expand Up @@ -151,6 +154,10 @@ export class EmulatedTracker extends (EventEmitter as {

const addr = this.socket.address();
this.emit('ready', addr.address, addr.port);

if (this.autoSearchForServer) {
this.searchForServer();
}
}

searchForServer() {
Expand All @@ -161,6 +168,15 @@ export class EmulatedTracker extends (EventEmitter as {
this.emit('searching-for-server');
}

stopSearchingForServer() {
if (this.state.status !== 'searching-for-server') return;

clearInterval(this.state.discoveryInterval);
this.state = { status: 'disconnected' };

this.emit('stopped-searching-for-server', 'manual');
}

private log(msg: string) {
console.log(`[Tracker:${this.mac}] ${msg}`);
}
Expand Down Expand Up @@ -226,6 +242,7 @@ export class EmulatedTracker extends (EventEmitter as {
if (msg.readUint8(0) !== DeviceBoundHandshakePacket.type) return;

clearInterval(this.state.discoveryInterval);
this.emit('stopped-searching-for-server', 'found-server');

this.state = {
status: 'connected-to-server',
Expand Down

0 comments on commit 26c8dc1

Please sign in to comment.