Help us shape the future of Go! Take the 2026 Go Developer Survey

gamelink

package
v0.1.53 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package gamelink carries a game's UDP traffic between two PCs over a peer stream. The guest's game connects to a loopback endpoint; the host forwards each admitted guest to its game server with a socket of its own, so the server sees every guest as a separate client. Each stream opens with a handshake that names the server process the guest expects, and game packets flow only once the host accepts it.

Index

Constants

This section is empty.

Variables

View Source
var ErrGameLeft = errors.New("Left the game")

ErrGameLeft reports that the game stopped sending after the server answered it: the player left the server or quit Deadlock.

View Source
var ErrUnanswered = errors.New("The host's game did not answer. Choose Join again.")

ErrUnanswered reports that the game stopped sending before the server ever answered it: the game gave up connecting to a server that never replied.

Functions

func AcceptGeneration

func AcceptGeneration(ctx context.Context, stream io.ReadWriteCloser) error

AcceptGeneration tells the guest that its forwarding socket is in place, so game packets may flow.

func Forward

func Forward(ctx context.Context, target *net.UDPAddr, peer stream.MessageStream, accept func() error) error

Forward forwards an admitted guest's stream to the game server at target until ctx ends or either side stops. It binds the guest a socket of its own, so the server sees it as a separate client and replies cannot cross guests, and calls accept, such as AcceptGeneration, once that socket is bound. It owns peer even when it fails.

func ReadGeneration

func ReadGeneration(ctx context.Context, stream io.ReadWriteCloser) (uint64, error)

ReadGeneration reads the server generation a guest asks for, before any game packets. The caller compares it with the server it runs.

Types

type Client

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

Client owns one loopback endpoint until the peer disconnects, the game leaves, or Close returns. It serves one server process and is not reused.

func Dial

func Dial(ctx context.Context, b bus.Bus, protocol protocol.ID, local, host peer.ID, generation uint64) (*Client, error)

Dial opens a game stream of protocol to the host peer and keeps its link until forwarding stops. ctx owns both the dial and the endpoint's lifetime. It returns the endpoint only once the host accepts generation and has reserved the guest's socket.

func Open

func Open(ctx context.Context, peer stream.MessageStream) (*Client, error)

Open takes ownership of an admitted game stream and binds the loopback endpoint for it. The caller then points the game at Address; no process is launched.

func (*Client) Address

func (c *Client) Address() string

Address is the game's connect endpoint, valid until Done closes.

func (*Client) Arrived

func (c *Client) Arrived() <-chan struct{}

Arrived closes when the server's first packet reaches the game, so the game's own connect attempts alone never count as arrival.

func (*Client) Close

func (c *Client) Close()

Close cancels forwarding and waits for sockets and pumps to close.

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done closes after forwarding has stopped.

func (*Client) Wait

func (c *Client) Wait() error

Wait returns the forwarding result after both directions stop. ErrGameLeft means the game went quiet after it arrived, and ErrUnanswered that it went quiet before the server ever answered.

Jump to

Keyboard shortcuts

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