@matter/general 0.16.8-alpha.0-20260125-38e62bc3e → 0.16.8-alpha.0-20260127-65e1b40e2
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/cjs/net/ServerAddress.d.ts.map +1 -1
- package/dist/cjs/net/ServerAddress.js +2 -1
- package/dist/cjs/net/ServerAddress.js.map +1 -1
- package/dist/cjs/net/dns-sd/DnssdName.d.ts +56 -0
- package/dist/cjs/net/dns-sd/DnssdName.d.ts.map +1 -0
- package/dist/cjs/net/dns-sd/DnssdName.js +193 -0
- package/dist/cjs/net/dns-sd/DnssdName.js.map +6 -0
- package/dist/cjs/net/dns-sd/DnssdNames.d.ts +77 -0
- package/dist/cjs/net/dns-sd/DnssdNames.d.ts.map +1 -0
- package/dist/cjs/net/dns-sd/DnssdNames.js +238 -0
- package/dist/cjs/net/dns-sd/DnssdNames.js.map +6 -0
- package/dist/cjs/net/dns-sd/DnssdSolicitor.d.ts +80 -0
- package/dist/cjs/net/dns-sd/DnssdSolicitor.d.ts.map +1 -0
- package/dist/cjs/net/dns-sd/DnssdSolicitor.js +212 -0
- package/dist/cjs/net/dns-sd/DnssdSolicitor.js.map +6 -0
- package/dist/cjs/net/dns-sd/IpService.d.ts +73 -0
- package/dist/cjs/net/dns-sd/IpService.d.ts.map +1 -0
- package/dist/cjs/net/dns-sd/IpService.js +329 -0
- package/dist/cjs/net/dns-sd/IpService.js.map +6 -0
- package/dist/cjs/net/dns-sd/IpServiceResolution.d.ts +16 -0
- package/dist/cjs/net/dns-sd/IpServiceResolution.d.ts.map +1 -0
- package/dist/cjs/net/dns-sd/IpServiceResolution.js +162 -0
- package/dist/cjs/net/dns-sd/IpServiceResolution.js.map +6 -0
- package/dist/cjs/net/dns-sd/IpServiceStatus.d.ts +58 -0
- package/dist/cjs/net/dns-sd/IpServiceStatus.d.ts.map +1 -0
- package/dist/cjs/net/dns-sd/IpServiceStatus.js +191 -0
- package/dist/cjs/net/dns-sd/IpServiceStatus.js.map +6 -0
- package/dist/cjs/net/dns-sd/index.d.ts +6 -0
- package/dist/cjs/net/dns-sd/index.d.ts.map +1 -1
- package/dist/cjs/net/dns-sd/index.js +6 -0
- package/dist/cjs/net/dns-sd/index.js.map +1 -1
- package/dist/cjs/net/udp/UdpInterface.js +1 -1
- package/dist/cjs/net/udp/UdpInterface.js.map +1 -1
- package/dist/esm/net/ServerAddress.d.ts.map +1 -1
- package/dist/esm/net/ServerAddress.js +2 -1
- package/dist/esm/net/ServerAddress.js.map +1 -1
- package/dist/esm/net/dns-sd/DnssdName.d.ts +56 -0
- package/dist/esm/net/dns-sd/DnssdName.d.ts.map +1 -0
- package/dist/esm/net/dns-sd/DnssdName.js +173 -0
- package/dist/esm/net/dns-sd/DnssdName.js.map +6 -0
- package/dist/esm/net/dns-sd/DnssdNames.d.ts +77 -0
- package/dist/esm/net/dns-sd/DnssdNames.d.ts.map +1 -0
- package/dist/esm/net/dns-sd/DnssdNames.js +218 -0
- package/dist/esm/net/dns-sd/DnssdNames.js.map +6 -0
- package/dist/esm/net/dns-sd/DnssdSolicitor.d.ts +80 -0
- package/dist/esm/net/dns-sd/DnssdSolicitor.d.ts.map +1 -0
- package/dist/esm/net/dns-sd/DnssdSolicitor.js +192 -0
- package/dist/esm/net/dns-sd/DnssdSolicitor.js.map +6 -0
- package/dist/esm/net/dns-sd/IpService.d.ts +73 -0
- package/dist/esm/net/dns-sd/IpService.d.ts.map +1 -0
- package/dist/esm/net/dns-sd/IpService.js +309 -0
- package/dist/esm/net/dns-sd/IpService.js.map +6 -0
- package/dist/esm/net/dns-sd/IpServiceResolution.d.ts +16 -0
- package/dist/esm/net/dns-sd/IpServiceResolution.d.ts.map +1 -0
- package/dist/esm/net/dns-sd/IpServiceResolution.js +142 -0
- package/dist/esm/net/dns-sd/IpServiceResolution.js.map +6 -0
- package/dist/esm/net/dns-sd/IpServiceStatus.d.ts +58 -0
- package/dist/esm/net/dns-sd/IpServiceStatus.d.ts.map +1 -0
- package/dist/esm/net/dns-sd/IpServiceStatus.js +171 -0
- package/dist/esm/net/dns-sd/IpServiceStatus.js.map +6 -0
- package/dist/esm/net/dns-sd/index.d.ts +6 -0
- package/dist/esm/net/dns-sd/index.d.ts.map +1 -1
- package/dist/esm/net/dns-sd/index.js +6 -0
- package/dist/esm/net/dns-sd/index.js.map +1 -1
- package/dist/esm/net/udp/UdpInterface.js +1 -1
- package/dist/esm/net/udp/UdpInterface.js.map +1 -1
- package/package.json +2 -2
- package/src/net/ServerAddress.ts +2 -1
- package/src/net/dns-sd/DnssdName.ts +252 -0
- package/src/net/dns-sd/DnssdNames.ts +208 -0
- package/src/net/dns-sd/DnssdSolicitor.ts +231 -0
- package/src/net/dns-sd/IpService.ts +346 -0
- package/src/net/dns-sd/IpServiceResolution.ts +134 -0
- package/src/net/dns-sd/IpServiceStatus.ts +212 -0
- package/src/net/dns-sd/index.ts +6 -0
- package/src/net/udp/UdpInterface.ts +1 -1
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022-2026 Matter.js Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { DnsRecord } from "#codec/DnsCodec.js";
|
|
8
|
+
import { Duration } from "#time/Duration.js";
|
|
9
|
+
import { Time } from "#time/Time.js";
|
|
10
|
+
import { Timestamp } from "#time/Timestamp.js";
|
|
11
|
+
import { Seconds } from "#time/TimeUnit.js";
|
|
12
|
+
import { Entropy } from "#util/Entropy.js";
|
|
13
|
+
import { Lifetime } from "#util/Lifetime.js";
|
|
14
|
+
import { Observable, ObserverGroup } from "#util/Observable.js";
|
|
15
|
+
import { Scheduler } from "#util/Scheduler.js";
|
|
16
|
+
import { DnssdName } from "./DnssdName.js";
|
|
17
|
+
import { QueryMulticaster } from "./DnssdSolicitor.js";
|
|
18
|
+
import { MdnsSocket } from "./MdnsSocket.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Names collected via DNS-SD.
|
|
22
|
+
*
|
|
23
|
+
* TODO - API is designed to support Avahi, Bonjour etc. but current implementation is tied to local MDNS
|
|
24
|
+
*/
|
|
25
|
+
export class DnssdNames {
|
|
26
|
+
readonly #socket: MdnsSocket;
|
|
27
|
+
readonly #lifetime: Lifetime;
|
|
28
|
+
readonly #entropy: Entropy;
|
|
29
|
+
readonly #filter?: (record: DnsRecord) => boolean;
|
|
30
|
+
readonly #solicitor: QueryMulticaster;
|
|
31
|
+
readonly #observers = new ObserverGroup();
|
|
32
|
+
readonly #names = new Map<string, DnssdName>();
|
|
33
|
+
readonly #expiration: Scheduler<DnssdName.Record>;
|
|
34
|
+
readonly #discovered = new Observable<[name: DnssdName]>();
|
|
35
|
+
readonly #goodbyeProtectionWindow: Duration;
|
|
36
|
+
readonly #minTtl: Duration;
|
|
37
|
+
|
|
38
|
+
constructor({
|
|
39
|
+
socket,
|
|
40
|
+
lifetime = Lifetime.process,
|
|
41
|
+
entropy,
|
|
42
|
+
filter,
|
|
43
|
+
goodbyeProtectionWindow,
|
|
44
|
+
minTtl: minTtl,
|
|
45
|
+
}: DnssdNames.Context) {
|
|
46
|
+
this.#socket = socket;
|
|
47
|
+
this.#lifetime = lifetime.join("mdns client");
|
|
48
|
+
this.#entropy = entropy;
|
|
49
|
+
this.#filter = filter;
|
|
50
|
+
this.#solicitor = new QueryMulticaster(this);
|
|
51
|
+
this.#goodbyeProtectionWindow = goodbyeProtectionWindow ?? DnssdNames.defaults.goodbyeProtectionWindow;
|
|
52
|
+
this.#minTtl = minTtl ?? DnssdNames.defaults.minTtl;
|
|
53
|
+
this.#observers.on(this.#socket.receipt, this.#handleMessage.bind(this));
|
|
54
|
+
|
|
55
|
+
this.#expiration = new Scheduler({
|
|
56
|
+
name: "expiration scheduler",
|
|
57
|
+
lifetime: this.#lifetime,
|
|
58
|
+
timeOf: a => {
|
|
59
|
+
return a.expiresAt;
|
|
60
|
+
},
|
|
61
|
+
run: record => {
|
|
62
|
+
const discoveryName = this.#names.get(record.name);
|
|
63
|
+
if (discoveryName) {
|
|
64
|
+
discoveryName.deleteRecord(record);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#handleMessage(message: MdnsSocket.Message) {
|
|
71
|
+
let goodbyesBefore: undefined | Timestamp;
|
|
72
|
+
for (let record of [...message.answers, ...message.additionalRecords]) {
|
|
73
|
+
if (this.#filter && !this.#filter(record)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const name = this.get(record.name);
|
|
78
|
+
if (record.ttl) {
|
|
79
|
+
if (record.ttl < this.#minTtl) {
|
|
80
|
+
record = { ...record, ttl: this.#minTtl };
|
|
81
|
+
}
|
|
82
|
+
const wasDiscovered = name.isDiscovered;
|
|
83
|
+
name.installRecord(record);
|
|
84
|
+
if (!wasDiscovered && name.isDiscovered) {
|
|
85
|
+
this.#discovered.emit(name);
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
if (goodbyesBefore === undefined) {
|
|
89
|
+
goodbyesBefore = Timestamp(Time.nowMs - this.#goodbyeProtectionWindow);
|
|
90
|
+
}
|
|
91
|
+
name.deleteRecord(record, goodbyesBefore);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Test for existence of name.
|
|
98
|
+
*/
|
|
99
|
+
has(name: string) {
|
|
100
|
+
name = name.toLowerCase();
|
|
101
|
+
return this.#names.has(name);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Retrieve the {@link DnssdName} for {@link name}.
|
|
106
|
+
*
|
|
107
|
+
* This will create the name if it does not exist, and if you do not add an observer then it will not automatically
|
|
108
|
+
* delete if there are no records. So if you may not use the record test for existence with {@link has} first.
|
|
109
|
+
*/
|
|
110
|
+
get(qname: string): DnssdName {
|
|
111
|
+
let name = this.maybeGet(qname);
|
|
112
|
+
if (name === undefined) {
|
|
113
|
+
name = new DnssdName(qname, this.#nameContext);
|
|
114
|
+
this.#names.set(qname, name);
|
|
115
|
+
}
|
|
116
|
+
return name;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Retrieve the {@link DnssdName} if known.
|
|
121
|
+
*/
|
|
122
|
+
maybeGet(name: string) {
|
|
123
|
+
name = name.toLowerCase();
|
|
124
|
+
return this.#names.get(name);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Wait for all workers and close all names.
|
|
129
|
+
*/
|
|
130
|
+
async close() {
|
|
131
|
+
using _closing = this.#lifetime.closing();
|
|
132
|
+
this.#observers.close();
|
|
133
|
+
await this.#expiration.close();
|
|
134
|
+
for (const name of this.#names.values()) {
|
|
135
|
+
await name.close();
|
|
136
|
+
this.#names.delete(name.qname);
|
|
137
|
+
}
|
|
138
|
+
await this.#solicitor.close();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get socket() {
|
|
142
|
+
return this.#socket;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Emits when a {@link DnssdName} is first discovered.
|
|
147
|
+
*/
|
|
148
|
+
get discovered() {
|
|
149
|
+
return this.#discovered;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Shared solicitor.
|
|
154
|
+
*
|
|
155
|
+
* We offer solicitation in this object so there is not redundant solicitation across interested parties.
|
|
156
|
+
*/
|
|
157
|
+
get solicitor() {
|
|
158
|
+
return this.#solicitor;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
get entropy() {
|
|
162
|
+
return this.#entropy;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#nameContext: DnssdName.Context = {
|
|
166
|
+
delete: name => {
|
|
167
|
+
const known = this.#names.get(name.qname);
|
|
168
|
+
if (known === name) {
|
|
169
|
+
this.#names.delete(name.qname);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
registerForExpiration: record => {
|
|
174
|
+
this.#expiration.add(record);
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
unregisterForExpiration: record => {
|
|
178
|
+
this.#expiration.delete(record);
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export namespace DnssdNames {
|
|
184
|
+
export interface Context {
|
|
185
|
+
socket: MdnsSocket;
|
|
186
|
+
lifetime?: Lifetime.Owner;
|
|
187
|
+
entropy: Entropy;
|
|
188
|
+
filter?: (record: DnsRecord) => boolean;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The interval after discovering a record for which we ignore goodbyes.
|
|
192
|
+
*
|
|
193
|
+
* This serves as protection for out-of-order messages when a device expires then broadcasts the same record
|
|
194
|
+
* in a very short amount of time.
|
|
195
|
+
*/
|
|
196
|
+
goodbyeProtectionWindow?: Duration;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Minimum TTL for PTR records.
|
|
200
|
+
*/
|
|
201
|
+
minTtl?: Duration;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export const defaults = {
|
|
205
|
+
goodbyeProtectionWindow: Seconds(1),
|
|
206
|
+
minTtl: Seconds(15), // This is the value that Apple uses
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022-2026 Matter.js Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { DnsMessageType, DnsQuery, DnsRecord, DnsRecordClass, DnsRecordType } from "#codec/DnsCodec.js";
|
|
8
|
+
import { Logger } from "#log/Logger.js";
|
|
9
|
+
import { RetrySchedule } from "#net/RetrySchedule.js";
|
|
10
|
+
import { Time } from "#time/Time.js";
|
|
11
|
+
import { Hours, Millis, Seconds } from "#time/TimeUnit.js";
|
|
12
|
+
import { Abort } from "#util/Abort.js";
|
|
13
|
+
import { BasicMultiplex } from "#util/Multiplex.js";
|
|
14
|
+
import { ObservableValue } from "#util/Observable.js";
|
|
15
|
+
import type { DnssdName } from "./DnssdName.js";
|
|
16
|
+
import type { DnssdNames } from "./DnssdNames.js";
|
|
17
|
+
|
|
18
|
+
const logger = new Logger("DiscoverySolicitor");
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Solicits DNS-SD records for specific names.
|
|
22
|
+
*/
|
|
23
|
+
export interface DnssdSolicitor {
|
|
24
|
+
/**
|
|
25
|
+
* Send a single MDNS query for a specific DNS-SD name.
|
|
26
|
+
*
|
|
27
|
+
* Multiple solicitations for the same name are coalesced into the same query using a macrotask.
|
|
28
|
+
*/
|
|
29
|
+
solicit(solicitation: DnssdSolicitor.Solicitation): void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Send MDNS queries for a specific DNS-SD name using a standard MDNS transmission schedule.
|
|
33
|
+
*
|
|
34
|
+
* The solicitor does not have a notion of "discovery complete", so this function does not return until
|
|
35
|
+
* {@link DnssdSolicitor.Discovery.abort} signals abort (or the solicitor is closed).
|
|
36
|
+
*
|
|
37
|
+
* Multiple simultaneous attempts to complete discovery of the same name will not result in redundant solicitations.
|
|
38
|
+
*
|
|
39
|
+
* If fields in {@link discovery} change their value is used for the next solicitation.
|
|
40
|
+
*/
|
|
41
|
+
discover(discovery: DnssdSolicitor.Discovery): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Solicit one or more record types for a name.
|
|
46
|
+
*
|
|
47
|
+
* "Soliciting" consists of broadcasting a query for a DNS-SD name. Groups multiple solicitations in the same
|
|
48
|
+
* macrotask into a single packet.
|
|
49
|
+
*/
|
|
50
|
+
export namespace DnssdSolicitor {
|
|
51
|
+
/**
|
|
52
|
+
* Configures solicitation of a single name.
|
|
53
|
+
*/
|
|
54
|
+
export interface Solicitation {
|
|
55
|
+
/**
|
|
56
|
+
* The name to solicit.
|
|
57
|
+
*/
|
|
58
|
+
name: DnssdName;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Record types to request.
|
|
62
|
+
*/
|
|
63
|
+
recordTypes: DnsRecordType[];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Additional names to include as known answers.
|
|
67
|
+
*/
|
|
68
|
+
associatedNames?: Iterable<DnssdName>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Configures repeated solicitation.
|
|
73
|
+
*/
|
|
74
|
+
export interface Discovery extends Solicitation {
|
|
75
|
+
/**
|
|
76
|
+
* Terminates discovery.
|
|
77
|
+
*/
|
|
78
|
+
abort: AbortSignal;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Default retry schedule per RFC 6762 (initial delay of 20-120ms. handled separately).
|
|
83
|
+
*/
|
|
84
|
+
export const DefaultRetries: RetrySchedule.Configuration = {
|
|
85
|
+
initialInterval: Seconds(1),
|
|
86
|
+
jitterFactor: 0.2,
|
|
87
|
+
backoffFactor: 2,
|
|
88
|
+
maximumInterval: Hours(1),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Concrete implementation of {@link DnssdSolicitor} that sends DNS-SD queries via multicast.
|
|
94
|
+
*/
|
|
95
|
+
export class QueryMulticaster implements DnssdSolicitor {
|
|
96
|
+
#names: DnssdNames;
|
|
97
|
+
#schedule: RetrySchedule;
|
|
98
|
+
#abort = new Abort();
|
|
99
|
+
#toSolicit = new Map<DnssdName, DnssdSolicitor.Solicitation>();
|
|
100
|
+
#discovering = new Map<DnssdName, { abort: Abort; finished: Promise<void>; waiting: Set<{}> }>();
|
|
101
|
+
#namesReady = new ObservableValue();
|
|
102
|
+
#workers = new BasicMultiplex();
|
|
103
|
+
|
|
104
|
+
constructor(names: DnssdNames, retries?: RetrySchedule.Configuration) {
|
|
105
|
+
this.#names = names;
|
|
106
|
+
this.#schedule = new RetrySchedule(
|
|
107
|
+
names.entropy,
|
|
108
|
+
RetrySchedule.Configuration(DnssdSolicitor.DefaultRetries, retries),
|
|
109
|
+
);
|
|
110
|
+
this.#workers.add(this.#emitSolicitations());
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
solicit(solicitation: DnssdSolicitor.Solicitation) {
|
|
114
|
+
if (this.#abort.aborted) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const entry = this.#toSolicit.get(solicitation.name);
|
|
118
|
+
if (entry === undefined) {
|
|
119
|
+
this.#toSolicit.set(solicitation.name, { ...solicitation });
|
|
120
|
+
} else {
|
|
121
|
+
entry.recordTypes = [...new Set([...entry.recordTypes, ...solicitation.recordTypes])];
|
|
122
|
+
if (solicitation.associatedNames) {
|
|
123
|
+
if (!entry.associatedNames) {
|
|
124
|
+
entry.associatedNames = solicitation.associatedNames;
|
|
125
|
+
} else {
|
|
126
|
+
entry.associatedNames = [...new Set([...entry.associatedNames, ...solicitation.associatedNames])];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
this.#namesReady.emit(true);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async discover(discovery: DnssdSolicitor.Discovery) {
|
|
134
|
+
let active = this.#discovering.get(discovery.name);
|
|
135
|
+
if (active) {
|
|
136
|
+
active.waiting.add(discovery);
|
|
137
|
+
} else {
|
|
138
|
+
// This abort is different from the input abort because we only abort when the input aborts if nobody else
|
|
139
|
+
// is waiting on discovery of the same name
|
|
140
|
+
const abort = new Abort({ abort: this.#abort });
|
|
141
|
+
active = {
|
|
142
|
+
abort,
|
|
143
|
+
finished: this.#discover(discovery, abort),
|
|
144
|
+
waiting: new Set([discovery]),
|
|
145
|
+
};
|
|
146
|
+
this.#discovering.set(discovery.name, active);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
await Abort.race(discovery.abort, active.finished);
|
|
151
|
+
} finally {
|
|
152
|
+
active.waiting.delete(discovery);
|
|
153
|
+
if (active.waiting.size === 0) {
|
|
154
|
+
active.abort();
|
|
155
|
+
this.#discovering.delete(discovery.name);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async #discover(solicitation: DnssdSolicitor.Solicitation, abort: Abort) {
|
|
161
|
+
// Wait initially 20 - 120 ms per RFC 6762
|
|
162
|
+
let timeout = Millis.floor(Millis(20 + 100 * (this.#names.entropy.randomUint32 / Math.pow(2, 32))));
|
|
163
|
+
|
|
164
|
+
for (const nextTimeout of this.#schedule) {
|
|
165
|
+
using delay = new Abort({ abort, timeout });
|
|
166
|
+
|
|
167
|
+
await delay;
|
|
168
|
+
if (abort.aborted) {
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
timeout = nextTimeout;
|
|
173
|
+
|
|
174
|
+
this.solicit(solicitation);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async close() {
|
|
179
|
+
this.#abort();
|
|
180
|
+
await this.#workers;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async #emitSolicitations() {
|
|
184
|
+
while (true) {
|
|
185
|
+
// Wait for names to solicit
|
|
186
|
+
await this.#abort.race(this.#namesReady);
|
|
187
|
+
if (this.#abort.aborted) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Delay using a macrotask so we coalesce names
|
|
192
|
+
await this.#abort.race(Time.sleep("discovery solicitor delay", 0));
|
|
193
|
+
if (this.#abort.aborted) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Gather names we will solicit in this iteration
|
|
198
|
+
const entries = [...this.#toSolicit.values()];
|
|
199
|
+
this.#namesReady.value = false;
|
|
200
|
+
this.#toSolicit.clear();
|
|
201
|
+
|
|
202
|
+
// Create sets for queries and known answers
|
|
203
|
+
const queries = Array<DnsQuery>();
|
|
204
|
+
const answers = Array<DnsRecord>();
|
|
205
|
+
|
|
206
|
+
for (const {
|
|
207
|
+
name: { qname: name, records },
|
|
208
|
+
recordTypes,
|
|
209
|
+
} of entries) {
|
|
210
|
+
for (const recordType of recordTypes) {
|
|
211
|
+
queries.push({ name, recordClass: DnsRecordClass.IN, recordType });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
answers.push(...records);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Send the message
|
|
218
|
+
try {
|
|
219
|
+
await this.#abort.race(
|
|
220
|
+
this.#names.socket.send({
|
|
221
|
+
messageType: DnsMessageType.Query,
|
|
222
|
+
queries,
|
|
223
|
+
answers,
|
|
224
|
+
}),
|
|
225
|
+
);
|
|
226
|
+
} catch (e) {
|
|
227
|
+
logger.error("Unhandled error soliciting DNS-SD names:", e);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|