GitHubはじめる

API リファレンスはソースコード内の Go ドキュメントコメントから生成されており、英語で記述されています。

package portmap

import "github.com/nsgo-dev/game/pkg/online/portmap"

概要

Package portmap defines the gateway port-mapping contract used by player-hosted servers. Native protocol providers implement Provider, installed with SetProvider, without coupling games to UPnP, NAT-PMP or PCP wire details.

Package portmap is experimental: its API and documented formats may change in a minor release. See the API stability policy.

目次

変数

var (
	// ErrUnsupported reports that no provider is installed for this target.
	ErrUnsupported = errors.New("portmap: unsupported on this target")
	// ErrNoGateway reports that no gateway answered the provider's protocol.
	ErrNoGateway = errors.New("portmap: no gateway found")
	// ErrRefused reports that the gateway answered but refused the mapping.
	ErrRefused = errors.New("portmap: gateway refused mapping")
	// ErrDoubleNAT reports that the gateway's external address is itself private, so a mapping would not be reachable from the internet.
	ErrDoubleNAT = errors.New("portmap: gateway external address is private")
)

関数

func SetProvider(p Provider)

SetProvider makes p the provider Open uses on this target; nil restores the default, which returns ErrUnsupported. It is safe to call while other goroutines call Open.

型

type Mapping interface {
	External() netip.AddrPort
	Protocol() Protocol
	InternalPort() uint16
	Renew(context.Context) error
	Close() error
}

Mapping is a port the gateway forwards: its public address, protocol and internal port. Renew extends it by its lifetime and Close removes it.

func Open(ctx context.Context, req Request) (Mapping, error)

Open validates req and asks the provider set with SetProvider to map it. Without a provider it returns ErrUnsupported.

type Protocol uint8

Protocol is the transport protocol of a mapped port.

const (
	// UDP maps a UDP port.
	UDP Protocol = iota
	// TCP maps a TCP port.
	TCP
)
type Provider interface {
	Open(context.Context, Request) (Mapping, error)
}

Provider opens mappings with one gateway protocol, such as UPnP, NAT-PMP or PCP.

type Request struct {
	Protocol     Protocol
	InternalPort uint16
	ExternalPort uint16
	Description  string
	Lifetime     time.Duration
}

Request asks the gateway to forward ExternalPort (0 lets the gateway choose) to InternalPort on this host for Protocol, for Lifetime, under a description the router shows.

func (r Request) Validate() error

Validate reports an error unless the internal port is non-zero, the protocol is UDP or TCP and the lifetime is positive.