nazanet

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2020 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

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

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

func NewAvailUDPConnPool

func NewAvailUDPConnPool(minPort uint16, maxPort uint16) *AvailUDPConnPool

func (*AvailUDPConnPool) Acquire

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

func (*AvailUDPConnPool) Acquire2

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

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

func (*AvailUDPConnPool) Peek

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

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

type ModUDPConnectionOption added in v0.15.0

type ModUDPConnectionOption func(option *UDPConnectionOption)

type OnReadUDPPacket

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

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

type UDPConnection

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

func NewUDPConnection added in v0.14.0

func NewUDPConnection(modOptions ...ModUDPConnectionOption) (*UDPConnection, error)

func (*UDPConnection) Dispose

func (c *UDPConnection) Dispose() error

func (*UDPConnection) ReadWithTimeout added in v0.14.0

func (c *UDPConnection) ReadWithTimeout(timeoutMS int) ([]byte, *net.UDPAddr, error)

直接读取数据,不使用RunLoop

func (*UDPConnection) RunLoop

func (c *UDPConnection) RunLoop(onRead OnReadUDPPacket) error

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

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

func (*UDPConnection) Write

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

func (*UDPConnection) Write2Addr added in v0.14.0

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

type UDPConnectionOption added in v0.15.0

type UDPConnectionOption struct {
	// 两种初始化方式:
	// 方式一:直接传入外部创建好的连接对象供内部使用
	Conn *net.UDPConn
	// 方式二:填入地址,内部创建连接对象
	// LAddr: 本地bind地址,如果设置为空,则自动选择可用端口
	//        比如作为客户端时,如果不想特别指定本地端口,可以设置为空
	//
	// Raddr: 如果为空,则只能使用func Write2Addr携带对端地址进行发送,不能使用func Write
	//        不为空的作用:作为客户端时,对端地址通常只有一个,在构造函数中指定,后续就不用每次发送都指定
	//        注意,对端地址需显示填写IP
	//
	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