connection

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnectionPanic = errors.New("connection: using in a wrong way")
	ErrClosedAlready   = errors.New("connection: connection closed already")
	ErrWriteChanFull   = errors.New("connection: write channel full")
	ErrRAddrIsNil      = errors.New("connection: remote addr is nil")
	ErrUdpUnsupported  = errors.New("connection: udp unsupported")
)

Functions

func ListenUdpWithAddr

func ListenUdpWithAddr(addr string) (*net.UDPConn, error)

func TcpConnectTimeout

func TcpConnectTimeout(lAddr, rAddr string, timeout time.Duration) (net.Conn, error)

Types

type IConnection

type IConnection interface {
	Read(b []byte) (n int, rAddr net.Addr, err error)

	// ReadAtLeast tcp支持
	ReadAtLeast(buf []byte, min int) (n int, err error)

	// ReadLine tcp支持
	ReadLine() (line []byte, isPrefix bool, err error)

	Write(b []byte) (n int, err error)

	WriteByAddr(b []byte, rAddr net.Addr) (n int, err error)

	Close()

	LocalAddr() net.Addr

	Flush() error

	// Done
	/******************************************************
	 * @Description: 阻塞直到连接关闭或发生错误
	 * @return <-chan  返回nil则是本端主动调用Close关闭
	 ******************************************************/
	Done() <-chan error

	GetStat() Stat
}

type ModOption

type ModOption func(option *Option)

type Option

type Option struct {
	//tcp有效 如果不为0,则之后每次读/写使用bufio的缓冲
	ReadBufSize  int
	WriteBufSize int

	// 如果不为0,则之后每次读/写都带超时
	ReadTimeoutMs  int
	WriteTimeoutMs int

	// 如果不为0,则写使用channel将数据发送到后台协程中发送
	WriteChanSize int

	// 系统socket缓存大小
	SocketReadBufSize  int
	SocketWriteBufSize int

	// 使用channel发送数据时,channel满了时Write函数的行为
	// WriteChanFullBehaviorReturnError 返回错误
	// WriteChanFullBehaviorBlock 阻塞直到向channel写入成功
	WriteChanFullBehavior WriteChanFullBehavior

	// LocalAddr
	LocalAddr string

	// RemoteAddr
	RemoteAddr string

	TcpConnectTimeout time.Duration
}

type Stat

type Stat struct {
	ReadBytesSum  uint64
	WroteBytesSum uint64
}

type StatAtomic

type StatAtomic struct {
	ReadBytesSum  atomicUtils.Uint64
	WroteBytesSum atomicUtils.Uint64
}

type TcpClientConnection

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

func NewTcpClientConnection

func NewTcpClientConnection(modOptions ...ModOption) (*TcpClientConnection, error)

func NewTcpServerConnection

func NewTcpServerConnection(conn net.Conn, modOptions ...ModOption) (*TcpClientConnection, error)

func (*TcpClientConnection) Close

func (c *TcpClientConnection) Close()

func (*TcpClientConnection) Done

func (c *TcpClientConnection) Done() <-chan error

func (*TcpClientConnection) Flush

func (c *TcpClientConnection) Flush() error

func (*TcpClientConnection) GetStat

func (c *TcpClientConnection) GetStat() (s Stat)

func (*TcpClientConnection) LocalAddr

func (c *TcpClientConnection) LocalAddr() net.Addr

func (*TcpClientConnection) Read

func (c *TcpClientConnection) Read(b []byte) (n int, rAddr net.Addr, err error)

func (*TcpClientConnection) ReadAtLeast

func (c *TcpClientConnection) ReadAtLeast(buf []byte, min int) (n int, err error)

func (*TcpClientConnection) ReadLine

func (c *TcpClientConnection) ReadLine() (line []byte, isPrefix bool, err error)

func (*TcpClientConnection) Write

func (c *TcpClientConnection) Write(b []byte) (n int, err error)

func (*TcpClientConnection) WriteByAddr

func (c *TcpClientConnection) WriteByAddr(b []byte, rAddr net.Addr) (n int, err error)

type TcpConnectHandle

type TcpConnectHandle func(ctx context.Context, conn *TcpClientConnection)

type TcpServer

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

func NewTcpServer

func NewTcpServer(handle TcpConnectHandle, modOptions ...ModOption) *TcpServer

func (*TcpServer) Close

func (s *TcpServer) Close()

func (*TcpServer) RunLoop

func (s *TcpServer) RunLoop() error

type UdpConnection

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

func NewUdpConnection

func NewUdpConnection(modOptions ...ModOption) (*UdpConnection, error)

func (*UdpConnection) Close

func (c *UdpConnection) Close()

func (*UdpConnection) Done

func (c *UdpConnection) Done() <-chan error

func (*UdpConnection) Flush

func (c *UdpConnection) Flush() error

func (*UdpConnection) GetStat

func (c *UdpConnection) GetStat() (s Stat)

func (*UdpConnection) LocalAddr

func (c *UdpConnection) LocalAddr() net.Addr

func (*UdpConnection) Read

func (c *UdpConnection) Read(b []byte) (n int, rAddr net.Addr, err error)

func (*UdpConnection) ReadAtLeast

func (c *UdpConnection) ReadAtLeast(buf []byte, min int) (n int, err error)

func (*UdpConnection) ReadLine

func (c *UdpConnection) ReadLine() (line []byte, isPrefix bool, err error)

func (*UdpConnection) Write

func (c *UdpConnection) Write(b []byte) (n int, err error)

func (*UdpConnection) WriteByAddr

func (c *UdpConnection) WriteByAddr(b []byte, rAddr net.Addr) (n int, err error)

type WriteChanFullBehavior

type WriteChanFullBehavior int
const (
	WriteChanFullBehaviorReturnError WriteChanFullBehavior = iota + 1
	WriteChanFullBehaviorBlock
)

Jump to

Keyboard shortcuts

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