lan

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package lan implements a mocked Warcraft III LAN client that can be used to discover local games.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/nielsAD/gowarcraft3/network/lan"
	"github.com/nielsAD/gowarcraft3/protocol/w3gs"
)

func main() {
	var game = w3gs.GameVersion{
		Product: w3gs.ProductTFT,
		Version: w3gs.CurrentGameVersion,
	}

	// Find LAN game
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
	addr, id, secret, err := lan.FindGame(ctx, game)

	cancel()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Found game at %s (id: %d, secret %d)\n", addr, id, secret)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var MulticastGroup = net.UDPAddr{IP: net.IPv4(224, 0, 0, 251), Port: 5353}

MulticastGroup endpoint

View Source
var TypeCacheFlush uint16 = 1 << 15

TypeCacheFlush bit

View Source
var TypeUnicastResponse uint16 = 1 << 15

TypeUnicastResponse bit

Functions

func FindGame

func FindGame(ctx context.Context, gv w3gs.GameVersion) (addr string, hostCounter uint32, entryKey uint32, err error)

FindGame returns entry information for an arbitrary game hosted in LAN

Types

type Advertiser

type Advertiser interface {
	network.Listener

	Create() error
	Refresh(slotsUsed uint32, slotsAvailable uint32) error
	Decreate() error

	Run() error
	Close() error
}

Advertiser broadcasts available game information to the Local Area Network Emits events for every received packet, responds to search queries

func NewAdvertiser

func NewAdvertiser(info *w3gs.GameInfo) (Advertiser, error)

NewAdvertiser initializes proper Advertiser type for game version

type DNSPacketConn

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

DNSPacketConn manages a UDP connection that transfers DNS packets. Public methods/fields are thread-safe unless explicitly stated otherwise

func NewDNSPacketConn

func NewDNSPacketConn(conn net.PacketConn) *DNSPacketConn

NewDNSPacketConn returns conn wrapped in DNSPacketConn

func (*DNSPacketConn) Broadcast

func (c *DNSPacketConn) Broadcast(pkt *dns.Msg) (int, error)

Broadcast a packet over LAN

func (*DNSPacketConn) Close

func (c *DNSPacketConn) Close() error

Close the connection

func (*DNSPacketConn) Conn

func (c *DNSPacketConn) Conn() net.PacketConn

Conn returns the underlying net.PacketConn

func (*DNSPacketConn) NextPacket

func (c *DNSPacketConn) NextPacket(timeout time.Duration) (*dns.Msg, net.Addr, error)

NextPacket waits for the next packet (with given timeout) and returns its deserialized representation Not safe for concurrent invocation

func (*DNSPacketConn) Run

func (c *DNSPacketConn) Run(f network.Emitter, timeout time.Duration) error

Run reads packets (with given max time between packets) from Conn and emits an event for each received packet Not safe for concurrent invocation

func (*DNSPacketConn) Send

func (c *DNSPacketConn) Send(addr net.Addr, pkt *dns.Msg) (int, error)

Send pkt to addr over net.PacketConn

func (*DNSPacketConn) SetConn

func (c *DNSPacketConn) SetConn(conn net.PacketConn)

SetConn closes the old connection and starts using the new net.PacketConn

func (*DNSPacketConn) SetWriteTimeout

func (c *DNSPacketConn) SetWriteTimeout(wto time.Duration)

SetWriteTimeout for Send() calls

type GameList

type GameList interface {
	network.Listener
	Games() map[string]w3gs.GameInfo
	Run() error
	Close() error
}

GameList keeps track of all the hosted games in the Local Area Network Emits events for every received packet and Update{} when the output of Games() changes

func NewGameList

func NewGameList(gv w3gs.GameVersion) (GameList, error)

NewGameList initializes proper GameList type for game version

type MDNSAdvertiser

type MDNSAdvertiser struct {
	network.EventEmitter
	DNSPacketConn

	// Set once before Run(), read-only after that
	BroadcastInterval time.Duration
	// contains filtered or unexported fields
}

MDNSAdvertiser advertises a hosted game in the Local Area Network using MDNS (Bonjour)

func NewMDNSAdvertiser

func NewMDNSAdvertiser(info *w3gs.GameInfo) (*MDNSAdvertiser, error)

NewMDNSAdvertiser initializes MDNSAdvertiser struct

func (*MDNSAdvertiser) Close

func (a *MDNSAdvertiser) Close() error

Close the connection

func (*MDNSAdvertiser) Create

func (a *MDNSAdvertiser) Create() error

Create local game

func (*MDNSAdvertiser) Decreate

func (a *MDNSAdvertiser) Decreate() error

Decreate game

func (*MDNSAdvertiser) InitDefaultHandlers

func (a *MDNSAdvertiser) InitDefaultHandlers()

InitDefaultHandlers adds the default callbacks for relevant packets

func (*MDNSAdvertiser) Refresh

func (a *MDNSAdvertiser) Refresh(slotsUsed uint32, slotsAvailable uint32) error

Refresh game info

func (*MDNSAdvertiser) Run

func (a *MDNSAdvertiser) Run() error

Run broadcasts gameinfo in Local Area Network

type MDNSGameList

type MDNSGameList struct {
	network.EventEmitter
	DNSPacketConn

	// Set once before Run(), read-only after that
	GameVersion       w3gs.GameVersion
	BroadcastInterval time.Duration
	// contains filtered or unexported fields
}

MDNSGameList keeps track of all the hosted games in the Local Area Network using MDNS (Bonjour) Emits events for every received packet and Update{} when the output of Games() changes Public methods/fields are thread-safe unless explicitly stated otherwise

func NewMDNSGameList

func NewMDNSGameList(gv w3gs.GameVersion) (*MDNSGameList, error)

NewMDNSGameList opens a new UDP socket to listen for MDNS GameList updates

func (*MDNSGameList) Games

func (g *MDNSGameList) Games() map[string]w3gs.GameInfo

Games returns the current list of LAN games. Map key is the remote address.

func (*MDNSGameList) InitDefaultHandlers

func (g *MDNSGameList) InitDefaultHandlers()

InitDefaultHandlers adds the default callbacks for relevant packets

func (*MDNSGameList) Run

func (g *MDNSGameList) Run() error

Run reads packets from Conn and emits an event for each received packet Not safe for concurrent invocation

type UDPAdvertiser

type UDPAdvertiser struct {
	network.EventEmitter
	network.W3GSPacketConn

	// Set once before Run(), read-only after that
	BroadcastInterval time.Duration
	// contains filtered or unexported fields
}

UDPAdvertiser advertises a hosted game in the Local Area Network using UDP broadcast

func NewUDPAdvertiser

func NewUDPAdvertiser(info *w3gs.GameInfo, port int) (*UDPAdvertiser, error)

NewUDPAdvertiser initializes UDPAdvertiser struct

func (*UDPAdvertiser) Close

func (a *UDPAdvertiser) Close() error

Close the connection

func (*UDPAdvertiser) Create

func (a *UDPAdvertiser) Create() error

Create local game

func (*UDPAdvertiser) Decreate

func (a *UDPAdvertiser) Decreate() error

Decreate game

func (*UDPAdvertiser) InitDefaultHandlers

func (a *UDPAdvertiser) InitDefaultHandlers()

InitDefaultHandlers adds the default callbacks for relevant packets

func (*UDPAdvertiser) Refresh

func (a *UDPAdvertiser) Refresh(slotsUsed uint32, slotsAvailable uint32) error

Refresh game info

func (*UDPAdvertiser) Run

func (a *UDPAdvertiser) Run() error

Run broadcasts gameinfo in Local Area Network

type UDPGameList

type UDPGameList struct {
	network.EventEmitter
	network.W3GSPacketConn

	// Set once before Run(), read-only after that
	GameVersion       w3gs.GameVersion
	BroadcastInterval time.Duration
	// contains filtered or unexported fields
}

UDPGameList keeps track of all the hosted games in the Local Area Network using UDP broadcast Emits events for every received packet and Update{} when the output of Games() changes Public methods/fields are thread-safe unless explicitly stated otherwise

func NewUDPGameList

func NewUDPGameList(gv w3gs.GameVersion, port int) (*UDPGameList, error)

NewUDPGameList opens a new UDP socket to listen for LAN GameList updates

func (*UDPGameList) Encoding

func (g *UDPGameList) Encoding() w3gs.Encoding

Encoding for w3gs packets

func (*UDPGameList) Games

func (g *UDPGameList) Games() map[string]w3gs.GameInfo

Games returns the current list of LAN games. Map key is the remote address.

func (*UDPGameList) InitDefaultHandlers

func (g *UDPGameList) InitDefaultHandlers()

InitDefaultHandlers adds the default callbacks for relevant packets

func (*UDPGameList) Run

func (g *UDPGameList) Run() error

Run reads packets from Conn and emits an event for each received packet Not safe for concurrent invocation

type Update

type Update struct{}

Update event for GameList changes

Jump to

Keyboard shortcuts

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