gf: github.com/johng-cn/gf/g/net/gtcp Index | Files

package gtcp

import "github.com/johng-cn/gf/g/net/gtcp"

Package gtcp provides TCP server and client implementations.

Index

Package Files

gtcp.go gtcp_conn.go gtcp_conn_pkg.go gtcp_func.go gtcp_func_pkg.go gtcp_pool.go gtcp_pool_pkg.go gtcp_server.go

func Checksum Uses

func Checksum(buffer []byte) uint32

Deprecated. 常见的二进制数据校验方式,生成校验结果

func LoadKeyCrt Uses

func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error)

根据证书和密钥生成TLS对象

func NewNetConn Uses

func NewNetConn(addr string, timeout ...int) (net.Conn, error)

创建原生TCP链接, addr地址格式形如:127.0.0.1:80

func NewNetConnKeyCrt Uses

func NewNetConnKeyCrt(addr, crtFile, keyFile string) (net.Conn, error)

根据给定的证书和密钥文件创建支持TLS的原生TCP链接, addr地址格式形如:127.0.0.1:80

func NewNetConnTLS Uses

func NewNetConnTLS(addr string, tlsConfig *tls.Config) (net.Conn, error)

创建支持TLS的原生TCP链接, addr地址格式形如:127.0.0.1:80

func Send Uses

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

(面向短链接)发送数据

func SendPkg Uses

func SendPkg(addr string, data []byte, option ...PkgOption) error

简单协议: (面向短链接)发送消息包

func SendPkgWithTimeout Uses

func SendPkgWithTimeout(addr string, data []byte, timeout time.Duration, option ...PkgOption) error

简单协议: (面向短链接)带超时时间的数据发送

func SendRecv Uses

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

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

func SendRecvPkg Uses

func SendRecvPkg(addr string, data []byte, option ...PkgOption) ([]byte, error)

简单协议: (面向短链接)发送数据并等待接收返回数据

func SendRecvPkgWithTimeout Uses

func SendRecvPkgWithTimeout(addr string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)

简单协议: (面向短链接)发送数据并等待接收返回数据(带返回超时等待时间)

func SendRecvWithTimeout Uses

func SendRecvWithTimeout(addr string, data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)

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

func SendWithTimeout Uses

func SendWithTimeout(addr string, data []byte, timeout time.Duration, retry ...Retry) error

(面向短链接)带超时时间的数据发送

type Conn Uses

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

封装的链接对象

func NewConn Uses

func NewConn(addr string, timeout ...int) (*Conn, error)

创建TCP链接

func NewConnByNetConn Uses

func NewConnByNetConn(conn net.Conn) *Conn

将net.Conn接口对象转换为*gtcp.Conn对象

func NewConnKeyCrt Uses

func NewConnKeyCrt(addr, crtFile, keyFile string) (*Conn, error)

根据证书和密钥文件创建支持TLS加密通信的TCP链接

func NewConnTLS Uses

func NewConnTLS(addr string, tlsConfig *tls.Config) (*Conn, error)

创建支持TLS加密通信的TCP链接

func (*Conn) Close Uses

func (c *Conn) Close() error

关闭连接

func (*Conn) LocalAddr Uses

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

func (*Conn) Recv Uses

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

阻塞等待获取指定读取的数据长度,并给定重试策略。

需要注意: 1、往往在socket通信中需要指定固定的数据结构,并在设定对应的长度字段,并在读取数据时便于区分包大小; 2、当length < 0时表示获取缓冲区所有的数据,但是可能会引起包解析问题(可能出现粘包/断包情况),因此需要解析端注意解析策略; 3、当length = 0时表示获取一次缓冲区的数据后立即返回;

func (*Conn) RecvLine Uses

func (c *Conn) RecvLine(retry ...Retry) ([]byte, error)

按行读取数据,阻塞读取,直到完成一行读取位置(末尾以'\n'结尾,返回数据不包含换行符)

func (*Conn) RecvPkg Uses

func (c *Conn) RecvPkg(option ...PkgOption) (result []byte, err error)

简单协议: 获取一个数据包。

func (*Conn) RecvPkgWithTimeout Uses

func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) ([]byte, error)

简单协议: 带超时时间的消息包获取

func (*Conn) RecvWithTimeout Uses

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

带超时时间的数据获取

func (*Conn) RemoteAddr Uses

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

func (*Conn) Send Uses

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

发送数据

func (*Conn) SendPkg Uses

func (c *Conn) SendPkg(data []byte, option ...PkgOption) error

根据简单协议发送数据包。

简单协议数据格式:数据长度(24bit)|数据字段(变长)。

注意: 1. "数据长度"仅为"数据字段"的长度,不包含头信息的长度字段3字节。 2. 由于"数据长度"为3字节,并且使用的BigEndian字节序,因此这里最后返回的buffer使用了buffer[1:]。

func (*Conn) SendPkgWithTimeout Uses

func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) error

简单协议: 带超时时间的数据发送

func (*Conn) SendRecv Uses

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

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

func (*Conn) SendRecvPkg Uses

func (c *Conn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error)

简单协议: 发送数据并等待接收返回数据

func (*Conn) SendRecvPkgWithTimeout Uses

func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)

简单协议: 发送数据并等待接收返回数据(带返回超时等待时间)

func (*Conn) SendRecvWithTimeout Uses

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

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

func (*Conn) SendWithTimeout Uses

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

带超时时间的数据发送

func (*Conn) SetDeadline Uses

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

func (*Conn) SetRecvBufferWait Uses

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

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

func (*Conn) SetRecvDeadline Uses

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

func (*Conn) SetSendDeadline Uses

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

type PkgOption Uses

type PkgOption struct {
    MaxSize int   // (byte)数据读取的最大包大小,最大不能超过3字节(0xFFFFFF,15MB),默认为65535byte
    Retry   Retry // 失败重试
}

数据读取选项

type PoolConn Uses

type PoolConn struct {
    *Conn // 继承底层链接接口对象
    // contains filtered or unexported fields
}

链接池链接对象

func NewPoolConn Uses

func NewPoolConn(addr string, timeout ...int) (*PoolConn, error)

创建TCP链接池对象

func (*PoolConn) Close Uses

func (c *PoolConn) Close() error

(方法覆盖)覆盖底层接口对象的Close方法

func (*PoolConn) Recv Uses

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

(方法覆盖)接收数据

func (*PoolConn) RecvLine Uses

func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error)

(方法覆盖)按行读取数据,阻塞读取,直到完成一行读取位置(末尾以'\n'结尾,返回数据不包含换行符)

func (*PoolConn) RecvPkg Uses

func (c *PoolConn) RecvPkg(option ...PkgOption) ([]byte, error)

简单协议: (方法覆盖)接收数据

func (*PoolConn) RecvPkgWithTimeout Uses

func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) ([]byte, error)

简单协议: (方法覆盖)带超时时间的数据获取

func (*PoolConn) RecvWithTimeout Uses

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

(方法覆盖)带超时时间的数据获取

func (*PoolConn) Send Uses

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

(方法覆盖)发送数据

func (*PoolConn) SendPkg Uses

func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error)

简单协议: (方法覆盖)发送数据

func (*PoolConn) SendPkgWithTimeout Uses

func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) error

简单协议: (方法覆盖)带超时时间的数据发送

func (*PoolConn) SendRecv Uses

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

(方法覆盖)发送数据并等待接收返回数据

func (*PoolConn) SendRecvPkg Uses

func (c *PoolConn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error)

简单协议: (方法覆盖)发送数据并等待接收返回数据

func (*PoolConn) SendRecvPkgWithTimeout Uses

func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)

简单协议: (方法覆盖)发送数据并等待接收返回数据(带返回超时等待时间)

func (*PoolConn) SendRecvWithTimeout Uses

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

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

func (*PoolConn) SendWithTimeout Uses

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

(方法覆盖)带超时时间的数据发送

type Retry Uses

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

type Server Uses

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

TCP Server.

func GetServer Uses

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

GetServer returns the TCP server with specified <name>, or it returns a new normal TCP server named <name> if it does not exist. The parameter <name> is used to specify the TCP server

func NewServer Uses

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

NewServer creates and returns a new normal TCP server. The parameter <name> is optional, which is used to specify the instance name of the server.

func NewServerKeyCrt Uses

func NewServerKeyCrt(address, crtFile, keyFile string, handler func(*Conn), name ...string) *Server

NewServerKeyCrt creates and returns a new TCP server with TLS support. The parameter <name> is optional, which is used to specify the instance name of the server.

func NewServerTLS Uses

func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), name ...string) *Server

NewServerTLS creates and returns a new TCP server with TLS support. The parameter <name> is optional, which is used to specify the instance name of the server.

func (*Server) Close Uses

func (s *Server) Close() error

Close closes the listener and shutdowns the server.

func (*Server) Run Uses

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

Run starts running the TCP Server.

func (*Server) SetAddress Uses

func (s *Server) SetAddress(address string)

SetAddress sets the listening address for server.

func (*Server) SetHandler Uses

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

SetHandler sets the connection handler for server.

func (*Server) SetTLSConfig Uses

func (s *Server) SetTLSConfig(tlsConfig *tls.Config)

SetTlsConfig sets the TLS configuration of server.

func (*Server) SetTLSKeyCrt Uses

func (s *Server) SetTLSKeyCrt(crtFile, keyFile string) error

SetTlsKeyCrt sets the certificate and key file for TLS configuration of server.

Package gtcp imports 15 packages (graph). Updated 2019-06-19. Refresh now. Tools for package owners.