gudp

package
v2.7.8 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gudp provides UDP server and client implementations.

Index

Examples

Constants

View Source
const (
	// FreePortAddress marks the server listens using random free port.
	FreePortAddress = ":0"
)

Variables

This section is empty.

Functions

func GetFreePort

func GetFreePort() (port int, err error)

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

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

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

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

NewNetConn creates and returns a *net.UDPConn with given addresses.

func Send

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

Send writes data to `address` using UDP connection and then closes the connection. Note that it is used for short connection usage.

func SendRecv

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

SendRecv writes data to `address` using UDP connection, reads response and then closes the connection. Note that it is used for short connection usage.

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

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

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

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

SendRecv writes data to connection and blocks reading response.

func (ClientConn) SetDeadline added in v2.7.8

func (c ClientConn) SetDeadline(t time.Time) (err error)

SetDeadline sets the read and write deadlines associated with the connection.

func (ClientConn) SetDeadlineRecv added in v2.7.8

func (c ClientConn) SetDeadlineRecv(t time.Time) (err error)

SetDeadlineRecv sets the read deadline associated with the connection.

func (ClientConn) SetDeadlineSend added in v2.7.8

func (c ClientConn) SetDeadlineSend(t time.Time) (err error)

SetDeadlineSend sets the deadline of sending for current connection.

type Retry

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

Retry holds the retry options. TODO replace with standalone retry package.

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

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

Close closes the connection. It will make server shutdowns immediately.

func (*Server) GetListenedAddress

func (s *Server) GetListenedAddress() string

GetListenedAddress retrieves and returns the address string which are listened by current server.

func (*Server) GetListenedPort

func (s *Server) GetListenedPort() int

GetListenedPort retrieves and returns one port which is listened to by current server.

func (*Server) Run

func (s *Server) Run() error

Run starts listening UDP connection.

func (*Server) SetAddress

func (s *Server) SetAddress(address string)

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

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

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) Send added in v2.7.8

func (c *ServerConn) Send(data []byte, remoteAddr *net.UDPAddr, retry ...Retry) (err error)

Send writes data to remote address.

func (ServerConn) SetDeadline added in v2.7.8

func (c ServerConn) SetDeadline(t time.Time) (err error)

SetDeadline sets the read and write deadlines associated with the connection.

func (ServerConn) SetDeadlineRecv added in v2.7.8

func (c ServerConn) SetDeadlineRecv(t time.Time) (err error)

SetDeadlineRecv sets the read deadline associated with the connection.

func (ServerConn) SetDeadlineSend added in v2.7.8

func (c ServerConn) SetDeadlineSend(t time.Time) (err error)

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.

Jump to

Keyboard shortcuts

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