udp类

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

gudp包提供了UDP服务器和客户端的实现。

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连接将数据写入`address`,然后关闭连接。 注意,它适用于短连接场景。

func SendRecv

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

SendRecv 使用UDP连接将数据写入`address`,读取响应后再关闭连接。 注意,它适用于短连接场景。

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(udp *net.UDPConn) *Conn

NewConnByNetConn 通过给定的 *net.UDPConn 对象创建一个 UDP 连接对象。

func (*Conn) Recv

func (c *Conn) Recv(buffer int, retry ...Retry) ([]byte, error)

Recv 从远程地址接收并返回数据。 参数`buffer`用于自定义接收缓冲区大小。如果`buffer` <= 0, 则使用默认的缓冲区大小,即1024字节。

在UDP协议中有包边界的限制,如果指定的缓冲区大小足够大,我们可以一次性接收一个完整的包。 非常注意的是,我们应该一次性接收完整个包,否则剩余的包数据会被丢弃。

func (*Conn) RecvWithTimeout

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

RecvWithTimeout在指定超时时间内从远程地址读取数据。

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) SendRecv

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

SendRecv 向连接写入数据,并阻塞等待读取响应。

func (*Conn) SendRecvWithTimeout

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

SendRecvWithTimeout 将数据写入连接并在超时时间内读取响应。

func (*Conn) SendWithTimeout

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

SendWithTimeout 在设定的超时时间内向连接写入数据。

func (*Conn) SetBufferWaitRecv

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

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

func (*Conn) SetDeadline

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

SetDeadline为连接设置读取和写入的截止时间。

func (*Conn) SetDeadlineRecv

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

SetDeadlineRecv 设置与连接关联的读取截止时间。

func (*Conn) SetDeadlineSend

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

SetDeadlineSend 设置当前连接的发送截止时间。

type Retry

type Retry struct {
	Count    int           // Max retry count.
	Interval time.Duration // Retry interval.
}

type Server

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

Server 是 UDP 服务器。

func GetServer

func GetServer(name ...interface{}) *Server

GetServer 根据给定名称创建并返回一个UDP服务器实例。

func NewServer

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

NewServer 创建并返回一个 UDP 服务器。 可选参数 `name` 用于指定其名称,可用于 GetServer 函数来获取其实例。

func (*Server) Close

func (s *Server) Close() (err error)

Close 关闭连接。 它将使服务器立即关闭。

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() error

Run 开始监听 UDP 连接。

func (*Server) SetAddress

func (s *Server) SetAddress(address string)

SetAddress 设置 UDP 服务器的服务器地址。

func (*Server) SetHandler

func (s *Server) SetHandler(handler func(*Conn))

SetHandler 设置 UDP 服务器的连接处理器。

Jump to

Keyboard shortcuts

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