gtcp

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: 16 Imported by: 0

Documentation

Overview

Package gtcp provides TCP server and client implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Checksum

func Checksum(buffer []byte) uint32

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

func LoadKeyCrt

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

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

func NewNetConn

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

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

func NewNetConnKeyCrt

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

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

func NewNetConnTLS

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

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

func Send

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

(面向短链接)发送数据

func SendPkg

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

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

func SendPkgWithTimeout

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

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

func SendRecv

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

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

func SendRecvPkg

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

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

func SendRecvPkgWithTimeout

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

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

func SendRecvWithTimeout

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

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

func SendWithTimeout

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

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

Types

type Conn

type Conn struct {
	net.Conn // 底层tcp对象
	// contains filtered or unexported fields
}

封装的链接对象

func NewConn

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

创建TCP链接

func NewConnByNetConn

func NewConnByNetConn(conn net.Conn) *Conn

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

func NewConnKeyCrt

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

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

func NewConnTLS

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

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

func (*Conn) Recv

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

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

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

func (*Conn) RecvLine

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

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

func (*Conn) RecvPkg

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

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

func (*Conn) RecvPkgWithTimeout

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

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

func (*Conn) RecvWithTimeout

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

带超时时间的数据获取

func (*Conn) Send

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

发送数据

func (*Conn) SendPkg

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

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

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

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

func (*Conn) SendPkgWithTimeout

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

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

func (*Conn) SendRecv

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

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

func (*Conn) SendRecvPkg

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

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

func (*Conn) SendRecvPkgWithTimeout

func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]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(bufferWaitDuration 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 PkgOption

type PkgOption struct {
	HeaderSize  int   // 自定义头大小(默认为2字节,最大不能超过4字节)
	MaxDataSize int   // (byte)数据读取的最大包大小,默认最大不能超过2字节(65535 byte)
	Retry       Retry // 失败重试
}

数据读取选项

type PoolConn

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

链接池链接对象

func NewPoolConn

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

创建TCP链接池对象

func (*PoolConn) Close

func (c *PoolConn) Close() error

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

func (*PoolConn) Recv

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

(方法覆盖)接收数据

func (*PoolConn) RecvLine

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

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

func (*PoolConn) RecvPkg

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

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

func (*PoolConn) RecvPkgWithTimeout

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

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

func (*PoolConn) RecvWithTimeout

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

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

func (*PoolConn) Send

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

(方法覆盖)发送数据

func (*PoolConn) SendPkg

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

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

func (*PoolConn) SendPkgWithTimeout

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

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

func (*PoolConn) SendRecv

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

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

func (*PoolConn) SendRecvPkg

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

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

func (*PoolConn) SendRecvPkgWithTimeout

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

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

func (*PoolConn) SendRecvWithTimeout

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

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

func (*PoolConn) SendWithTimeout

func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err 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

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

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

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

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

func (s *Server) Close() error

Close closes the listener and shutdowns the server.

func (*Server) Run

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

Run starts running the TCP Server.

func (*Server) SetAddress

func (s *Server) SetAddress(address string)

SetAddress sets the listening address for server.

func (*Server) SetHandler

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

SetHandler sets the connection handler for server.

func (*Server) SetTLSConfig

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

SetTlsConfig sets the TLS configuration of server.

func (*Server) SetTLSKeyCrt

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

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

Jump to

Keyboard shortcuts

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