gtcp

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 14 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 SendReceive

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

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

参数 `length` 指定等待接收的字节数。 它接收所有缓冲区内容并在 `length` 为 -1 时返回。 可选参数 `retry` 指定写入数据失败时的重试策略。

func SendReceivePkg

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

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

func SendReceivePkgWithTimeout

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

SendReceivePkgWithTimeout 将包含 `data` 的包发送到 `address` ,接收具有超时限制的响应并关闭连接。 可选参数 `option` 指定发送的包选项。

func SendReceiveWithTimeout

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

SendReceiveWithTimeout 执行具有读取超时限制的 SendReceive 逻辑。

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

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

Receive 从连接中接收和返回数据。

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

func (*Conn) ReceiveLine

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

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

func (*Conn) ReceivePkg

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

ReceivePkg 使用包协议从连接接收数据。

func (*Conn) ReceivePkgWithTimeout

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

ReceivePkgWithTimeout 使用包协议从超时连接中读取数据。

func (*Conn) ReceiveTill

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

ReceiveTill 从连接中读取数据,直到读取字节`til`。 请注意,返回的结果包含最后一个字节`til`。

func (*Conn) ReceiveWithTimeout

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

ReceiveWithTimeout 从超时的连接中读取数据。

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

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

SendReceive 将数据写入连接并阻止读取响应。

func (*Conn) SendReceivePkg

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

SendReceivePkg 使用包协议将数据写入连接并阻止读取响应。

func (*Conn) SendReceivePkgWithTimeout

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

SendReceivePkgWithTimeout 使用包协议将数据写入连接并读取超时响应。

func (*Conn) SendReceiveWithTimeout

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

SendReceiveWithTimeout 将数据写入连接并读取超时响应。

func (*Conn) SendWithTimeout

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

SendWithTimeout 将数据写入超时的连接。

func (*Conn) SetDeadline

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

func (*Conn) SetReceiveBufferWait

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

SetReceiveBufferWait 从连接读取所有数据时设置缓冲区等待超时。 等待时间不能太长,否则可能会延迟从远程地址接收数据。

func (*Conn) SetReceiveDeadline

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

func (*Conn) SetSendDeadline

func (c *Conn) SetSendDeadline(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 added in v1.2.5

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

NewServerTLS 新建 TCP TLS 服务器。

func (*Server) Close added in v1.2.0

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

Close 关闭 TCP 服务器。

func (*Server) GetListenedAddress added in v1.2.3

func (s *Server) GetListenedAddress() string

GetListenedAddress 获取当前服务器监听地址。

func (*Server) GetListenedPort added in v1.2.3

func (s *Server) GetListenedPort() int

GetListenedPort 获取当前服务器监听端口。

func (*Server) Run

func (s *Server) Run(ctx context.Context) (err error)

Run 启动 TCP 服务器。

Jump to

Keyboard shortcuts

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