gudp

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package gtcp provides UDP server and client implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNetConn

func NewNetConn(raddr string, laddr ...string) (*net.UDPConn, error)

创建标准库UDP链接操作对象

func Send

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

(面向短链接)发送数据

func SendRecv

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

(面向短链接)发送数据并等待接收返回数据

Types

type Conn

type Conn struct {
	*net.UDPConn // 底层链接对象
	// contains filtered or unexported fields
}

封装的UDP链接对象

func NewConn

func NewConn(raddr string, laddr ...string) (*Conn, error)

创建TCP链接

func NewConnByNetConn

func NewConnByNetConn(udp *net.UDPConn) *Conn

将*net.UDPConn对象转换为*Conn对象

func (*Conn) Recv

func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error)

接收UDP协议数据.

注意事项: 1、UDP协议存在消息边界,因此使用 length < 0 可以获取缓冲区所有消息包数据,即一个完整包; 2、当length = 0时,表示获取当前的缓冲区数据,获取一次后立即返回;

func (*Conn) RecvWithTimeout

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

带超时时间的数据获取

func (*Conn) RemoteAddr

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

不能使用c.conn.RemoteAddr(),其返回为nil, 这里使用c.raddr获取远程连接地址。

func (*Conn) Send

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

发送数据

func (*Conn) SendRecv

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

发送数据并等待接收返回数据

func (*Conn) SendRecvWithTimeout

func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)

发送数据并等待接收返回数据(带返回超时等待时间)

func (*Conn) SendWithTimeout

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

带超时时间的数据发送

func (*Conn) SetDeadline

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

func (*Conn) SetRecvBufferWait

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

读取全部缓冲区数据时,读取完毕后的写入等待间隔,如果超过该等待时间后仍无可读数据,那么读取操作返回。 该时间间隔不能设置得太大,会影响Recv读取时长(默认为1毫秒)。

func (*Conn) SetRecvDeadline

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

func (*Conn) SetSendDeadline

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

type Retry

type Retry struct {
	Count    int // 重试次数
	Interval int // 重试间隔(毫秒)
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

tcp server结构体

func GetServer

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

获取/创建一个空配置的UDP Server 单例模式,请保证name的唯一性

func NewServer

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

创建一个tcp server对象,并且可以选择指定一个单例名字

func (*Server) Close

func (s *Server) Close() error

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

func (*Server) Run

func (s *Server) Run() error

执行监听

func (*Server) SetAddress

func (s *Server) SetAddress(address string)

设置参数 - address

func (*Server) SetHandler

func (s *Server) SetHandler(handler func(*Conn))

设置参数 - handler

Jump to

Keyboard shortcuts

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