@@ -37,12 +37,17 @@ public async Task Run()
37
37
mdns . AnswerReceived += Mdns_AnswerReceived ;
38
38
await mdns . Start ( ) ;
39
39
40
+ //Query all services (unicast first since we are new to the network)
40
41
var records = await mdns . QueryService ( ALL_SERVICES , DEFAULT_DOMAIN , true ) ;
41
42
await ProcessRecords ( records ) ;
42
43
43
44
await Task . Delay ( 10000 ) ;
44
- records = await mdns . QueryService ( ALL_SERVICES , DEFAULT_DOMAIN , true ) ;
45
+
46
+ //Check for any services we didn't hear about in the last query (known answer suppression is automatic)
47
+ records = await mdns . QueryService ( ALL_SERVICES , DEFAULT_DOMAIN , false ) ;
45
48
await ProcessRecords ( records ) ;
49
+
50
+ //In long running implementations, scanning should occur every 15-60 mins
46
51
}
47
52
48
53
private async Task Mdns_AnswerReceived ( DNSMessageEvent e )
@@ -59,9 +64,10 @@ private async Task ProcessRecords(ResourceRecord[] records)
59
64
string ? serviceName ;
60
65
PtrRecord item = ( PtrRecord ) record ;
61
66
if ( ! item . Domain . EndsWith ( ".local" ) )
62
- return ; //NOT DNS-SD
67
+ return ; //NOT DNS-SD this is regular MDNS
63
68
if ( item . Name . StartsWith ( ALL_SERVICES ) )
64
69
{
70
+ //These records tell us a service exists on the network for 1 or more hosts
65
71
serviceName = MDNS . GetServiceName ( item . DomainLabels ) ;
66
72
var cachedAnswers = await mdns . QueryService ( serviceName ! , DEFAULT_DOMAIN ) ;
67
73
if ( serviceName != null )
@@ -73,12 +79,14 @@ private async Task ProcessRecords(ResourceRecord[] records)
73
79
serviceName = MDNS . GetServiceName ( item . DomainLabels ) ;
74
80
if ( serviceName == ALL_SERVICES )
75
81
continue ;
82
+ //These records are instance pointers - they tell us about hosts which have a specific service
76
83
string ? serviceInstance = MDNS . GetInstanceName ( item . DomainLabels ) ;
77
84
78
85
if ( serviceName != null )
79
86
{
80
87
if ( UpdateService ( serviceName , serviceInstance ) )
81
88
{
89
+ // Request details on this instance of the service
82
90
var lst = await mdns . ResolveServiceInstance ( serviceInstance ! , serviceName ! , DEFAULT_DOMAIN ) ;
83
91
foreach ( var msg in lst )
84
92
await ProcessRecords ( msg . Answers ) ;
@@ -88,6 +96,7 @@ private async Task ProcessRecords(ResourceRecord[] records)
88
96
}
89
97
else if ( record . Type == DNSRecordType . SRV )
90
98
{
99
+ // A detailed record of a host that is running a service on a particular port (with optional text info)
91
100
string ? serviceName = MDNS . GetServiceName ( ( ( SRVRecord ) record ) . Labels ) ;
92
101
string ? serviceInstance = MDNS . GetInstanceName ( ( ( SRVRecord ) record ) . Labels ) ;
93
102
List < ResourceRecord > rcds = new List < ResourceRecord > ( ) ;
@@ -101,6 +110,7 @@ private async Task ProcessRecords(ResourceRecord[] records)
101
110
{
102
111
if ( records . Any ( r => r . Type == DNSRecordType . SRV && record . Name . Equals ( r . Name ) ) )
103
112
continue ;
113
+ //There are a few services that publish text without a SRV record - catch them here. Most implementations won't need this.
104
114
string ? serviceName = MDNS . GetServiceName ( ( ( TxtRecord ) record ) . Labels ) ;
105
115
string ? serviceInstance = MDNS . GetInstanceName ( ( ( TxtRecord ) record ) . Labels ) ;
106
116
if ( serviceInstance != null && serviceName != null )
0 commit comments