tcp

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UNKNOWN = iota
	ACTIVE
	ERROR
)

Variables

View Source
var (
	// DefaultWaitTimeout 默认等待超时时间
	DefaultWaitTimeout = time.Millisecond

	// DefaultConnTimeout 默认连接超时时间
	DefaultConnTimeout = 30 * time.Second

	// DefaultRetryInterval 默认重试间隔
	DefaultRetryInterval = 100 * time.Millisecond

	// DefaultReadBuffer 默认读取buffer大小
	DefaultReadBuffer = 1 << 12

	// DefaultServer 默认的服务名称
	DefaultServer = "Default"
)

Functions

func RecoveryBuffer

func RecoveryBuffer(data *[]byte)

RecoveryBuffer 用于回收已经不使用的 *[]byte 如果使用已经回收的资源,可能会造成panic,请注意

func Send

func Send(addr string, data []byte, retry *Retry) error

Send 拨号并发送消息,关闭链接

func SendRecv

func SendRecv(addr string, data []byte, length int, retry *Retry) ([]byte, error)

SendRecv 拨号发送并读取响应

func SendRecvWithTimeout

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

SendRecvWithTimeout 创建链接发送读取有超时限制的连接

func SendWithTimeout

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

SendWithTimeout 创建并发送具有写入超时限制的连接

Types

type Conn

type Conn struct {
	// net.conn: 原始的conn对象
	net.Conn
	// contains filtered or unexported fields
}

Conn 封装原始 net.conn 对象

func NewConn

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

NewConn 通过原始拨号建立

func NewConnByNetConn

func NewConnByNetConn(conn net.Conn) *Conn

NewConnByNetConn 通过原始的 net.Conn 链接建立 Conn 封装对象

func NewConnTLS

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

NewConnTLS 通过tls建立

func (*Conn) Recv

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

Recv 接受数据 length == 0 从 Conn一次读取立即返回 length < 0 从 Conn 接收所有数据,并将其返回,直到没有数据 length > 0 从 Conn 接收到对应的数据返回

func (*Conn) RecvLine

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

RecvLine 读取一行 '\n'

func (*Conn) RecvWithTimeout

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

RecvWithTimeout 读取已经超时的链接

func (*Conn) Send

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

Send 发送数据至对端,有重试机制

func (*Conn) SendRecv

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

SendRecv 写入数据并读取返回

func (*Conn) SendRecvWithTimeout

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

SendRecvWithTimeout 将数据写入并读出已经超时的链接

func (*Conn) SendWithTimeout

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

SendWithTimeout 写入数据给已经超时的链接

func (*Conn) SetDeadline

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

func (*Conn) SetRecvBufferInterval

func (c *Conn) SetRecvBufferInterval(t time.Duration)

SetRecvBufferInterval 读取缓存间隔时间

func (*Conn) SetRecvDeadline

func (c *Conn) SetRecvDeadline(t time.Time) error

func (*Conn) SetSendDeadline

func (c *Conn) SetSendDeadline(t time.Time) error

type Retry

type Retry struct {
	// Count: 重试次数,每重试一次就会 -1, 如果==0默认不重试
	Count uint

	// Interval: 重试间隔
	Interval time.Duration
}

Retry 重试配置

Jump to

Keyboard shortcuts

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