xudp

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FreePortAddress 使用随机端口标记服务器监听。
	FreePortAddress = ":0"
)

Variables

This section is empty.

Functions

func GetFreePort

func GetFreePort() (port int, err error)

GetFreePort 检索并返回一个空闲的端口。

func GetFreePorts

func GetFreePorts(count int) (ports []int, err error)

GetFreePorts 检索并返回指定数量的空闲端口。

func MustGetFreePort

func MustGetFreePort() (port int)

MustGetFreePort 执行 GetFreePort,但发生任何错误都会 panic。

func NewNetConn

func NewNetConn(remoteAddress string, localAddress ...string) (*net.UDPConn, error)

NewNetConn 创建并返回具有指定地址的 *net.UDPConn。

func Send

func Send(address string, data []byte, retry ...Retry) error

Send 使用 UDP 连接将数据写入“地址”,然后关闭连接。 请注意,它用于短连接使用。

func SendReceive

func SendReceive(address string, data []byte, receive int, retry ...Retry) ([]byte, error)

SendReceive 使用 UDP 连接将数据写入“地址”,读取响应,然后关闭连接。 请注意,它用于短连接使用。

Types

type Conn

type Conn struct {
	*net.UDPConn // 底层 UDP 连接。
	// contains filtered or unexported fields
}

Conn 处理 UDP 连接。

func NewConn

func NewConn(remoteAddress string, localAddress ...string) (*Conn, error)

NewConn 创建到 `remoteAddress` 的 UDP 连接。 可选参数“localAddress”指定连接的本地地址。

func NewConnByNetConn

func NewConnByNetConn(conn *net.UDPConn) *Conn

NewConnByNetConn 使用指定的 *net.UDPConn 对象创建一个 UDP 连接对象。

func (*Conn) Receive

func (c *Conn) Receive(length int, retry ...Retry) ([]byte, error)

Receive 从远程地址接收和返回数据。

func (*Conn) ReceiveWithTimeout

func (c *Conn) ReceiveWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)

ReceiveWithTimeout 从远程地址读取数据超时。

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr 返回当前 UDP 连接的远程地址。 请注意,它不能使用 c.conn.RemoteAddr(),因为它是 nil。

func (*Conn) Send

func (c *Conn) Send(data []byte, retry ...Retry) (err error)

Send 将数据写入远程地址。

func (*Conn) SendReceive

func (c *Conn) SendReceive(data []byte, receive int, retry ...Retry) ([]byte, error)

SendReceive 将数据写入连接并阻止读取响应。

func (*Conn) SendReceiveWithTimeout

func (c *Conn) SendReceiveWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)

SendReceiveWithTimeout 将数据写入连接并读取超时响应。

func (*Conn) SendWithTimeout

func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error)

SendWithTimeout 将数据写入超时连接。

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) (err error)

func (*Conn) SetReceiveBufferWait

func (c *Conn) SetReceiveBufferWait(d time.Duration)

SetReceiveBufferWait 从连接读取所有数据时设置缓冲区等待超时。 等待时间不能太长,否则可能会延迟从远程地址接收数据。

func (*Conn) SetReceiveDeadline

func (c *Conn) SetReceiveDeadline(t time.Time) (err error)

func (*Conn) SetSendDeadline

func (c *Conn) SetSendDeadline(t time.Time) (err error)

type Retry

type Retry struct {
	Count    int           // 最大重试次数。
	Interval time.Duration // 重试间隔。
}

type Server

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

Server 定义 UDP 服务器。

func NewServer

func NewServer(address string, handler func(*Conn)) *Server

NewServer 新建 UDP 服务器。

func (*Server) Close

func (s *Server) Close(ctx context.Context) (err error)

Close 关闭 UDP 服务器。

func (*Server) GetListenedAddress

func (s *Server) GetListenedAddress() string

GetListenedAddress 获取当前服务器监听地址。

func (*Server) GetListenedPort

func (s *Server) GetListenedPort() int

GetListenedPort 获取当前服务器监听端口。

func (*Server) Run

func (s *Server) Run(ctx context.Context) (err error)

Run 启动 UDP 服务器。

Jump to

Keyboard shortcuts

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