Documentation
¶
Index ¶
- Constants
- func GetFreePort() (port int, err error)
- func GetFreePorts(count int) (ports []int, err error)
- func MustGetFreePort() int
- func NewNetConn(address 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 Retry
- type Server
- func (s *Server) Close(ctx context.Context) error
- func (s *Server) GetAddress() string
- func (s *Server) GetListenedAddress() string
- func (s *Server) GetListenedPort() int
- func (s *Server) Run(ctx context.Context) (err error)
- func (s *Server) SetAddress(address string)
- func (s *Server) SetHandler(handler func(*Conn))
- func (s *Server) SetTLSConfig(tlsConfig *tls.Config)
Constants ¶
const (
// FreePortAddress 使用随机端口标记服务器监听。
FreePortAddress = ":0"
)
Variables ¶
This section is empty.
Functions ¶
func GetFreePorts ¶
GetFreePorts 检索并返回指定数量的空闲端口。
func NewNetConn ¶
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 SendPkgWithTimeout ¶
func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error
SendPkgWithTimeout 将包含 `data` 的包发送到具有超时限制的 `address` 并关闭连接。 可选参数 `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 将包含 `data` 的包发送到 `address` ,接收具有超时限制的响应并关闭连接。 可选参数 `option` 指定发送的包选项。
Types ¶
type Conn ¶
Conn TCP 连接对象。
func NewConnByNetConn ¶
NewConnByNetConn 使用指定的 net.Conn 对象创建并返回 TCP 连接对象。
func NewConnTLS ¶
NewConnTLS 创建并返回一个新的 TLS 连接 使用指定的地址和 TLS 配置。
func (*Conn) Recv ¶
Recv 从连接中接收和返回数据。
注意, 1. 如果length = 0,表示从当前缓冲区接收数据并立即返回。 2. 如果length < 0,表示从connection接收所有数据,直到没有数据才返回 从连接。 如果您决定从缓冲区接收所有数据,开发人员应该注意自己解析的包。 3. 如果length > 0,这意味着它阻止从连接中读取数据,直到收到长度大小。 它是数据接收最常用的长度值。
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 使用包协议发送数据。
包协议: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) 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 从连接读取所有数据时设置缓冲区等待超时。 等待时间不能太长,否则可能会延迟从远程地址接收数据。
type PkgOption ¶
type PkgOption struct {
// HeaderSize 用于标记下一次数据接收的数据长度。
// 默认为 2 字节,最大 4 字节,表示最大数据长度为 65535 到 4294967295 字节。
HeaderSize int
// MaxDataSize 是用于数据长度验证的数据字段大小(以字节为单位)。
// 如果不手动设置,会自动与HeaderSize对应设置。
MaxDataSize int
// Retry 操作失败时的策略。
Retry Retry
}
PkgOption 是协议的封装选项。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 定义 TCP 服务包装器。
func NewServerTLS ¶
NewServerTLS 新建 TCP TLS 服务器。
func (*Server) GetListenedAddress ¶
GetListenedAddress 获取当前服务器监听地址。
func (*Server) GetListenedPort ¶
GetListenedPort 获取当前服务器监听端口。
func (*Server) SetTLSConfig ¶
SetTLSConfig 设置 TLS 配置。