Documentation ¶
Overview ¶
Package gtcp provides TCP server and client implementations.
Index ¶
- func GetFreePort() (port int, err error)
- func GetFreePorts(count int) (ports []int, err error)
- func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error)
- func MustGetFreePort() int
- func NewNetConn(address string, timeout ...time.Duration) (net.Conn, error)
- func NewNetConnKeyCrt(addr, crtFile, keyFile string, timeout ...time.Duration) (net.Conn, error)
- func NewNetConnTLS(address string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error)
- func Send(address string, data []byte, retry ...Retry) error
- func SendPkg(address string, data []byte, option ...PkgOption) error
- func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error
- func SendRecv(address string, data []byte, length int, retry ...Retry) ([]byte, error)
- func SendRecvPkg(address string, data []byte, option ...PkgOption) ([]byte, error)
- func SendRecvPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
- func SendRecvWithTimeout(address string, data []byte, receive int, timeout time.Duration, ...) ([]byte, error)
- func SendWithTimeout(address string, data []byte, timeout time.Duration, retry ...Retry) error
- type Conn
- func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error)
- func (c *Conn) RecvLine(retry ...Retry) ([]byte, error)
- func (c *Conn) RecvPkg(option ...PkgOption) (result []byte, err error)
- func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error)
- func (c *Conn) RecvTill(til []byte, retry ...Retry) ([]byte, error)
- func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
- func (c *Conn) Send(data []byte, retry ...Retry) error
- func (c *Conn) SendPkg(data []byte, option ...PkgOption) error
- func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error)
- func (c *Conn) SendRecv(data []byte, length int, retry ...Retry) ([]byte, error)
- func (c *Conn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error)
- func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
- func (c *Conn) SendRecvWithTimeout(data []byte, length int, timeout time.Duration, retry ...Retry) ([]byte, error)
- func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error)
- func (c *Conn) SetDeadline(t time.Time) (err error)
- func (c *Conn) SetReceiveBufferWait(bufferWaitDuration time.Duration)
- func (c *Conn) SetReceiveDeadline(t time.Time) (err error)
- func (c *Conn) SetSendDeadline(t time.Time) (err error)
- type PkgOption
- 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(option ...PkgOption) ([]byte, error)
- func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error)
- func (c *PoolConn) RecvTill(til []byte, retry ...Retry) ([]byte, error)
- func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
- func (c *PoolConn) Send(data []byte, retry ...Retry) error
- func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error)
- func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error)
- func (c *PoolConn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
- func (c *PoolConn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error)
- func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]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) (err error)
- type Retry
- type Server
- func GetServer(name ...interface{}) *Server
- func NewServer(address string, handler func(*Conn), name ...string) *Server
- func NewServerKeyCrt(address, crtFile, keyFile string, handler func(*Conn), name ...string) (*Server, error)
- func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), name ...string) *Server
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFreePort ¶
GetFreePort retrieves and returns a port that is free.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/net/gtcp" ) func main() { fmt.Println(gtcp.GetFreePort()) // May Output: // 57429 <nil> }
Output:
func GetFreePorts ¶
GetFreePorts retrieves and returns specified number of ports that are free.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/net/gtcp" ) func main() { fmt.Println(gtcp.GetFreePorts(2)) // May Output: // [57743 57744] <nil> }
Output:
func LoadKeyCrt ¶
LoadKeyCrt creates and returns a TLS configuration object with given certificate and key files.
func MustGetFreePort ¶
func MustGetFreePort() int
MustGetFreePort performs as GetFreePort, but it panics is any error occurs.
func NewNetConn ¶
NewNetConn creates and returns a net.Conn with given address like "127.0.0.1:80". The optional parameter `timeout` specifies the timeout for dialing connection.
func NewNetConnKeyCrt ¶
NewNetConnKeyCrt creates and returns a TLS net.Conn with given TLS certificate and key files and address like "127.0.0.1:80". The optional parameter `timeout` specifies the timeout for dialing connection.
func NewNetConnTLS ¶
func NewNetConnTLS(address string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error)
NewNetConnTLS creates and returns a TLS net.Conn with given address like "127.0.0.1:80". The optional parameter `timeout` specifies the timeout for dialing connection.
func Send ¶
Send creates connection to `address`, writes `data` to the connection and then closes the connection. The optional parameter `retry` specifies the retry policy when fails in writing data.
func SendPkg ¶
SendPkg sends a package containing `data` to `address` and closes the connection. The optional parameter `option` specifies the package options for sending.
func SendPkgWithTimeout ¶
func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error
SendPkgWithTimeout sends a package containing `data` to `address` with timeout limitation and closes the connection. The optional parameter `option` specifies the package options for sending.
func SendRecv ¶
SendRecv creates connection to `address`, writes `data` to the connection, receives response and then closes the connection.
The parameter `length` specifies the bytes count waiting to receive. It receives all buffer content and returns if `length` is -1.
The optional parameter `retry` specifies the retry policy when fails in writing data.
func SendRecvPkg ¶
SendRecvPkg sends a package containing `data` to `address`, receives the response and closes the connection. The optional parameter `option` specifies the package options for sending.
func SendRecvPkgWithTimeout ¶
func SendRecvPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
SendRecvPkgWithTimeout sends a package containing `data` to `address`, receives the response with timeout limitation and closes the connection. The optional parameter `option` specifies the package options for sending.
Types ¶
type Conn ¶
type Conn struct { net.Conn // Underlying TCP connection object. // contains filtered or unexported fields }
Conn is the TCP connection object.
func NewConnByNetConn ¶
NewConnByNetConn creates and returns a TCP connection object with given net.Conn object.
func NewConnKeyCrt ¶
NewConnKeyCrt creates and returns a new TLS connection with given address and TLS certificate and key files.
func NewConnTLS ¶
NewConnTLS creates and returns a new TLS connection with given address and TLS configuration.
func (*Conn) Recv ¶
Recv receives and returns data from the connection.
Note that,
- If length = 0, which means it receives the data from current buffer and returns immediately.
- If length < 0, which means it receives all data from connection and returns it until no data from connection. Developers should notice the package parsing yourself if you decide receiving all data from buffer.
- If length > 0, which means it blocks reading data from connection until length size was received. It is the most commonly used length value for data receiving.
func (*Conn) RecvLine ¶
RecvLine reads data from the connection until reads char '\n'. Note that the returned result does not contain the last char '\n'.
func (*Conn) RecvPkgWithTimeout ¶
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) RecvTill ¶
RecvTill reads data from the connection until reads bytes `til`. Note that the returned result contains the last bytes `til`.
func (*Conn) RecvWithTimeout ¶
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
RecvWithTimeout reads data from the connection with timeout.
func (*Conn) SendPkg ¶
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. The integer bytes of the package are encoded using BigEndian order.
func (*Conn) SendPkgWithTimeout ¶
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) SendRecvPkg ¶
SendRecvPkg writes data to connection and blocks reading response using simple package protocol.
func (*Conn) SendRecvPkgWithTimeout ¶
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 ¶
func (c *Conn) SendRecvWithTimeout(data []byte, length int, timeout time.Duration, retry ...Retry) ([]byte, error)
SendRecvWithTimeout writes data to the connection and reads response with timeout.
func (*Conn) SendWithTimeout ¶
SendWithTimeout writes data to the connection with timeout.
func (*Conn) SetReceiveBufferWait ¶
SetReceiveBufferWait 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.
type PkgOption ¶
type PkgOption struct { // HeaderSize is used to mark the data length for next data receiving. // It's 2 bytes in default, 4 bytes max, which stands for the max data length // from 65535 to 4294967295 bytes. HeaderSize int // MaxDataSize is the data field size in bytes for data length validation. // If it's not manually set, it'll automatically be set correspondingly with the HeaderSize. MaxDataSize int // Retry policy when operation fails. Retry Retry }
PkgOption is package option for simple protocol.
type PoolConn ¶
type PoolConn struct { *Conn // Underlying connection object. // contains filtered or unexported fields }
PoolConn is a connection with pool feature for TCP. Note that it is NOT a pool or connection manager, it is just a TCP connection object.
func NewPoolConn ¶
NewPoolConn creates and returns a connection with pool feature.
func (*PoolConn) Close ¶
Close puts back the connection to the pool if it's active, or closes the connection if it's not active.
Note that, if `c` calls Close function closing itself, `c` can not be used again.
func (*PoolConn) RecvLine ¶
RecvLine reads data from the connection until reads char '\n'. Note that the returned result does not contain the last char '\n'.
func (*PoolConn) RecvPkg ¶
RecvPkg receives package from connection using simple package protocol. The optional parameter `option` specifies the package options for receiving.
func (*PoolConn) RecvPkgWithTimeout ¶
func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error)
RecvPkgWithTimeout reads data from connection with timeout using simple package protocol.
func (*PoolConn) RecvTill ¶
RecvTill reads data from the connection until reads bytes `til`. Note that the returned result contains the last bytes `til`.
func (*PoolConn) RecvWithTimeout ¶
func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
RecvWithTimeout reads data from the connection with timeout.
func (*PoolConn) Send ¶
Send writes data to the connection. It retrieves a new connection from its pool if it fails writing data.
func (*PoolConn) SendPkg ¶
SendPkg sends a package containing `data` to the connection. The optional parameter `option` specifies the package options for sending.
func (*PoolConn) SendPkgWithTimeout ¶
func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error)
SendPkgWithTimeout writes data to connection with timeout using simple package protocol.
func (*PoolConn) SendRecvPkg ¶
SendRecvPkg writes data to connection and blocks reading response using simple package protocol.
func (*PoolConn) SendRecvPkgWithTimeout ¶
func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
SendRecvPkgWithTimeout reads data from connection with timeout using simple package protocol.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a 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 ¶
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, error)
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) SetAddress ¶
SetAddress sets the listening address for server.
func (*Server) SetHandler ¶
SetHandler sets the connection handler for server.
func (*Server) SetTLSConfig ¶
SetTLSConfig sets the TLS configuration of server.
func (*Server) SetTLSKeyCrt ¶
SetTLSKeyCrt sets the certificate and key file for TLS configuration of server.