discovery

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 23 Imported by: 1

Documentation

Index

Constants

View Source
const (
	IDRequestPacket uint16 = iota
	IDResponsePacket
	IDMessagePacket
)
View Source
const DefaultPort = 7551

DefaultPort is the port used for LAN discovery in Minecraft: Bedrock Edition. Servers should listen on this port to receive RequestPacket broadcasted from clients.

Variables

This section is empty.

Functions

func Marshal

func Marshal(pk Packet, senderID uint64) []byte

Marshal encodes the packet into the bytes with using the sender ID.

Types

type Header struct {
	// PacketID is the ID of the packet.
	PacketID uint16
	// SenderID is the ID of the network that sent the packet.
	SenderID uint64
}

Header describes the binary structure of the initial bytes of a packet transmitted during LAN discovery.

func (*Header) Read

func (h *Header) Read(r io.Reader) error

Read reads and decodes the Header from the io.Reader.

func (*Header) Write

func (h *Header) Write(w io.Writer)

Write writes the binary structure of Header into the io.Writer.

type ListenConfig

type ListenConfig struct {
	// NetworkID specifies the network ID for the NetherNet network being announced to the clients in the
	// same network. In NetherNet, it is explicitly defined as a string, but in LAN discovery it is sent as an uint8.
	NetworkID uint64

	// BroadcastAddress specifies the UDP broadcast address for sending request packets to the servers in
	// the same network. If nil, an *net.UDPAddr with net.IPv4bcast and port 7551 will be used.
	BroadcastAddress *net.UDPAddr

	// Log is used for logging messages at various levels.
	Log *slog.Logger
}

func (ListenConfig) Listen

func (conf ListenConfig) Listen(addr string) (*Listener, error)

Listen starts listening on the specified address. For servers, it should be typically composed for using port 7551 to respond from request packets broadcasted from clients. For clients, it can be an empty string or any address with any port, as it doesn't need to response with other clients; when you expect that port 7551 is already in use by the game itself, you might want to specify the address in this way.

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener represents a listener for LAN discovery. It uses UDP as the underlying protocol and allows both discovering servers on the network using broadcast or announcing servers to the clients in the network.

func (*Listener) Close

func (l *Listener) Close() (err error)

Close closes the Listener. Any notifiers registered to the Listener will be stopped.

func (*Listener) Context

func (l *Listener) Context() context.Context

Context returns a context that is canceled when the Listener is closed.

func (*Listener) Credentials

func (l *Listener) Credentials(context.Context) (*nethernet.Credentials, error)

Credentials returns a nil *nethernet.Credentials with a nil error if the Listener is not closed.

func (*Listener) NetworkID

func (l *Listener) NetworkID() string

NetworkID returns the numerical NetherNet network ID for the Listener in string form.

func (*Listener) Notify

func (l *Listener) Notify(signals chan<- *nethernet.Signal) (stop func())

Notify registers a channel to receive incoming NetherNet signals.

The returned stop function unregisters the channel and closes it. Callers must not close the channel themselves.

func (*Listener) PongData

func (l *Listener) PongData(b []byte)

PongData sets the application data contained in ResponsePacket in response to clients broadcasting RequestPacket. It currently decodes the data as a pong data from Minecraft listeners.

func (*Listener) Responses

func (l *Listener) Responses() map[uint64][]byte

Responses returns the responses sent from servers in the same network during LAN discovery using RequestPacket with the broadcast address specified in [ListenConfig.BroadcastAddress].

func (*Listener) ServerData

func (l *Listener) ServerData(d *ServerData)

ServerData stores the ServerData for responding to the clients broadcasting RequestPacket with a ResponsePacket containing the binary representation.

func (*Listener) Signal

func (l *Listener) Signal(ctx context.Context, signal *nethernet.Signal) error

Signal sends a NetherNet signal to the corresponding address for the network ID.

type MessagePacket

type MessagePacket struct {
	// RecipientID is the network ID to be signaled by MessagePacket. Note that this is
	// not the connection ID. The connection ID is included in the Data field and used only
	// during a single negotiation, whereas the network ID may be used across multiple connections.
	RecipientID uint64
	// Data is the actual data for signaling. It contains the string form of nethernet.Signal.
	Data string
}

MessagePacket is sent by both server and client to negotiate a NetherNet connection.

func (*MessagePacket) ID

func (*MessagePacket) ID() uint16

ID ...

func (*MessagePacket) Read

func (pk *MessagePacket) Read(r io.Reader) error

Read ...

func (*MessagePacket) Write

func (pk *MessagePacket) Write(w io.Writer)

Write ...

type Packet

type Packet interface {
	// ID uniquely returns the ID of the packet. It is one of the first byte
	// in the Header and used to identify which packet to decode in [Unmarshal].
	ID() uint16

	// Read reads/decodes the packet data from the [io.Reader].
	Read(r io.Reader) error
	// Write writes the packet data into the [io.Writer].
	Write(w io.Writer)
}

Packet describes a packet used in LAN discovery.

func Unmarshal

func Unmarshal(b []byte) (Packet, uint64, error)

Unmarshal decodes the packet from the bytes and returns them with an ID of the sender which has sent the packet. An error may be returned during decoding the content.

type RequestPacket

type RequestPacket struct{}

RequestPacket is sent by clients to discover servers on the same network using the broadcast address on port 7551. Servers listen on the same port and respond with a ResponsePacket containing basic information about their world.

func (*RequestPacket) ID

func (*RequestPacket) ID() uint16

ID ...

func (*RequestPacket) Read

func (*RequestPacket) Read(io.Reader) error

Read ...

func (*RequestPacket) Write

func (*RequestPacket) Write(io.Writer)

Write ...

type ResponsePacket

type ResponsePacket struct {
	// ApplicationData contains application-specific data for the packet. In Minecraft: Bedrock Edition, it is
	// typically structured as ServerData.
	ApplicationData []byte
}

ResponsePacket is a packet sent by servers in response to a RequestPacket from clients to advertise the world.

func (*ResponsePacket) ID

func (*ResponsePacket) ID() uint16

ID ...

func (*ResponsePacket) Read

func (pk *ResponsePacket) Read(r io.Reader) error

Read ...

func (*ResponsePacket) Write

func (pk *ResponsePacket) Write(w io.Writer)

Write ...

type ServerData

type ServerData struct {
	// ServerName is the name of the server. It is typically the player name of the owner
	// hosting the server and is displayed below the LevelName in the world card.
	ServerName string
	// LevelName identifies the name of the world and appears at the top of ServerName in the world card.
	LevelName string
	// GameType is the default game mode of the world. Players receive this game mode when they
	// join. It remains unchanged during gameplay and may be updated the next time the world is hosted.
	GameType uint8
	// PlayerCount is the amount of players currently connected to the world. Worlds
	// with a player count of 0 or less are not displayed by clients, so it should at
	// least 1 even if the server reports 0 to prevent world cards not appearing for the server.
	PlayerCount int32
	// MaxPlayerCount is the maximum amount of players allowed to join the world.
	MaxPlayerCount int32
	// EditorWorld is a value dictates if the world was created as a project in Editor Mode.
	// When enabled, the server or world card is only visible to clients in Editor Mode.
	EditorWorld bool
	// Hardcore indicates that the world is in hardcore mode. When enabled, it is common to also set
	// GameType to Survival (0) as well to reproduce expected behavior.
	Hardcore bool
	// TransportLayer indicates the transport layer used by the server. In vanilla, this is typically
	// 2 for NetherNet. Other values are also supported but are currently not useful in LAN discovery
	// as it only allows connections over NetherNet. Therefore, the purposes or usage of this field is
	// currently unknown.
	TransportLayer uint8
	// ConnectionType indicates the connection type used alongside the transport layer.
	// In vanilla, this is typically 4 for using LAN as a signaling for NetherNet.
	// Other values are supported but are currently not useful in LAN discovery.
	ConnectionType uint8
}

ServerData defines the binary structure representing worlds in Minecraft: Bedrock Edition. It is encapsulated in [ResponsePacket.ApplicationData] and sent in response to RequestPacket broadcasted by clients on port 7551.

func (*ServerData) MarshalBinary

func (d *ServerData) MarshalBinary() ([]byte, error)

MarshalBinary ...

func (*ServerData) UnmarshalBinary

func (d *ServerData) UnmarshalBinary(data []byte) error

UnmarshalBinary ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL