gudp

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 7 Imported by: 7

Documentation

Overview

Package gudp provides UDP server and client implementations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFreePort

func GetFreePort() (port int, err error)

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

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

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 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 Conn

type Conn struct {
	*net.UDPConn // Underlying UDP connection.
	// contains filtered or unexported fields
}

Conn handles the UDP connection.

func NewConn

func NewConn(remoteAddress string, localAddress ...string) (*Conn, error)

NewConn creates UDP connection to `remoteAddress`. The optional parameter `localAddress` specifies the local address for connection.

func NewConnByNetConn

func NewConnByNetConn(udp *net.UDPConn) *Conn

NewConnByNetConn creates a UDP connection object with given *net.UDPConn object.

func (*Conn) Recv

func (c *Conn) Recv(buffer int, retry ...Retry) ([]byte, 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 (*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

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote address of current UDP connection. Note that it cannot use c.conn.RemoteAddr() as it is nil.

func (*Conn) Send

func (c *Conn) Send(data []byte, retry ...Retry) (err error)

Send writes data to remote address.

func (*Conn) SendRecv

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

SendRecv writes data to connection and blocks reading response.

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

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

SendWithTimeout writes data to connection with timeout.

func (*Conn) SetDeadline

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

func (*Conn) SetRecvBufferWait

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

SetRecvBufferWait 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) SetRecvDeadline

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

func (*Conn) SetSendDeadline

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

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 is the UDP server.

func GetServer

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

GetServer creates and returns a UDP server instance with given name.

func NewServer

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

NewServer creates and returns a 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) 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 func(*Conn))

SetHandler sets the connection handler for UDP server.

Jump to

Keyboard shortcuts

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