gtcp

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 13 Imported by: 1

README

TCP

gnet/gtcp 参考 GF 框架实现了 TCP 网络服务器。

Documentation

Index

Constants

View Source
const (
	// FreePortAddress 使用随机端口标记服务器监听。
	FreePortAddress = ":0"
)

Variables

This section is empty.

Functions

func GetFreePort

func GetFreePort() (port int, err error)

GetFreePort 检索并返回一个空闲的端口。

func GetFreePorts

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

GetFreePorts 检索并返回指定数量的空闲端口。

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 NewNetConnTLS

func NewNetConnTLS(address string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error)

NewNetConnTLS 创建并返回具有指定地址的 TLS net.Conn,例如“127.0.0.1:80”。 可选参数`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 将包含 `data` 的包发送到具有超时限制的 `address` 并关闭连接。 可选参数 `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 将包含 `data` 的包发送到 `address` ,接收具有超时限制的响应并关闭连接。 可选参数 `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 发送具有写入超时限制的逻辑。

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 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,表示从connection接收所有数据,直到没有数据才返回 从连接。 如果您决定从缓冲区接收所有数据,开发人员应该注意自己解析的包。 3. 如果length > 0,这意味着它阻止从连接中读取数据,直到收到长度大小。 它是数据接收最常用的长度值。

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 使用包协议发送数据。

包协议:DataLength(24bit)|DataField(variant)。

注意, 1. DataLength是DataField的长度,不包含header的大小。 2. 包的整数字节使用 BigEndian 顺序编码。

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)

func (*Conn) SetDeadlineRecv

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

func (*Conn) SetDeadlineSend

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

type PkgOption

type PkgOption struct {
	// HeaderSize 用于标记下一次数据接收的数据长度。
	// 默认为 2 字节,最大 4 字节,表示最大数据长度为 65535 到 4294967295 字节。
	HeaderSize int

	// MaxDataSize 是用于数据长度验证的数据字段大小(以字节为单位)。
	// 如果不手动设置,会自动与HeaderSize对应设置。
	MaxDataSize int

	// Retry 操作失败时的策略。
	Retry Retry
}

PkgOption 是协议的封装选项。

type Retry

type Retry struct {
	Count    int           // 重试计数。
	Interval time.Duration // 重试间隔。
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 定义 TCP 服务包装器。

func NewServer

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

NewServer 新建 TCP 服务器。

func NewServerTLS

func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn)) *Server

NewServerTLS 新建 TCP TLS 服务器。

func (*Server) Close

func (s *Server) Close(ctx context.Context) error

Close 关闭 TCP 服务器。

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(ctx context.Context) (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 配置。

Jump to

Keyboard shortcuts

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