Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ handle resend errors #62

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/sender.ts
lukas-runge marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Socket, createSocket } from 'dgram';
import { EventEmitter } from 'events';
import { multicastGroup } from './util';
import { Packet, Options } from './packet';

Expand Down Expand Up @@ -47,9 +48,21 @@ export namespace Sender {
*/
useUnicastDestination?: string;
}

export interface EventMap {
changedResendStatus: boolean;
error: Error;
}
}

export class Sender {
export declare interface Sender {
on<K extends keyof Sender.EventMap>(
type: K,
listener: (event: Sender.EventMap[K]) => void,
): this;
}

export class Sender extends EventEmitter {
private socket: Socket;

private readonly port: Sender.Props['port'];
Expand All @@ -66,6 +79,8 @@ export class Sender {

private sequence = 0;

public resendStatus = false;

#loopId: NodeJS.Timeout | undefined;

/**
Expand All @@ -84,6 +99,7 @@ export class Sender {
iface,
useUnicastDestination,
}: Sender.Props) {
super();
this.port = port;
this.universe = universe;
this.#destinationIp = useUnicastDestination || multicastGroup(universe);
Expand Down Expand Up @@ -123,7 +139,23 @@ export class Sender {
}

private reSend() {
if (this.#latestPacketOptions) this.send(this.#latestPacketOptions);
if (this.#latestPacketOptions) {
this.send(this.#latestPacketOptions)
.then(() => {
this.updateResendStatus(true);
})
.catch((err) => {
this.updateResendStatus(false);
this.emit('error', err);
});
}
}

private updateResendStatus(success: boolean) {
if (success !== this.resendStatus) {
this.resendStatus = success;
this.emit('changedResendStatus', success);
lukas-runge marked this conversation as resolved.
Show resolved Hide resolved
}
}

public close(): this {
Expand Down
5 changes: 5 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ describe('Receiver & Sender (integration test)', () => {
try {
const received: Packet[] = [];
const errors: Error[] = [];
const receivedEvents: boolean[] = [];
Tx.on('changedResendStatus', (event) => receivedEvents.push(event));
Tx.on('error', (ex) => errors.push(ex));
Rx.on('packet', (packet) => received.push(packet));
collectErrors(Rx, errors);

Expand All @@ -58,6 +61,8 @@ describe('Receiver & Sender (integration test)', () => {

assert.strictEqual(errors.length, 0);
assert.strictEqual(received.length, 4); // send at 0s, 1s, 2s, 3s. Then at 3.5s we stop
assert.strictEqual(receivedEvents.length, 1);
assert.deepStrictEqual(receivedEvents[0], true);
assert.deepStrictEqual(received[0]!.payload, { 1: 100 });
assert.deepStrictEqual(received[1]!.payload, { 1: 100 });
assert.deepStrictEqual(received[2]!.payload, { 1: 100 });
Expand Down
Loading