ports

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ports provides PortManager that manages allocating, reserving and releasing ports.

Index

Constants

View Source
const (
	// 临时端口的最小值
	FirstEphemeral = 16000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type PortManager

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

管理端口的对象,由它来保留和释放端口

func NewPortManager

func NewPortManager() *PortManager

NewPortManager 新建一个端口管理器

func (*PortManager) IsPortAvailable

func (s *PortManager) IsPortAvailable(networks []tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber,
	addr tcpip.Address, port uint16) bool

IsPortAvailable tests if the given port is available on all given protocols.

func (*PortManager) PickEphemeralPort

func (s *PortManager) PickEphemeralPort(testPort func(p uint16) (bool, *tcpip.Error)) (port uint16, err *tcpip.Error)

PickEphemeralPort 从端口管理器中随机分配一个端口,并调用testPort来检测是否可用。

func (*PortManager) ReleasePort

func (s *PortManager) ReleasePort(networks []tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber,
	addr tcpip.Address, port uint16)

ReleasePort releases the reservation on a port/IP combination so that it can be reserved by other endpoints. 释放绑定的端口,以便别的程序复用。

func (*PortManager) ReservePort

func (s *PortManager) ReservePort(networks []tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber,
	addr tcpip.Address, port uint16) (reservedPort uint16, err *tcpip.Error)

ReservePort 将端口和IP地址绑定在一起,这样别的程序就无法使用已经被绑定的端口。 如果传入的端口不为0,那么会尝试绑定该端口,若该端口没有被占用,那么绑定成功。 如果传人的端口等于0,那么就是告诉协议栈自己分配端口,端口管理器就会随机返回一个端口。

Jump to

Keyboard shortcuts

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