Documentation ¶
Overview ¶
Package gtcp provides TCP server and client implementations.
Index ¶
- Constants
- func Checksum(buffer []byte) uint32
- func NewNetConn(addr string, timeout ...int) (net.Conn, error)
- func Send(addr string, data []byte, retry ...Retry) error
- func SendPkg(addr string, data []byte, retry ...Retry) error
- func SendPkgWithTimeout(addr string, data []byte, timeout time.Duration, retry ...Retry) error
- func SendRecv(addr string, data []byte, receive int, retry ...Retry) ([]byte, error)
- func SendRecvPkg(addr string, data []byte, retry ...Retry) ([]byte, error)
- func SendRecvPkgWithTimeout(addr string, data []byte, timeout time.Duration, retry ...Retry) ([]byte, error)
- func SendRecvWithTimeout(addr string, data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
- func SendWithTimeout(addr string, data []byte, timeout time.Duration, retry ...Retry) error
- type Conn
- func (c *Conn) Close()
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error)
- func (c *Conn) RecvLine(retry ...Retry) ([]byte, error)
- func (c *Conn) RecvPkg(retry ...Retry) (result []byte, err error)
- func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) Send(data []byte, retry ...Retry) error
- func (c *Conn) SendPkg(data []byte, retry ...Retry) error
- func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
- func (c *Conn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
- func (c *Conn) SendRecvPkg(data []byte, retry ...Retry) ([]byte, error)
- func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, 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) error
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetRecvBufferWait(bufferWaitDuration time.Duration)
- func (c *Conn) SetRecvDeadline(t time.Time) error
- func (c *Conn) SetSendDeadline(t time.Time) error
- type PoolConn
- func (c *PoolConn) Close() error
- func (c *PoolConn) Recv(length int, retry ...Retry) ([]byte, error)
- func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error)
- func (c *PoolConn) RecvPkg(retry ...Retry) ([]byte, error)
- func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *PoolConn) Send(data []byte, retry ...Retry) error
- func (c *PoolConn) SendPkg(data []byte, retry ...Retry) (err error)
- func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
- func (c *PoolConn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
- func (c *PoolConn) SendRecvPkg(data []byte, retry ...Retry) ([]byte, error)
- func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *PoolConn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
- type Retry
- type Server
Constants ¶
const ( // 允许最大的简单协议包大小(byte), 15MB PKG_MAX_SIZE = 0xFFFFFF // 消息包头大小: "总长度"3字节+"校验码"4字节 PKG_HEADER_SIZE = 7 )
Variables ¶
This section is empty.
Functions ¶
func NewNetConn ¶
创建原生TCP链接, addr地址格式形如:127.0.0.1:80
func SendPkgWithTimeout ¶
简单协议: (面向短链接)带超时时间的数据发送
func SendRecvPkg ¶
简单协议: (面向短链接)发送数据并等待接收返回数据
func SendRecvPkgWithTimeout ¶
func SendRecvPkgWithTimeout(addr string, data []byte, timeout time.Duration, retry ...Retry) ([]byte, error)
简单协议: (面向短链接)发送数据并等待接收返回数据(带返回超时等待时间)
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
封装的链接对象
func (*Conn) Recv ¶
获取数据,指定读取的数据长度(length < 1表示获取所有可读数据),以及重试策略(retry) 需要注意: 1、往往在socket通信中需要指定固定的数据结构,并在设定对应的长度字段,并在读取数据时便于区分包大小; 2、当length < 1时表示获取缓冲区所有的数据,但是可能会引起包解析问题(可能出现非完整的包情况),因此需要解析端注意解析策略;
func (*Conn) RecvPkgWithTimeout ¶
简单协议: 带超时时间的消息包获取
func (*Conn) RecvWithTimeout ¶
带超时时间的数据获取
func (*Conn) RemoteAddr ¶ added in v1.5.0
func (*Conn) SendPkg ¶
根据简单协议发送数据包。 简单协议数据格式:总长度(24bit)|校验码(32bit)|数据(变长)。 注意: 1. "总长度"包含自身3字节及"校验码"4字节。 2. 由于"总长度"为3字节,并且使用的BigEndian字节序,因此最后返回的buffer使用了buffer[1:]。
func (*Conn) SendPkgWithTimeout ¶
简单协议: 带超时时间的数据发送
func (*Conn) SendRecvPkg ¶
简单协议: 发送数据并等待接收返回数据
func (*Conn) SendRecvPkgWithTimeout ¶
func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, 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 (*Conn) SetRecvBufferWait ¶
读取全部缓冲区数据时,读取完毕后的写入等待间隔,如果超过该等待时间后仍无可读数据,那么读取操作返回。 该时间间隔不能设置得太大,会影响Recv读取时长(默认为1毫秒)。
type PoolConn ¶
type PoolConn struct { *Conn // 继承底层链接接口对象 // contains filtered or unexported fields }
链接池链接对象
func (*PoolConn) RecvPkgWithTimeout ¶
简单协议: (方法覆盖)带超时时间的数据获取
func (*PoolConn) RecvWithTimeout ¶
func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error)
(方法覆盖)带超时时间的数据获取
func (*PoolConn) SendPkgWithTimeout ¶
简单协议: (方法覆盖)带超时时间的数据发送
func (*PoolConn) SendRecvPkg ¶
简单协议: (方法覆盖)发送数据并等待接收返回数据
func (*PoolConn) SendRecvPkgWithTimeout ¶
func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) ([]byte, error)
简单协议: (方法覆盖)发送数据并等待接收返回数据(带返回超时等待时间)