Documentation ¶
Overview ¶
gudp包提供了UDP服务器和客户端的实现。
Index ¶
- Constants
- func GetFreePort() (port int, err error)
- func GetFreePorts(count int) (ports []int, err error)
- func MustGetFreePort() (port int)
- func NewNetConn(remoteAddress string, localAddress ...string) (*net.UDPConn, error)
- func Send(address string, data []byte, retry ...Retry) error
- func SendRecv(address string, data []byte, receive int, retry ...Retry) ([]byte, error)
- type Conn
- func (c *Conn) Recv(buffer int, retry ...Retry) ([]byte, error)
- func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) Send(data []byte, retry ...Retry) (err error)
- func (c *Conn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
- func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error)
- func (c *Conn) SetBufferWaitRecv(d time.Duration)
- func (c *Conn) SetDeadline(t time.Time) (err error)
- func (c *Conn) SetDeadlineRecv(t time.Time) (err error)
- func (c *Conn) SetDeadlineSend(t time.Time) (err error)
- type Retry
- type Server
Constants ¶
const (
// FreePortAddress 表示服务器使用随机空闲端口进行监听。
FreePortAddress = ":0"
)
Variables ¶
This section is empty.
Functions ¶
func GetFreePorts ¶
GetFreePorts 获取并返回指定数量的空闲端口。
func MustGetFreePort ¶
func MustGetFreePort() (port int)
MustGetFreePort 的行为与 GetFreePort 相同,但是当出现任何错误时它会触发 panic。
func NewNetConn ¶
NewNetConn 根据给定的地址创建并返回一个 *net.UDPConn 类型的实例。
Types ¶
type Conn ¶
Conn 处理UDP连接。
func NewConnByNetConn ¶
NewConnByNetConn 通过给定的 *net.UDPConn 对象创建一个 UDP 连接对象。
func (*Conn) Recv ¶
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 ¶
RemoteAddr 返回当前 UDP 连接的远程地址。 注意,由于 c.conn.RemoteAddr() 为 nil,所以不能使用它。
func (*Conn) SendRecvWithTimeout ¶
func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
SendRecvWithTimeout 将数据写入连接并在超时时间内读取响应。
func (*Conn) SendWithTimeout ¶
SendWithTimeout 在设定的超时时间内向连接写入数据。
func (*Conn) SetBufferWaitRecv ¶
SetBufferWaitRecv 设置从连接读取所有数据时的缓冲等待超时时间。 等待时长不能过长,否则可能延迟从远程地址接收数据。
func (*Conn) SetDeadline ¶
SetDeadline为连接设置读取和写入的截止时间。
func (*Conn) SetDeadlineRecv ¶
SetDeadlineRecv 设置与连接关联的读取截止时间。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 是 UDP 服务器。
func (*Server) GetListenedAddress ¶
GetListenedAddress 获取并返回当前服务器正在监听的地址字符串。
func (*Server) GetListenedPort ¶
GetListenedPort 获取并返回当前服务器正在监听的一个端口。
func (*Server) SetAddress ¶
SetAddress 设置 UDP 服务器的服务器地址。
func (*Server) SetHandler ¶
SetHandler 设置 UDP 服务器的连接处理器。