Documentation ¶
Overview ¶
Package gtcp 提供了 TCP 服务器和客户端的实现。
Index ¶
- Constants
- 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) SetBufferWaitRecv(bufferWaitDuration time.Duration)
- func (c *Conn) SetDeadline(t time.Time) (err error)
- func (c *Conn) SetDeadlineRecv(t time.Time) (err error)
- func (c *Conn) SetDeadlineSend(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
- func (s *Server) Close() error
- func (s *Server) GetAddress() string
- func (s *Server) GetListenedAddress() string
- func (s *Server) GetListenedPort() int
- func (s *Server) Run() (err error)
- func (s *Server) SetAddress(address string)
- func (s *Server) SetHandler(handler func(*Conn))
- func (s *Server) SetTLSConfig(tlsConfig *tls.Config)
- func (s *Server) SetTLSKeyCrt(crtFile, keyFile string) error
Examples ¶
Constants ¶
const (
// FreePortAddress 表示服务器使用随机空闲端口进行监听。
FreePortAddress = ":0"
)
Variables ¶
This section is empty.
Functions ¶
func GetFreePort ¶
GetFreePort 获取并返回一个可用的端口。
Example ¶
fmt.Println(tcp类.GetFreePort()) // May Output: // 57429 <nil>
Output:
func GetFreePorts ¶
GetFreePorts 获取并返回指定数量的空闲端口。
Example ¶
fmt.Println(tcp类.GetFreePorts(2)) // May Output: // [57743 57744] <nil>
Output:
func LoadKeyCrt ¶
LoadKeyCrt 通过给定的证书和密钥文件创建并返回一个 TLS 配置对象。
func MustGetFreePort ¶
func MustGetFreePort() int
MustGetFreePort 的行为与 GetFreePort 相同,但是,如果发生任何错误,它会触发 panic。
func NewNetConn ¶
NewNetConn 创建并返回一个 net.Conn,其地址格式如 "127.0.0.1:80"。 可选参数 `timeout` 指定了拨号连接的超时时间。
func NewNetConnKeyCrt ¶
NewNetConnKeyCrt 根据给定的 TLS 证书和密钥文件以及类似 "127.0.0.1:80" 的地址创建并返回一个 TLS 网络连接(net.Conn)。可选参数 `timeout` 指定了建立连接时的超时时间。
func NewNetConnTLS ¶
func NewNetConnTLS(address string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error)
NewNetConnTLS 根据给定的地址(如 "127.0.0.1:80")创建并返回一个 TLS 安全连接 net.Conn。 可选参数 `timeout` 指定了建立连接时的超时时间。
func SendPkgWithTimeout ¶
func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error
SendPkgWithTimeout 函数向 `address` 发送包含 `data` 的数据包,并设置超时限制,同时关闭连接。可选参数 `option` 指定了发送数据包的相关选项。
func SendRecv ¶
SendRecv 创建到 `address` 的连接,将 `data` 写入连接,接收响应,然后关闭连接。
参数 `length` 指定等待接收的字节数量。如果 `length` 为 -1,则接收所有缓冲区内容并返回。
可选参数 `retry` 指定了在写入数据失败时重试策略。
func SendRecvPkg ¶
SendRecvPkg 将包含 `data` 的数据包发送到 `address`,接收响应并关闭连接。可选参数 `option` 指定了发送数据包时的选项。
func SendRecvPkgWithTimeout ¶
func SendRecvPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
SendRecvPkgWithTimeout 函数向 `address` 发送包含 `data` 的数据包,在有限的超时时间内接收响应并关闭连接。 可选参数 `option` 用于指定发送数据包的相关选项。
Types ¶
type Conn ¶
Conn 是 TCP 连接对象。
func NewConnByNetConn ¶
NewConnByNetConn 根据给定的 net.Conn 对象创建并返回一个 TCP 连接对象。
func NewConnKeyCrt ¶
NewConnKeyCrt 根据给定的地址和 TLS 证书及密钥文件创建并返回一个新的 TLS 连接。
func NewConnTLS ¶
NewConnTLS 根据给定的地址和 TLS 配置创建并返回一个新的 TLS 连接。
func (*Conn) Recv ¶
Recv 从连接中接收并返回数据。
注意:
- 如果 length 等于 0,表示它会从当前缓冲区接收数据并立即返回。
- 如果 length 小于 0,表示它会从连接中接收所有数据并返回,直到没有更多的数据从连接中获取为止。开发者需要注意, 如果决定从缓冲区接收所有数据,则需要自行处理包解析问题。
- 如果 length 大于 0,表示它会阻塞读取连接中的数据,直到接收到长度为 length 的数据为止。这是最常见的用于接收数据的长度值。
func (*Conn) RecvPkgWithTimeout ¶
func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error)
RecvPkgWithTimeout 使用简单包协议,以超时方式从连接中读取数据。
func (*Conn) RecvWithTimeout ¶
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
RecvWithTimeout函数以超时方式从连接中读取数据。
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 使用简单的包协议并设置超时,向连接写入数据。
func (*Conn) SendRecvPkg ¶
SendRecvPkg 使用简单的包协议将数据写入连接,并阻塞等待读取响应。
func (*Conn) SendRecvPkgWithTimeout ¶
func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
SendRecvPkgWithTimeout 使用简单包协议,以超时机制向连接写入数据并读取响应。
func (*Conn) SendRecvWithTimeout ¶
func (c *Conn) SendRecvWithTimeout(data []byte, length int, timeout time.Duration, retry ...Retry) ([]byte, error)
SendRecvWithTimeout 函数向连接写入数据并设定超时读取响应。
func (*Conn) SendWithTimeout ¶
SendWithTimeout 函数在设定的超时时间内向连接写入数据。
func (*Conn) SetBufferWaitRecv ¶
SetBufferWaitRecv 设置从连接中读取所有数据时的缓冲等待超时时间。 等待时长不能过长,否则可能导致接收远程地址数据延迟。
func (*Conn) SetDeadline ¶
SetDeadline 设置当前连接的截止时间。
func (*Conn) SetDeadlineRecv ¶
SetDeadlineRecv为当前连接设置接收的截止时间。
type PkgOption ¶
type PkgOption struct { // HeaderSize 用于标记下一段数据的长度,用于接收数据时进行判断。 // 默认情况下占2字节,最大为4字节,可表示的数据长度范围从65535字节到4294967295字节。 HeaderSize int // MaxDataSize 是用于数据长度验证的数据字段大小,单位为字节。 // 若未手动设置,它将根据 HeaderSize 自动进行相应的设置。 MaxDataSize int // 当操作失败时的重试策略。 Retry Retry }
PkgOption 是用于简单协议的包选项。
type PoolConn ¶
type PoolConn struct { *Conn // 基础连接对象。 // contains filtered or unexported fields }
PoolConn 是具有连接池特性的 TCP 连接。 注意,它不是一个连接池或连接管理器,它仅仅是一个 TCP 连接对象。
func NewPoolConn ¶
NewPoolConn 创建并返回一个具有连接池功能的连接。
func (*PoolConn) Close ¶
Close函数会将活跃的连接归还给连接池,如果该连接处于非活跃状态,则关闭该连接。
注意,如果通过`c`调用Close函数来关闭自身,则此后不能再使用`c`。
func (*PoolConn) RecvPkgWithTimeout ¶
func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error)
RecvPkgWithTimeout 使用简单包协议,以超时方式从连接中读取数据。
func (*PoolConn) RecvWithTimeout ¶
func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error)
RecvWithTimeout函数以超时方式从连接中读取数据。
func (*PoolConn) SendPkgWithTimeout ¶
func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error)
SendPkgWithTimeout 使用简单的包协议并设置超时,向连接写入数据。
func (*PoolConn) SendRecvPkg ¶
SendRecvPkg 使用简单的包协议将数据写入连接,并阻塞等待读取响应。
func (*PoolConn) SendRecvPkgWithTimeout ¶
func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
SendRecvPkgWithTimeout 使用简单包协议并通过超时读取从连接中接收数据。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 是一个 TCP 服务器。
func GetServer ¶
func GetServer(name ...interface{}) *Server
GetServer 函数返回指定名称 `name` 的 TCP 服务器, 如果该服务器不存在,则返回一个新的普通 TCP 服务器并命名为 `name`。 参数 `name` 用于指定要获取的 TCP 服务器。
func NewServerKeyCrt ¶
func NewServerKeyCrt(address, crtFile, keyFile string, handler func(*Conn), name ...string) (*Server, error)
NewServerKeyCrt 创建并返回一个带有 TLS 支持的新 TCP 服务器。 参数 `name` 是可选的,用于指定服务器实例名称。
func NewServerTLS ¶
func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), name ...string) *Server
NewServerTLS 创建并返回一个带有 TLS 支持的新 TCP 服务器。 参数 `name` 是可选的,用于指定服务器实例的名称。
func (*Server) GetListenedAddress ¶
GetListenedAddress 获取并返回当前服务器监听的地址字符串。
func (*Server) GetListenedPort ¶
GetListenedPort 获取并返回当前服务器正在监听的一个端口。
func (*Server) SetHandler ¶
SetHandler 设置服务器的连接处理器。
func (*Server) SetTLSConfig ¶
SetTLSConfig 设置服务器的 TLS 配置。
func (*Server) SetTLSKeyCrt ¶
SetTLSKeyCrt 用于设置服务器TLS配置所需的证书和密钥文件。