Documentation
¶
Overview ¶
Package discovery is mDNS room browsing: a host announces its open room on the local network, and a client lists what it hears without anybody reading a code out loud.
⚠️ The dependency is confined here, and that is the package's first job ¶
This is the second boundary in the repository written to hold a third-party network library in one place — `internal/socket` holds the WebSocket one — and the rule is the same: nothing outside this package imports `zeroconf`, and nothing inside it leaks a `zeroconf` type through an exported signature. What crosses the boundary is a Room, which is this game's own vocabulary.
Real mDNS rather than a private multicast beacon, chosen deliberately. A beacon on a group of our own would be smaller and would need no dependency at all, and it would also be invisible to `dns-sd -B _hexarena._tcp` and to `avahi-browse` — so a host who cannot see their own room in the list would have no way to tell whether the game or the network was at fault. Speaking the standard means the machine's own tooling is the second opinion.
⚠️ A record is read leniently and written strictly ¶
Everything else in this repository parses its data with DisallowUnknownFields, because a field nobody reads is a field an author thinks is doing something. That rule is exactly inverted here and the reason is who wrote the bytes: a data file is authored by *this* build, and a TXT record arrives from *another machine's* build, which may be a version ahead. A key this build does not know is therefore a fact about the sender rather than a mistake, and refusing the record over it would make every added key a flag day on a LAN.
What is NOT lenient is the code. A record with no room code in it is refused outright: the code is the only thing a client can act on, and an entry a player can see and cannot join is worse than an entry that never appeared.
Index ¶
Constants ¶
const ( Service = "_hexarena._tcp" Domain = "local." )
Service is the DNS-SD service type a room is announced under, and Domain is the only domain mDNS has.
The underscore prefix and the `._tcp` suffix are DNS-SD's, not a convention of ours: they are what makes `dns-sd -B _hexarena._tcp` and `avahi-browse _hexarena._tcp` find a room without either of them being told anything about this game.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advertisement ¶
type Advertisement struct {
// contains filtered or unexported fields
}
Advertisement is a room announced on the local network, and closing it takes the announcement back.
⚠️ **Closing is not optional and is not only tidiness.** zeroconf sends goodbye packets on shutdown — records with a zero TTL — and a browser that never receives them keeps offering a room that is gone until the TTL expires. A player picking that row pastes a code at nothing. So the host closes this before it exits, on every path, the same way it closes its listener.
func Advertise ¶
func Advertise(held Room, at netip.AddrPort) (*Advertisement, error)
Advertise publishes one room under the DNS-SD service type.
⚠️ **The room code is the INSTANCE NAME**, which is two decisions at once. DNS-SD requires an instance name to be unique on the network, and a room code already is by construction: it encodes the host's address, the port and the room's index, so two rooms cannot share one unless they are the same room. And a browser that receives the PTR and SRV records but loses the TXT still has the one field a client can act on. Naming the instance after the player, or after the machine, would have needed a uniqueness rule of its own and would have put the code somewhere it could go missing.
⚠️ **It registers as a PROXY for one address rather than letting the library advertise the machine**, and both halves of that are deliberate.
The address: zeroconf's own Register publishes *every* address on *every* interface, which is precisely the ambiguity cmd/hexarena-host's `pick` refuses to guess about — beside a real LAN address, a container bridge is up, is IPv4 and is unreachable from the other player's laptop. The host has already made that decision once, and the answer is in the room code. Publishing the same address the code carries means the SRV record and the code cannot disagree.
The host name: `os.Hostname()` on macOS answers `vukynMac.local`, and zeroconf appends the domain to whatever it is given — measured, the SRV target came out `vukynMac.local.local.`, which resolves nowhere. `dns-sd -L` shows it and our own Browse never reads it, so the game worked and every other DNS-SD tool on the network was handed a name it could not follow. hostLabel is the trim.
func (*Advertisement) Close ¶
func (a *Advertisement) Close()
Close withdraws the announcement. It is safe on a nil receiver, so a caller that never managed to advertise can defer it unconditionally.
func (*Advertisement) Closed ¶
func (a *Advertisement) Closed() bool
Closed reports whether the withdrawal has happened. A nil advertisement — a room that never announced — reads as closed, because there is nothing left to take back.
type Room ¶
type Room struct {
// Code is the twelve characters a player would otherwise have been read out.
// It already carries the address, the port and the room index, so a listing
// needs nothing else to act.
Code wire.RoomCode
// Format, Battles, Draft and Watch are what the host's own banner prints, so
// a player can tell a drafting bo3 from a squad bo1 before joining one.
Format wire.Format
Battles int
Draft bool
Watch bool
// Data is the host's short data digest and Protocol its protocol number.
// Both are what the join screen already compares, and both are here so a
// listing can say "this one will refuse you" before the dial rather than
// after it.
Data string
Protocol int
// At is what mDNS itself reported, and it is set by a browse and ignored by
// an advertisement.
//
// ⚠️ It is NOT what a client dials — the code is — and the two can honestly
// differ: a host picks one address for the code (see cmd/hexarena-host's
// `pick`, which refuses to guess between a LAN address and a container
// bridge) while mDNS answers with every address the machine has. It is kept
// so a listing can show a room whose code names an address the browser can
// see is not the one the packet came from, which is the shape of every
// "the code does not work" report this design can produce.
At netip.AddrPort
}
Room is one announcement: what a host publishes, and what a browse hands back.
⚠️ **The code is the whole of what a client acts on.** Everything else on this struct is for the listing to draw — a player picks a row and the join that follows is the ordinary one, pasting the ordinary code, over the ordinary path. Browsing adds a way to *find* a room and no way to *enter* one, which is what keeps this package out of the join's way entirely.
func Browse ¶
Browse listens for rooms until the context is done and hands back what it heard, one entry per room code, in code order.
⚠️ **It listens for a fixed stretch rather than returning on the first answer**, and the caller's context is what sets that stretch. mDNS is a question shouted at a network: an answer may arrive in a millisecond or in half a second, and there is no moment at which "everybody has replied" is a fact. So a browse is a *window*, and a listing drawn from it is what was heard in that window rather than what exists.
A record that cannot be read is skipped rather than failing the browse. One misbehaving announcer on the LAN — or one from a build that words a code differently — must not empty a list that has three good rooms in it.