Documentation
¶
Overview ¶
Package netUtils
对 net.Conn 接口的二次封装,目的有两个: 1. 在流媒体传输这种特定的长连接场景下提供更方便、高性能的接口 2. 便于后续将TCPConn替换成其他传输协议
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection interface {
// Read ...
Read(b []byte) (n int, err error)
// Write
/******************************************************
* @Description: 如果设置了 Option.WriteChanSize 做异步发送,那么`n`恒等于len(`b`)
* @param b
* @return n 发送成功的大小
* @return err
******************************************************/
Write(b []byte) (n int, err error)
// Close
/******************************************************
* @Description: 允许调用多次
* @return error
******************************************************/
Close() error
LocalAddr() net.Addr
RemoteAddr() net.Addr
SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
// Writev
/******************************************************
* @Description: 发送多块不连续的内存块时使用, 如果需要发送的是一块连续的内存块,建议使用 Write 发送
* @param b
* @return n
* @return err
******************************************************/
Writev(b net.Buffers) (n int, err error)
ReadAtLeast(buf []byte, min int) (n int, err error)
ReadLine() (line []byte, isPrefix bool, err error) // 只有设置了ReadBufSize才可以使用这个方法
// Flush
/******************************************************
* @Description: 如果使用了bufio写缓冲,则将缓冲中的数据发送出去,
如果使用了channel异步发送,则阻塞等待,直到之前channel中的数据全部发送完毕
一般在Close前,想要将剩余数据发送完毕时调用
* @return error
******************************************************/
Flush() error
// Done
/******************************************************
* @Description: 阻塞直到连接关闭或发生错误
* @return <-chan 返回nil则是本端主动调用Close关闭
******************************************************/
Done() <-chan error
ModWriteChanSize(n int)
ModWriteBufSize(n int)
ModReadTimeoutMs(n int)
ModWriteTimeoutMs(n int)
// GetStat
/******************************************************
* @Description: 连接上读取和发送的字节总数,
如果是异步发送,发送字节统计的是调用底层write的值,而非上层调用Connection发送的值,
也即不包含Connection中的发送缓存部分,但是可能包含内核socket发送缓冲区的值
* @return Stat
******************************************************/
GetStat() Stat
}
Connection *****************************************************
- @Description: 如果没有特别说明,函数的语义和 net.Conn 相同 *****************************************************
type Option ¶
type Option struct {
// 如果不为0,则之后每次读/写使用bufio的缓冲
ReadBufSize int
WriteBufSize int
// 如果不为0,则之后每次读/写都带超时
ReadTimeoutMs int
WriteTimeoutMs int
// 如果不为0,则写使用channel将数据发送到后台协程中发送
WriteChanSize int
// 使用channel发送数据时,channel满了时Write函数的行为
// WriteChanFullBehaviorReturnError 返回错误
// WriteChanFullBehaviorBlock 阻塞直到向channel写入成功
WriteChanFullBehavior WriteChanFullBehavior
}
type StatAtomic ¶
type StatAtomic struct {
ReadBytesSum atomicUtils.Uint64
WroteBytesSum atomicUtils.Uint64
}
type WriteChanFullBehavior ¶
type WriteChanFullBehavior int
const ( WriteChanFullBehaviorReturnError WriteChanFullBehavior = iota + 1 WriteChanFullBehaviorBlock )
Click to show internal directories.
Click to hide internal directories.