tcp类

package
v0.0.0-...-2910145 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package gtcp 提供了 TCP 服务器和客户端的实现。

Index

Examples

Constants

View Source
const (
	// FreePortAddress 表示服务器使用随机空闲端口进行监听。
	FreePortAddress = ":0"
)

Variables

This section is empty.

Functions

func GetFreePort

func GetFreePort() (port int, err error)

GetFreePort 获取并返回一个可用的端口。

Example
fmt.Println(tcp类.GetFreePort())

// May Output:
// 57429 <nil>
Output:

func GetFreePorts

func GetFreePorts(count int) (ports []int, err error)

GetFreePorts 获取并返回指定数量的空闲端口。

Example
fmt.Println(tcp类.GetFreePorts(2))

// May Output:
// [57743 57744] <nil>
Output:

func LoadKeyCrt

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

LoadKeyCrt 通过给定的证书和密钥文件创建并返回一个 TLS 配置对象。

func MustGetFreePort

func MustGetFreePort() int

MustGetFreePort 的行为与 GetFreePort 相同,但是,如果发生任何错误,它会触发 panic。

func NewNetConn

func NewNetConn(address string, timeout ...time.Duration) (net.Conn, error)

NewNetConn 创建并返回一个 net.Conn,其地址格式如 "127.0.0.1:80"。 可选参数 `timeout` 指定了拨号连接的超时时间。

func NewNetConnKeyCrt

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

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 Send

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

Send 函数创建到 `address` 的连接,将 `data` 数据写入该连接,然后关闭连接。 可选参数 `retry` 指定了在写入数据失败时的重试策略。

func SendPkg

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

SendPkg 将包含 `data` 的数据包发送到 `address`,并关闭连接。 可选参数 `option` 指定了发送数据包时的选项。

func SendPkgWithTimeout

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

SendPkgWithTimeout 函数向 `address` 发送包含 `data` 的数据包,并设置超时限制,同时关闭连接。可选参数 `option` 指定了发送数据包的相关选项。

func SendRecv

func SendRecv(address string, data []byte, length int, retry ...Retry) ([]byte, error)

SendRecv 创建到 `address` 的连接,将 `data` 写入连接,接收响应,然后关闭连接。

参数 `length` 指定等待接收的字节数量。如果 `length` 为 -1,则接收所有缓冲区内容并返回。

可选参数 `retry` 指定了在写入数据失败时重试策略。

func SendRecvPkg

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

SendRecvPkg 将包含 `data` 的数据包发送到 `address`,接收响应并关闭连接。可选参数 `option` 指定了发送数据包时的选项。

func SendRecvPkgWithTimeout

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

SendRecvPkgWithTimeout 函数向 `address` 发送包含 `data` 的数据包,在有限的超时时间内接收响应并关闭连接。 可选参数 `option` 用于指定发送数据包的相关选项。

func SendRecvWithTimeout

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

SendRecvWithTimeout 在读取超时限制下执行SendRecv逻辑。

func SendWithTimeout

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

SendWithTimeout 在写入超时限制下执行Send逻辑。

Types

type Conn

type Conn struct {
	net.Conn // 底层TCP连接对象。
	// contains filtered or unexported fields
}

Conn 是 TCP 连接对象。

func NewConn

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

NewConn 根据给定的地址创建并返回一个新的连接。

func NewConnByNetConn

func NewConnByNetConn(conn net.Conn) *Conn

NewConnByNetConn 根据给定的 net.Conn 对象创建并返回一个 TCP 连接对象。

func NewConnKeyCrt

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

NewConnKeyCrt 根据给定的地址和 TLS 证书及密钥文件创建并返回一个新的 TLS 连接。

func NewConnTLS

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

NewConnTLS 根据给定的地址和 TLS 配置创建并返回一个新的 TLS 连接。

func (*Conn) Recv

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

Recv 从连接中接收并返回数据。

注意:

  1. 如果 length 等于 0,表示它会从当前缓冲区接收数据并立即返回。
  2. 如果 length 小于 0,表示它会从连接中接收所有数据并返回,直到没有更多的数据从连接中获取为止。开发者需要注意, 如果决定从缓冲区接收所有数据,则需要自行处理包解析问题。
  3. 如果 length 大于 0,表示它会阻塞读取连接中的数据,直到接收到长度为 length 的数据为止。这是最常见的用于接收数据的长度值。

func (*Conn) RecvLine

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

RecvLine 从连接中读取数据,直到读取到字符 '\n'。 注意,返回的结果不包含最后的字符 '\n'。

func (*Conn) RecvPkg

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

RecvPkg 通过简单的包协议从连接中接收数据。

func (*Conn) RecvPkgWithTimeout

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

RecvPkgWithTimeout 使用简单包协议,以超时方式从连接中读取数据。

func (*Conn) RecvTill

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

RecvTill从连接中读取数据,直到读取到指定字节序列`til`为止。 注意,返回的结果中包含最后的字节序列`til`。

func (*Conn) RecvWithTimeout

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

RecvWithTimeout函数以超时方式从连接中读取数据。

func (*Conn) Send

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

Send 将数据写入远程地址。

func (*Conn) SendPkg

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. 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) SendRecv

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

SendRecv 向连接写入数据,并阻塞等待读取响应。

func (*Conn) SendRecvPkg

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

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

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

SendWithTimeout 函数在设定的超时时间内向连接写入数据。

func (*Conn) SetBufferWaitRecv

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

SetBufferWaitRecv 设置从连接中读取所有数据时的缓冲等待超时时间。 等待时长不能过长,否则可能导致接收远程地址数据延迟。

func (*Conn) SetDeadline

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

SetDeadline 设置当前连接的截止时间。

func (*Conn) SetDeadlineRecv

func (c *Conn) SetDeadlineRecv(t time.Time) (err error)

SetDeadlineRecv为当前连接设置接收的截止时间。

func (*Conn) SetDeadlineSend

func (c *Conn) SetDeadlineSend(t time.Time) (err error)

SetDeadlineSend 为当前连接设置发送的截止时间。

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

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

NewPoolConn 创建并返回一个具有连接池功能的连接。

func (*PoolConn) Close

func (c *PoolConn) Close() error

Close函数会将活跃的连接归还给连接池,如果该连接处于非活跃状态,则关闭该连接。

注意,如果通过`c`调用Close函数来关闭自身,则此后不能再使用`c`。

func (*PoolConn) Recv

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

Recv 从连接中接收数据。

func (*PoolConn) RecvLine

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

RecvLine 从连接中读取数据,直到读取到字符 '\n'。 注意,返回的结果不包含最后的字符 '\n'。

func (*PoolConn) RecvPkg

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

RecvPkg 通过简单的包协议从连接中接收数据包。 可选参数 `option` 指定了接收数据包时的包选项。

func (*PoolConn) RecvPkgWithTimeout

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

RecvPkgWithTimeout 使用简单包协议,以超时方式从连接中读取数据。

func (*PoolConn) RecvTill

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

RecvTill从连接中读取数据,直到读取到指定字节序列`til`为止。 注意,返回的结果中包含最后的字节序列`til`。

func (*PoolConn) RecvWithTimeout

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

RecvWithTimeout函数以超时方式从连接中读取数据。

func (*PoolConn) Send

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

Send 将数据写入连接。如果写入数据失败,它将从其连接池中获取一个新的连接。

func (*PoolConn) SendPkg

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

SendPkg 将包含 `data` 的数据包发送到连接。 可选参数 `option` 指定发送数据包的选项。

func (*PoolConn) SendPkgWithTimeout

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

SendPkgWithTimeout 使用简单的包协议并设置超时,向连接写入数据。

func (*PoolConn) SendRecv

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

SendRecv 向连接写入数据,并阻塞等待读取响应。

func (*PoolConn) SendRecvPkg

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

SendRecvPkg 使用简单的包协议将数据写入连接,并阻塞等待读取响应。

func (*PoolConn) SendRecvPkgWithTimeout

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

SendRecvPkgWithTimeout 使用简单包协议并通过超时读取从连接中接收数据。

func (*PoolConn) SendRecvWithTimeout

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

SendRecvWithTimeout 函数向连接写入数据并设定超时读取响应。

func (*PoolConn) SendWithTimeout

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

SendWithTimeout 函数在设定的超时时间内向连接写入数据。

type Retry

type Retry struct {
	Count    int           // Retry count.
	Interval time.Duration // Retry interval.
}

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 NewServer

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

NewServer 创建并返回一个新的普通TCP服务器。 参数`name`是可选的,用于指定服务器实例的名称。

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) Close

func (s *Server) Close() error

Close 关闭监听器并关闭服务器。

func (*Server) GetAddress

func (s *Server) GetAddress() string

GetAddress 获取服务器的监听地址。

func (*Server) GetListenedAddress

func (s *Server) GetListenedAddress() string

GetListenedAddress 获取并返回当前服务器监听的地址字符串。

func (*Server) GetListenedPort

func (s *Server) GetListenedPort() int

GetListenedPort 获取并返回当前服务器正在监听的一个端口。

func (*Server) Run

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

Run 开始运行 TCP 服务器。

func (*Server) SetAddress

func (s *Server) SetAddress(address string)

SetAddress 设置服务器的监听地址。

func (*Server) SetHandler

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

SetHandler 设置服务器的连接处理器。

func (*Server) SetTLSConfig

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

SetTLSConfig 设置服务器的 TLS 配置。

func (*Server) SetTLSKeyCrt

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

SetTLSKeyCrt 用于设置服务器TLS配置所需的证书和密钥文件。

Jump to

Keyboard shortcuts

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