Documentation
¶
Index ¶
Constants ¶
View Source
const ( DefaultConnectWaitStart = time.Millisecond * 20 DefaultConnectWaitMut = 2 DefaultConnectMaxWait = time.Second * 2 )
View Source
const ( TCPMaxPackageSize = 10240 TimeoutTime int64 = 7000 HeartbeatTime int64 = 2000 )
View Source
const ( DefaultMaxConnNum int = 3000 DefaultKeepAlive = time.Second * 1 UpdateInterval = time.Millisecond * 10 )
Variables ¶
View Source
var ( // ErrInvalidAddress 不合法的地址输入 ErrInvalidAddress = errors.New("invalid address input") // ErrInvalidGetAgentFunc 不合法的新建 agent 函数 ErrInvalidGetAgentFunc = errors.New("invalid new agent function") // ErrInvalidCodec 不合法的 Codec ErrInvalidCodec = errors.New("codec invalid") // ErrInvalidAgent 不合法的 Agent ErrInvalidAgent = errors.New("agent invalid") // ErrTooLessLength 写入缓冲长度太低 ErrTooLessLength = errors.New("buffer too less length") // ErrTooMoreLength 写入缓冲长度太高 ErrTooMoreLength = errors.New("buffer too more length") // ErrPacketSplit 网络包传输不完全 ErrPacketSplit = errors.New("network packet split") )
View Source
var ( // DefaultClientOptions 默认 Client 选项 DefaultClientOptions = ClientOptions{ Reconnect: false, Context: nil, } )
View Source
var ( // DefaultServerOptions 默认 Server 选项 DefaultServerOptions = ServerOptions{ MaxConnNum: -1, Context: nil, } )
Functions ¶
Types ¶
type Agent ¶
type Agent interface {
// OnConnect 连接创建
OnConnect(conn Conn)
// OnMessage 收到消息
OnMessage(b []byte, conn Conn)
// OnClose 连接关闭
OnClose(conn Conn)
}
Agent 网络代理
type Client ¶
type Client interface {
Object
// Start 开启客户端连接
Start(address string, newAgent GetAgent, opts ...ClientOption) error
// IsConnected 是否处于连接状态
IsConnected() bool
}
Client 网络客户端
type ClientOption ¶
type ClientOption func(*ClientOptions)
ClientOption 客户端配置项
func WithClientContext ¶
func WithClientContext(context map[string]interface{}) ClientOption
WithClientContext 特定参数配置
type ClientOptions ¶
type ClientOptions struct {
// 是否自动重连 默认为否
Reconnect bool
// 特定客户端参数
Context map[string]interface{}
}
ClientOptions 配置结构体
type Codec ¶
type Codec interface {
// Encode 加密传输
Encode(c CodecConn, buf []byte) ([]byte, error)
// Decode 解密传输
Decode(c CodecConn) ([]byte, error)
}
Codec 网络连接的 Codec。
Codec 可以理解为协议,例:websocket 是 tcp 的一种 codec;如果你需要 websocket,那你只需要用 tcp 的网络库 + websocket codec
type CodecConn ¶
type CodecConn interface {
Conn
// Read 读取所有数据,不移动读指针
Read() (buf []byte)
// ResetBuffer 重置读取容器
ResetBuffer()
// ReadN 读取给定长度的数据,如果数据不够,则返回所有数据,不移动读指针
ReadN(n int) (size int, buf []byte)
// ShiftN 移动读指针到给定长度
ShiftN(n int) (size int)
// BufferLength 读取容器数据长度
BufferLength() (size int)
}
CodecConn 支持 Codec 的网络连接
type Conn ¶
type Conn interface {
Object
// Write 写入并发送数据
Write(b []byte) (n int, err error)
// LocalAddr 本地地址
LocalAddr() net.Addr
// RemoteAddr 远程地址
RemoteAddr() net.Addr
}
Conn 网络连接
type ID ¶
type ID uint64
ID 标准数字唯一键
func GenerateID ¶
func GenerateID() ID
GenerateID 返回一个随机生成的 64 字节 ID。这个函数进程安全 这个方法大概花费 13.29 ns, 不产出任何内存垃圾
type Object ¶
type Object interface {
// Run 持续执行逻辑,Run 函数主逻辑不需要使用Goroutine,而应该在外层调用时决定是否使用Goroutine
Run()
// Close 关闭对象푍
Close()
}
Object 持续存在的对象,需要注意生命周期管理
type Server ¶
type Server interface {
Object
// Start 开启服务器
Start(address string, newAgent GetAgent, opts ...ServerOption) error
// GetConnNum 获取服务器连接数
GetConnNum() (num int)
}
Server 网络服务器
type ServerOption ¶
type ServerOption func(*ServerOptions)
ServerOption 服务器配置项
func WithServerContext ¶
func WithServerContext(context map[string]interface{}) ServerOption
WithServerContext 特定参数配置
type ServerOptions ¶
type ServerOptions struct {
// 最大连接数 默认为-1(不限)
MaxConnNum int
// 特定服务器参数
Context map[string]interface{}
}
ServerOptions 服务器配置结构体
Click to show internal directories.
Click to hide internal directories.