gf: github.com/gogf/gf/net/gtcp Index | Files

package gtcp

import "github.com/gogf/gf/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 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 {
    net.Conn // Underlying TCP connection object.
    // contains filtered or unexported fields
}

TCP connection object.

func NewConn Uses

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

NewConn creates and returns a new connection with given address.

func NewConnByNetConn Uses

func NewConnByNetConn(conn net.Conn) *Conn

NewConnByNetConn creates and returns a TCP connection object with given net.Conn object.

func NewConnKeyCrt Uses

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

NewConnKeyCrt creates and returns a new TLS connection with given address and TLS certificate and key files.

func NewConnTLS Uses

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

NewConnTLS creates and returns a new TLS connection with given address and TLS configuration.

func (*Conn) Recv Uses

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

Recv receives data from remote address.

Note that, 1. If length = 0, it means it receives the data from current buffer and returns immediately. 2. If length < 0, it means it receives all data from buffer and returns if it waits til no data from connection.

Developers should notice the package parsing yourself if you decide receiving all data from buffer.

3. If length > 0, it means it blocks reading data from connection until length size was received.

func (*Conn) RecvLine Uses

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

RecvLine reads data from connection until reads char '\n'. Note that the returned result does not contain char '\n'.

func (*Conn) RecvPkg Uses

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

Recv receives data from connection using simple package protocol.

func (*Conn) RecvPkgWithTimeout Uses

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

RecvPkgWithTimeout reads data from connection with timeout using simple package protocol.

func (*Conn) RecvWithTimeout Uses

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

RecvWithTimeout reads data from connection with timeout.

func (*Conn) Send Uses

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

Send writes data to remote address.

func (*Conn) SendPkg Uses

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

SendPkg send data using simple package protocol.

Simple package protocol: DataLength(24bit)|DataField(variant)。

Note that, 1. The DataLength is the length of DataField, which does not contain the header size 2 bytes. 2. The integer bytes of the package are encoded using BigEndian order.

func (*Conn) SendPkgWithTimeout Uses

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

SendPkgWithTimeout writes data to connection with timeout using simple package protocol.

func (*Conn) SendRecv Uses

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

SendRecv writes data to connection and blocks reading response.

func (*Conn) SendRecvPkg Uses

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

SendRecvPkg writes data to connection and blocks reading response using simple package protocol.

func (*Conn) SendRecvPkgWithTimeout Uses

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

SendRecvPkgWithTimeout writes data to connection and reads response with timeout using simple package protocol.

func (*Conn) SendRecvWithTimeout Uses

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 Uses

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

SendWithTimeout writes data to connection with timeout.

func (*Conn) SetDeadline Uses

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

func (*Conn) SetRecvBufferWait Uses

func (c *Conn) SetRecvBufferWait(bufferWaitDuration 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 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 {
    HeaderSize  int   // It's 2 bytes in default, max is 4 bytes.
    MaxDataSize int   // (Byte)data field size, it's 2 bytes in default, which means 65535 bytes.
    Retry       Retry // Retry policy.
}

Package option for simple protocol.

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) (data []byte, err 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) (err 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) (err 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) and is imported by 2 packages. Updated 2019-10-12. Refresh now. Tools for package owners.