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 ClientConn
- func (c ClientConn) Recv(buffer int, retry ...Retry) ([]byte, *net.UDPAddr, error)
- func (c *ClientConn) Send(data []byte, retry ...Retry) (err error)
- func (c *ClientConn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
- func (c ClientConn) SetDeadline(t time.Time) (err error)
- func (c ClientConn) SetDeadlineRecv(t time.Time) (err error)
- func (c ClientConn) SetDeadlineSend(t time.Time) (err error)
- type Retry
- type Server
- type ServerConn
- func (c ServerConn) Recv(buffer int, retry ...Retry) ([]byte, *net.UDPAddr, error)
- func (c *ServerConn) Send(data []byte, remoteAddr *net.UDPAddr, retry ...Retry) (err error)
- func (c ServerConn) SetDeadline(t time.Time) (err error)
- func (c ServerConn) SetDeadlineRecv(t time.Time) (err error)
- func (c ServerConn) SetDeadlineSend(t time.Time) (err error)
- type ServerHandler
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. Deprecated: the port might be used soon after they were returned, please use `:0` as the listening address which asks system to assign a free port instead.
Example ¶
package main import ( "fmt" "github.com/joy12825/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. Deprecated: the ports might be used soon after they were returned, please use `:0` as the listening address which asks system to assign a free port instead.
Example ¶
package main import ( "fmt" "github.com/joy12825/gf/v2/net/gudp" ) func main() { fmt.Println(gudp.GetFreePorts(2)) // May Output: // [57743 57744] <nil> }
Output:
func MustGetFreePort ¶
func MustGetFreePort() (port int)
MustGetFreePort performs as GetFreePort, but it panics if any error occurs. Deprecated: the port might be used soon after they were returned, please use `:0` as the listening address which asks system to assign a free port instead.
func NewNetConn ¶
NewNetConn creates and returns a *net.UDPConn with given addresses.
Types ¶
type ClientConn ¶ added in v2.7.8
type ClientConn struct {
// contains filtered or unexported fields
}
ClientConn holds the client side connection.
func NewClientConn ¶ added in v2.7.8
func NewClientConn(remoteAddress string, localAddress ...string) (*ClientConn, error)
NewClientConn creates UDP connection to `remoteAddress`. The optional parameter `localAddress` specifies the local address for connection.
func (ClientConn) Recv ¶ added in v2.7.8
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 (*ClientConn) Send ¶ added in v2.7.8
func (c *ClientConn) Send(data []byte, retry ...Retry) (err error)
Send writes data to remote address.
func (*ClientConn) SendRecv ¶ added in v2.7.8
SendRecv writes data to connection and blocks reading response.
func (ClientConn) SetDeadline ¶ added in v2.7.8
SetDeadline sets the read and write deadlines associated with the connection.
func (ClientConn) SetDeadlineRecv ¶ added in v2.7.8
SetDeadlineRecv sets the read deadline associated with the connection.
func (ClientConn) SetDeadlineSend ¶ added in v2.7.8
SetDeadlineSend sets the deadline of sending for current 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 ¶
func NewServer(address string, handler ServerHandler, name ...string) *Server
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 ¶
GetListenedAddress retrieves and returns the address string which are listened by current server.
func (*Server) GetListenedPort ¶
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 ¶
func (s *Server) SetHandler(handler ServerHandler)
SetHandler sets the connection handler for UDP server.
type ServerConn ¶ added in v2.7.8
type ServerConn struct {
// contains filtered or unexported fields
}
ServerConn holds the server side connection.
func NewServerConn ¶ added in v2.7.8
func NewServerConn(listenedConn *net.UDPConn) *ServerConn
NewServerConn creates an udp connection that listens to `localAddress`.
func (ServerConn) Recv ¶ added in v2.7.8
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 (ServerConn) SetDeadline ¶ added in v2.7.8
SetDeadline sets the read and write deadlines associated with the connection.
func (ServerConn) SetDeadlineRecv ¶ added in v2.7.8
SetDeadlineRecv sets the read deadline associated with the connection.
func (ServerConn) SetDeadlineSend ¶ added in v2.7.8
SetDeadlineSend sets the deadline of sending for current connection.
type ServerHandler ¶ added in v2.7.8
type ServerHandler func(conn *ServerConn)
ServerHandler handles all server connections.