znet

package
v0.0.0-...-2747672 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPerClientIPLimitReached = errors.New("client connection limit reached")
)

Functions

func GetDeviceID

func GetDeviceID(sid int64) (datacenterid, workerid int64)

获取数据中心ID和机器ID

func GetGenTime

func GetGenTime(sid int64) (t string)

获取创建ID时的时间字符串(精度:秒)

func GetGenTimestamp

func GetGenTimestamp(sid int64) (timestamp int64)

获取创建ID时的时间戳

func GetTimestamp

func GetTimestamp(sid int64) (timestamp int64)

获取时间戳

func GetTimestampStatus

func GetTimestampStatus() (state float64)

获取时间戳已使用的占比:范围(0.0 - 1.0)

func NewServer

func NewServer() ziface.IServer

实例服务器

Types

type BaseRouter

type BaseRouter struct {
}

实现router,先嵌入基类

func (*BaseRouter) Handle

func (br *BaseRouter) Handle(ziface.IRequest)

处理业务

func (*BaseRouter) PostHandle

func (br *BaseRouter) PostHandle(ziface.IRequest)

处理业务之后

func (*BaseRouter) PreHandle

func (br *BaseRouter) PreHandle(ziface.IRequest)

处理业务之前

type ConnManager

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

连接管理

func NewConnManager

func NewConnManager() *ConnManager

创建管理

func (*ConnManager) Add

func (connMgr *ConnManager) Add(conn ziface.IConnection)

添加连接

func (*ConnManager) ClearConn

func (connMgr *ConnManager) ClearConn()

清除全部连接

func (*ConnManager) Get

func (connMgr *ConnManager) Get(connID int64) (ziface.IConnection, error)

根据id查找连接

func (*ConnManager) Len

func (connMgr *ConnManager) Len() int

总连接个数

func (*ConnManager) Remove

func (connMgr *ConnManager) Remove(conn ziface.IConnection)

删除连接

type Connection

type Connection struct {

	//心跳检查间隔
	HeartbeatInterval int
	// 加入了哪些房间
	Rooms map[string]bool
	//当前属于那个server
	WsServer ziface.IServer
	//当前连接的ws
	Conn *zsocket.Conn
	//连接id
	ConnID int64

	//告知当前连接已经退出/停止,由reder退出的信号
	ExitChan chan bool

	//路由管理,用来绑定msgid与api关系
	MsgHandle ziface.IMsgHandle

	//消息类型 TextMessage 或 BinaryMessage 之类
	MessageType int `json:"messageType"`
	// contains filtered or unexported fields
}

连接管理

func NewConnection

func NewConnection(server ziface.IServer, conn *zsocket.Conn, connID int64, mh ziface.IMsgHandle) *Connection

初始化连接方法

func (*Connection) DelJoinRoomById

func (c *Connection) DelJoinRoomById(room string)

删除加入房间通过ID

func (*Connection) GetConnID

func (c *Connection) GetConnID() int64

获取当前连接的id

func (*Connection) GetJoinRooms

func (c *Connection) GetJoinRooms() map[string]bool

获取加入房间

func (*Connection) GetMessageType

func (c *Connection) GetMessageType() int

获取消息类型

func (*Connection) GetProperty

func (c *Connection) GetProperty(key string) (p interface{}, err error)

获取连接属性

func (*Connection) GetSnowflakeID

func (c *Connection) GetSnowflakeID() int64

获取snowflake不重复ID生成一个

func (*Connection) GetWsConnection

func (c *Connection) GetWsConnection() *zsocket.Conn

获取当前连接的websocket conn

func (*Connection) HeartbeatChecker

func (c *Connection) HeartbeatChecker()

每隔1秒, 检查一次连接是否健康

func (*Connection) IsAlive

func (c *Connection) IsAlive() bool

检查心跳(不需要太频繁)

func (*Connection) IsClosed

func (c *Connection) IsClosed() bool

是否关闭

func (*Connection) IsJoinRoom

func (c *Connection) IsJoinRoom(room string) bool

是否加入房间

func (*Connection) KeepAlive

func (c *Connection) KeepAlive()

更新心跳

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() net.Addr

获取连接客户端的信息,后续可以加userAgent等

func (*Connection) RemoveProperty

func (c *Connection) RemoveProperty(key string)

移除设置属性

func (*Connection) SendBuffByteMsg

func (c *Connection) SendBuffByteMsg(data []byte)

发送数据,将数据发送给远程客户端(有缓冲)

func (*Connection) SendBuffMsg

func (c *Connection) SendBuffMsg(data string)

发送数据,将数据发送给远程客户端(有缓冲)

func (*Connection) SendByteMsg

func (c *Connection) SendByteMsg(data string)

发送数据,将数据发送给远程客户端(无缓冲)

func (*Connection) SendErrorMsg

func (c *Connection) SendErrorMsg(error string)

发送数据,系统提示信息发送给远程客户端(有缓冲)

func (*Connection) SendMsg

func (c *Connection) SendMsg(data string)

发送数据,将数据发送给远程客户端(无缓冲)

func (*Connection) SetJoinRooms

func (c *Connection) SetJoinRooms(room string)

设置加入房间

func (*Connection) SetMessageType

func (c *Connection) SetMessageType(mt int)

设置消息类型

func (*Connection) SetProperty

func (c *Connection) SetProperty(key string, value interface{})

设置连接属性

func (*Connection) Start

func (c *Connection) Start()

启动连接,让当前连接,开始工作

func (*Connection) StartReader

func (c *Connection) StartReader()

读业务

func (*Connection) StartWriter

func (c *Connection) StartWriter()

写业务,专门发给客户端

func (*Connection) Stop

func (c *Connection) Stop()

停止连接,结束当前连接工作

type Error

type Error struct {
	Title string `json:"title"`
	Error string `json:"error"`
}

错误信息结构体

type Limiter

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

func NewLimiter

func NewLimiter(cfg LimiterConfig) *Limiter

func (*Limiter) Accept

func (l *Limiter) Accept(conn net.Conn) (func(), error)

func (*Limiter) Config

func (l *Limiter) Config() LimiterConfig

func (*Limiter) HTTPConnStateFunc

func (l *Limiter) HTTPConnStateFunc() func(net.Conn, http.ConnState)

func (*Limiter) HTTPConnStateFuncWithErrorHandler

func (l *Limiter) HTTPConnStateFuncWithErrorHandler(errorHandler func(error, net.Conn)) func(net.Conn, http.ConnState)

func (*Limiter) NumOpen

func (l *Limiter) NumOpen(addr net.Addr) int

func (*Limiter) SetConfig

func (l *Limiter) SetConfig(c LimiterConfig)

type LimiterConfig

type LimiterConfig struct {
	MaxConnsPerClientIP int
}

type Message

type Message struct {
	Command string          `json:"command"` //消息的命令
	Type    string          `json:"type"`    //消息的分类
	Data    json.RawMessage `json:"data"`    //消息的内容
}

接收客户端的数据结构

func NewMessage

func NewMessage(command string, data json.RawMessage) *Message

创建一个Message消息

func (*Message) GetCommand

func (msg *Message) GetCommand() string

获取消息命令

func (*Message) GetData

func (msg *Message) GetData() json.RawMessage

获取消息数据

func (*Message) GetType

func (msg *Message) GetType() string

获取消息命令类型

func (*Message) SetCommand

func (msg *Message) SetCommand(command string)

设置消息命令

func (*Message) SetData

func (msg *Message) SetData(data json.RawMessage)

设置消息数据

func (*Message) SetType

func (msg *Message) SetType(t string)

设置消息命令类型

type MsgHandle

type MsgHandle struct {
	//消息处理
	MsgPost map[string]ziface.IRouter
	//负责worker取任务消息队列
	TaskQueue []chan ziface.IRequest
	//业务工作worker池的worker数量
	WorkerPoolSize int64
	// contains filtered or unexported fields
}

消息处理模块

func NewMsgHandle

func NewMsgHandle() *MsgHandle

创建

func (*MsgHandle) DoMsgHandler

func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest)

执行对应的路由处理方法

func (*MsgHandle) SendMsgToTaskQueue

func (mh *MsgHandle) SendMsgToTaskQueue(request ziface.IRequest)

将消息交给taskqueue处理

func (*MsgHandle) SetRouter

func (mh *MsgHandle) SetRouter(command string, ctype string, name string, router ziface.IRouter)

添加路由

func (*MsgHandle) StartOneWorker

func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan ziface.IRequest)

启动一个工作流程

func (*MsgHandle) StartWorkerPool

func (mh *MsgHandle) StartWorkerPool()

启动一个worker工作池 只能发生一次

type Request

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

func NewRequest

func NewRequest(conn ziface.IConnection, msg ziface.IMessage) *Request

创建消息

func (*Request) GetCommand

func (r *Request) GetCommand() string

获取请求的消息的命令

func (*Request) GetConnection

func (r *Request) GetConnection() ziface.IConnection

得到当前连接

func (*Request) GetData

func (r *Request) GetData() json.RawMessage

得到请求数据

func (*Request) GetType

func (r *Request) GetType() string

获取请求的消息的命令类型

type Router

type Router struct {
	BaseRouter
}

路由

type Server

type Server struct {
	//服务器名称
	Name string
	//服务器协议 ws,wss
	Scheme string
	//服务器ip地址
	Host string
	//服务器端口
	Port uint32
	//协议扩展
	Path string
	//路由管理,用来绑定Command与API关系
	MsgHandle ziface.IMsgHandle
	//连接属性
	ConnMgr ziface.IConnManager
	//连接回调
	OnConnStart func(ziface.IConnection)
	//关闭回调
	OnConnStop func(ziface.IConnection)
	//超过最大连接回调
	OnMaxConn func(conn *zsocket.Conn)
	//ID生成
	Snowflake ziface.ISnowflake
}

func (*Server) CallOnConnStart

func (s *Server) CallOnConnStart(conn ziface.IConnection)

调用连接之前

func (*Server) CallOnConnStop

func (s *Server) CallOnConnStop(conn ziface.IConnection)

调用关闭之前

func (*Server) CallOnMaxConn

func (s *Server) CallOnMaxConn(conn *zsocket.Conn)

超过最大连接回调

func (*Server) GetConnMgr

func (s *Server) GetConnMgr() ziface.IConnManager

返回 连接管理

func (*Server) GetSnowflakeMgr

func (s *Server) GetSnowflakeMgr() ziface.ISnowflake

返回 Snowflake管理

func (*Server) Serve

func (s *Server) Serve()

运行状态

func (*Server) SetOnConnStart

func (s *Server) SetOnConnStart(hookStart func(conn ziface.IConnection))

连接之前回调

func (*Server) SetOnConnStop

func (s *Server) SetOnConnStop(hookStop func(conn ziface.IConnection))

关闭之前回调

func (*Server) SetOnMaxConn

func (s *Server) SetOnMaxConn(hookConn func(conn *zsocket.Conn))

超过最大连接回调

func (*Server) SetRouter

func (s *Server) SetRouter(command string, ctype string, name string, router ziface.IRouter)

添加路由

func (*Server) Start

func (s *Server) Start()

启动

func (*Server) Stop

func (s *Server) Stop()

停止

type Snowflake

type Snowflake struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewSnowflake

func NewSnowflake(datacenterid, workerid int64) *Snowflake

func (*Snowflake) NextVal

func (s *Snowflake) NextVal() int64

Jump to

Keyboard shortcuts

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