Import std.net.lan
std.net.lan
std.net's LAN discovery: LanAnnouncer, which offers a service to the LAN, and Lan.Find, which finds those of a type there, over mDNS and DNS-SD (RFCs 6762 and 6763), as Bonjour and Avahi have them, so the dns-sd and avahi-browse tools find what a program offers, and it finds what they offer. It goes over IPv4, through each of the machine's interfaces but loopback's, or through loopback's when nothing reaches the LAN, so programs on the machine still find each other.
std/net/lan.fb
Types
LanService
struct
Public Struct LanService Implements Printable
A service on the LAN, as DNS-SD has one: its type, like "_mygame._udp", its name, which none of its type shares, the host it's on and that host's addresses, its port, and its text, a map of keys to values, each key once whatever its letters' case, as the first of them is written.
-
Its type, like "_mygame._udp".
-
Its name, which none of its type shares.
-
The host it's on.
-
Its port.
-
Its host's addresses.
-
Its text: keys and their values.
-
Its first address and port, or Null when it has no address: the address its answer came from, which the finder can reach, then those it gave for its host.
-
Its text, as a string interpolation like "{value}" shows it.
Doc from
std.core.interfaces.Printable.ToString.
LanAnnouncer
class
Public Class LanAnnouncer
Offers a service to the LAN: it answers what Lan.Find and DNS-SD's other browsers ask, on the main thread, as Async code does, from Start until Stop, or until it goes, keeping the program running meanwhile, as a listener's AcceptAsync does. It doesn't check that no other service of its type has its name, as a DNS-SD responder's probe would, so names should differ, by the machine's name, say.
-
@PanicsAtCaller Public Static Method Start(type: String, name: String, port: UInt16, text: Map<String, String>? = Null, discoveryPort: UInt16 = MDNS_PORT): LanAnnouncer Throws NetErrorstatic method #Starts offering the service
nameoftype, which is "_" and up to 15 lowercase letters, digits, and '-', then "._udp" or "._tcp", atportof this machine, withtextas its keys and values, each pair 255 bytes at most and 8192 in all, each key once whatever its letters' case. A type,name, ortextthat isn't one panics. It answers at mDNS's port, 5353, or atdiscoveryPort, which Find has to be given too. What it sends through an interface gives that interface's address alone, as RFC 6762 has it. -
Its service, as Find gives it, with this machine's addresses.
-
Whether Stop stopped it.
-
Says goodbye to the LAN, so other mDNS caches drop the service's records, but not its host's addresses, which other services can have, and stops answering.
Lan
struct
Public Struct Lan
Finds the services on the LAN.
-
@PanicsAtCaller Public Static Method Find(type: String, timeout: Duration? = Null, discoveryPort: UInt16 = MDNS_PORT): LanService[] Throws NetErrorstatic method #The services of
type, as LanAnnouncer.Start takes it, that answer withintimeout, a second unless it's given, in the order they answered, each once. It asks again a third of the way through, as answers can be lost. Throws a NetError when it can't ask, as when the machine has no network. A type that isn't one panics. -
@PanicsAtCaller Public Static Async Method FindAsync(type: String, timeout: Duration? = Null, discoveryPort: UInt16 = MDNS_PORT): LanService[] Throws NetErrorstatic method #Find's Async twin, which waits on the main thread.
-
The addresses this machine has on its networks, but loopback's, IPv4's and IPv6's, as a LAN game shows its players where to join.
-
This machine's name, as its OS has it.
Report an issue with this page or with FutureBlitz on GitHub.