Documentation ¶
Overview ¶
Package gudp provides UDP server and client implementations.
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
Examples ¶
Constants ¶
const (
// FreePortAddress marks the server listens using random free port.
FreePortAddress = ":0"
)
Variables ¶
This section is empty.
Functions ¶
func GetFreePort ¶
GetFreePort retrieves and returns a port that is free.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/net/gudp" ) func main() { fmt.Println(gudp.GetFreePort()) // May Output: // 57429 <nil> }
Output:
func GetFreePorts ¶
GetFreePorts retrieves and returns specified number of ports that are free.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/net/gudp" ) func main() { fmt.Println(gudp.GetFreePorts(2)) // May Output: // [57743 57744] <nil> }
Output:
func MustGetFreePort ¶ added in v2.2.0
func MustGetFreePort() (port int)
MustGetFreePort performs as GetFreePort, but it panics if any error occurs.
func NewNetConn ¶
NewNetConn creates and returns a *net.UDPConn with given addresses.
Types ¶
type Conn ¶
type Conn struct { *net.UDPConn // Underlying UDP connection. // contains filtered or unexported fields }
Conn handles the UDP connection.
func NewConn ¶
NewConn creates UDP connection to `remoteAddress`. The optional parameter `localAddress` specifies the local address for connection.
func NewConnByNetConn ¶
NewConnByNetConn creates an UDP connection object with given *net.UDPConn object.
func (*Conn) Recv ¶
Recv receives and returns data from remote address. The parameter `buffer` is used for customizing the receiving buffer size. If `buffer` <= 0, it uses the default buffer size, which is 1024 byte.
There's package border in UDP protocol, we can receive a complete package if specified buffer size is big enough. VERY NOTE that we should receive the complete package in once or else the leftover package data would be dropped.
func (*Conn) RecvWithTimeout ¶
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
RecvWithTimeout reads data from remote address with timeout.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote address of current UDP connection. Note that it cannot use c.conn.RemoteAddr() as it is nil.
func (*Conn) SendRecvWithTimeout ¶
func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
SendRecvWithTimeout writes data to connection and reads response with timeout.
func (*Conn) SendWithTimeout ¶
SendWithTimeout writes data to connection with timeout.
func (*Conn) SetBufferWaitRecv ¶ added in v2.3.0
SetBufferWaitRecv sets the buffer waiting timeout when reading all data from connection. The waiting duration cannot be too long which might delay receiving data from remote address.
func (*Conn) SetDeadline ¶
SetDeadline sets the read and write deadlines associated with the connection.
func (*Conn) SetDeadlineRecv ¶ added in v2.3.0
SetDeadlineRecv sets the read deadline associated with the connection.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the UDP server.
func GetServer ¶
func GetServer(name ...interface{}) *Server
GetServer creates and returns an UDP server instance with given name.
func NewServer ¶
NewServer creates and returns an UDP server. The optional parameter `name` is used to specify its name, which can be used for GetServer function to retrieve its instance.
func (*Server) GetListenedAddress ¶ added in v2.2.0
GetListenedAddress retrieves and returns the address string which are listened by current server.
func (*Server) GetListenedPort ¶ added in v2.2.0
GetListenedPort retrieves and returns one port which is listened to by current server.
func (*Server) SetAddress ¶
SetAddress sets the server address for UDP server.
func (*Server) SetHandler ¶
SetHandler sets the connection handler for UDP server.