nazanet

package
v0.30.10 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 5 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNazaNet = errors.New("nazanet: fxxk")

Functions

This section is empty.

Types

type AvailUdpConnPool added in v0.19.0

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

从指定的UDP端口范围内,寻找可绑定监听的端口,绑定监听并返回 Pool只提供Acquire获取接口,不提供释放接口,连接资源是标准*net.UDPConn对象,需要释放时,外部直接Close即可

func NewAvailUdpConnPool added in v0.19.0

func NewAvailUdpConnPool(minPort uint16, maxPort uint16) *AvailUdpConnPool

func (*AvailUdpConnPool) Acquire added in v0.19.0

func (a *AvailUdpConnPool) Acquire() (*net.UDPConn, uint16, error)

func (*AvailUdpConnPool) Acquire2 added in v0.19.0

func (a *AvailUdpConnPool) Acquire2() (*net.UDPConn, uint16, *net.UDPConn, uint16, error)

有的业务场景,需要返回两个可用的端口,并且必须是连续的 @return 前面的是端口小的,后面的是端口+1的

func (*AvailUdpConnPool) Peek added in v0.19.0

func (a *AvailUdpConnPool) Peek() (uint16, error)

通过Acquire获取到可用net.UDPConn对象后,将对象关闭,只返回可用的端口

type ModUdpConnectionOption added in v0.19.0

type ModUdpConnectionOption func(option *UdpConnectionOption)

type OnReadUdpPacket added in v0.19.0

type OnReadUdpPacket func(b []byte, raddr *net.UDPAddr, err error) bool

OnReadUdpPacket

@return 上层回调返回false,则关闭UdpConnection

type UdpConnection added in v0.19.0

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

UdpConnection xxx

func NewUdpConnection added in v0.19.0

func NewUdpConnection(modOptions ...ModUdpConnectionOption) (*UdpConnection, error)

func (*UdpConnection) Dispose added in v0.19.0

func (c *UdpConnection) Dispose() error

func (*UdpConnection) ReadWithTimeout added in v0.19.0

func (c *UdpConnection) ReadWithTimeout(timeoutMs int) ([]byte, *net.UDPAddr, error)

ReadWithTimeout 直接读取数据,不使用RunLoop

func (*UdpConnection) RunLoop added in v0.19.0

func (c *UdpConnection) RunLoop(onRead OnReadUdpPacket) error

RunLoop 阻塞直至Read发生错误或上层回调函数返回false

@return error: 如果外部调用Dispose,会返回error

注意,回调存在err!=nil(*net.OpError, Err={error | poll.errNetClosing} use of closed network connection), len==0的情况

func (*UdpConnection) SetReadBuffer added in v0.30.6

func (c *UdpConnection) SetReadBuffer(bufSize int) error

func (*UdpConnection) SetWriteBuffer added in v0.30.6

func (c *UdpConnection) SetWriteBuffer(bufSize int) error

func (*UdpConnection) Write added in v0.19.0

func (c *UdpConnection) Write(b []byte) error

func (*UdpConnection) Write2Addr added in v0.19.0

func (c *UdpConnection) Write2Addr(b []byte, ruaddr *net.UDPAddr) error

type UdpConnectionOption added in v0.19.0

type UdpConnectionOption struct {
	// 两种初始化方式:
	// 方式一:直接传入外部创建好的连接对象供内部使用
	Conn *net.UDPConn
	// 方式二:填入地址,内部创建连接对象
	// LAddr: 本地bind地址,如果设置为空,则自动选择可用端口
	//        比如作为客户端时,如果不想特别指定本地端口,可以设置为空
	//
	// RAddr: 对端地址,决定 UdpConnection.Write 函数的发送行为。
	// 注意,对端地址需显式填写IP。
	// 注意,即使使用方式一,也可以设置Rddr。
	// 作用: 比如作为客户端时,对端地址通常只有一个,在构造函数中指定,后续就不用每次发送都指定。
	//
	// 发送有两个函数 UdpConnection.Write 和 UdpConnection.Write2Addr:
	// UdpConnection.Write2Addr 在自身函数参数中显示指定对端地址,跟此处的 RAddr 不相关。
	// UdpConnection.Write 自身函数参数不包含对端地址,当此处的 RAddr 存在时,使用此处的地址,否则,使用读取到数据时的对端地址。
	// 注意,如果此处 RAddr 没有设置,且没有读取过数据,那么使用 UdpConnection.Write 将直接返回错误。
	LAddr string
	RAddr string

	MaxReadPacketSize int  // 读取数据时,内存块大小
	AllocEachRead     bool // 使用Read Loop时,是否每次读取都申请新的内存块,如果为false,则复用一块内存块
}

Jump to

Keyboard shortcuts

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