Documentation ¶
Overview ¶
Package connect 实现了 micserver 中 模块间连接/客户端Gateway连接 的逻辑, 包括了所有连接需要用到的方法,连接池管理方法。
Index ¶
- Constants
- type BaseConnect
- func (bc *BaseConnect) GetCreateTime() int64
- func (bc *BaseConnect) GetIConnection() IConnection
- func (bc *BaseConnect) GetJobNum() uint32
- func (bc *BaseConnect) GetRecvMessageChannel() chan *msg.MessageBinary
- func (bc *BaseConnect) GetTempID() string
- func (bc *BaseConnect) HookProtocal(p baseio.Protocal)
- func (bc *BaseConnect) Init()
- func (bc *BaseConnect) IsTerminate(curtime int64) bool
- func (bc *BaseConnect) IsTerminateForce() bool
- func (bc *BaseConnect) IsTerminateTimeout(curtime int64) bool
- func (bc *BaseConnect) IsVertify() bool
- func (bc *BaseConnect) RemoteAddr() string
- func (bc *BaseConnect) SendBytes(cmdid uint16, protodata []byte) error
- func (bc *BaseConnect) SendCmd(v msg.IMsgStruct) error
- func (bc *BaseConnect) SendCmdWithCallback(v msg.IMsgStruct, cb func(interface{}), cbarg interface{}) error
- func (bc *BaseConnect) SetJobNum(jnum uint32)
- func (bc *BaseConnect) SetLogger(l *log.Logger)
- func (bc *BaseConnect) SetMsgCodec(codec msg.IMsgCodec)
- func (bc *BaseConnect) SetTerminateTime(value int64)
- func (bc *BaseConnect) SetVertify(value bool)
- func (bc *BaseConnect) Shutdown()
- func (bc *BaseConnect) StartRecv()
- func (bc *BaseConnect) Terminate()
- type Client
- type ClientPool
- func (cp *ClientPool) Add(client *Client)
- func (cp *ClientPool) Get(tempid string) *Client
- func (cp *ClientPool) GetRandom() *Client
- func (cp *ClientPool) Init()
- func (cp *ClientPool) Len() uint32
- func (cp *ClientPool) LoadOrStore(k string, v *Client) (*Client, bool)
- func (cp *ClientPool) NewTCPClient(conn net.Conn, connHook IConnectHook) (*Client, error)
- func (cp *ClientPool) Range(cb func(*Client) bool)
- func (cp *ClientPool) Remove(tempid string)
- func (cp *ClientPool) SetLogger(l *log.Logger)
- type IConnectHook
- type IConnection
- type Ping
- type Server
- type ServerPool
- func (sp *ServerPool) AddServer(connct *Server, tmpid string)
- func (sp *ServerPool) AddServerAuto(connct *Server)
- func (sp *ServerPool) BroadcastByType(servertype string, v msg.IMsgStruct)
- func (sp *ServerPool) BroadcastCmd(v msg.IMsgStruct)
- func (sp *ServerPool) ChangeServerTempid(tcptask *Server, newTmpID string) error
- func (sp *ServerPool) GetLatestVersionByType(servertype string) uint64
- func (sp *ServerPool) GetMinLoadServer(servertype string) *Server
- func (sp *ServerPool) GetMinLoadServerLatestVersion(servertype string) *Server
- func (sp *ServerPool) GetRandomServer(servertype string) *Server
- func (sp *ServerPool) GetServer(tmpid string) *Server
- func (sp *ServerPool) Init(groupID uint16)
- func (sp *ServerPool) Len() int
- func (sp *ServerPool) NewChanServer(sctype TServerSCType, sendChan chan *msg.MessageBinary, ...) *Server
- func (sp *ServerPool) NewTCPServer(sctype TServerSCType, conn net.Conn, moduleid string, ...) *Server
- func (sp *ServerPool) RangeServer(callback func(*Server) bool)
- func (sp *ServerPool) RemoveServer(tmpid string)
- type TServerSCType
Constants ¶
const ClientConnRecvBufferSize = 256 * 1024
ClientConnRecvBufferSize 发送缓冲大小,用于将多个小消息拼接发送的缓冲大小
const ClientConnRecvChanSize = 256
ClientConnRecvChanSize 客户端连接发送消息缓冲不宜过大, 10*64KiB*100000连接=64GiB
const ClientConnSendBufferSize = 16 * 1024
ClientConnSendBufferSize 发送缓冲大小,用于将多个小消息拼接发送的缓冲大小
const ClientConnSendChanSize = 256
ClientConnSendChanSize 客户端连接发送消息缓冲不宜过大, 10*64KiB*100000连接=64GiB
const ServerRecvBufferSize = msg.MessageMaxSize * 10
ServerRecvBufferSize 发送缓冲大小,用于将多个小消息拼接发送的缓冲大小
const ServerRecvChanSize = 100000
ServerRecvChanSize 服务器连接发送消息缓冲要考虑到服务器处理消息的能力
const ServerSendBufferSize = msg.MessageMaxSize * 10
ServerSendBufferSize 发送缓冲大小,用于将多个小消息拼接发送的缓冲大小
const ServerSendChanSize = 100000
ServerSendChanSize 服务器连接发送消息缓冲要考虑到服务器处理消息的能力
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseConnect ¶
type BaseConnect struct { *log.Logger // 连接实体 IConnection IConnection // 是否是正常的断开连接 IsNormalDisconnect bool // contains filtered or unexported fields }
BaseConnect 一个连接都会具有的基础对象,整合了一个连接的基础属性
func (*BaseConnect) GetCreateTime ¶
func (bc *BaseConnect) GetCreateTime() int64
GetCreateTime 获取该连接对象构造完成的时间,由 BaseConnect.Init 初始化
func (*BaseConnect) GetIConnection ¶
func (bc *BaseConnect) GetIConnection() IConnection
GetIConnection 获取该连接的底层连接接口
func (*BaseConnect) GetRecvMessageChannel ¶
func (bc *BaseConnect) GetRecvMessageChannel() chan *msg.MessageBinary
GetRecvMessageChannel 获取该连接的消息处理 channel ,可以通过该 channel 接收到该连接收到的消息, 接收到的消息已经经过了 BaseConnect.HookProtocal 处理特殊网络协议, 经过 SetMsgCodec 处理特殊消息编解码格式, 从该 channel 中得到的 *msg.MessageBinary 的 protodata 已是消息本身的内容。
func (*BaseConnect) HookProtocal ¶
func (bc *BaseConnect) HookProtocal(p baseio.Protocal)
HookProtocal 设置该连接的协议,如果 p == nil ,该连接的网络协议视为普通的 TCP 协议, 通过提供非空的 p ,可以将该连接实现为使用 websocket 等其他网络协议
func (*BaseConnect) IsTerminate ¶
func (bc *BaseConnect) IsTerminate(curtime int64) bool
IsTerminate 判断连接是否已终止,包括了主动终止以及超时终止
func (*BaseConnect) IsTerminateForce ¶
func (bc *BaseConnect) IsTerminateForce() bool
IsTerminateForce 判断连接是否已被主动强制终止,通过 BaseConnect.Terminate 设置
func (*BaseConnect) IsTerminateTimeout ¶
func (bc *BaseConnect) IsTerminateTimeout(curtime int64) bool
IsTerminateTimeout 通过过期时间等判断是否已强制终止
func (*BaseConnect) IsVertify ¶
func (bc *BaseConnect) IsVertify() bool
IsVertify 该连接是否通过了验证,如果这是一个 Module 间的连接,需要经过 server.Server 的登陆逻辑处理,才会成为一个经过验证的连接。如果是一个客户端连接, 默认该连接不是一个经过验证的连接。
func (*BaseConnect) SendBytes ¶
func (bc *BaseConnect) SendBytes( cmdid uint16, protodata []byte) error
SendBytes 异步发送一条消息,使用 cmdid 及 protodata 来发送,如果不使用 msg.MsgStruct 作为消息发送,你可以利用该方法,将消息编码之后发送。
func (*BaseConnect) SendCmd ¶
func (bc *BaseConnect) SendCmd(v msg.IMsgStruct) error
SendCmd 异步发送一条消息
func (*BaseConnect) SendCmdWithCallback ¶
func (bc *BaseConnect) SendCmdWithCallback(v msg.IMsgStruct, cb func(interface{}), cbarg interface{}) error
SendCmdWithCallback 异步发送一条消息,带发送完成回调,在消息真正通过 network 发送成功之后, 会调用 cb 回调。
func (*BaseConnect) SetLogger ¶
func (bc *BaseConnect) SetLogger(l *log.Logger)
SetLogger 设置该连接的 Logger ,便于Log信息整理
func (*BaseConnect) SetMsgCodec ¶
func (bc *BaseConnect) SetMsgCodec(codec msg.IMsgCodec)
SetMsgCodec 设置该连接的消息编解码器
func (*BaseConnect) SetTerminateTime ¶
func (bc *BaseConnect) SetTerminateTime(value int64)
SetTerminateTime 设置连接过期时间,如果一个连接过期了,在下一个发送或接收行为会将连接置为断开状态
func (*BaseConnect) SetVertify ¶
func (bc *BaseConnect) SetVertify(value bool)
SetVertify 设置该连接的验证状态
func (*BaseConnect) StartRecv ¶
func (bc *BaseConnect) StartRecv()
StartRecv 开始接收消息,在调用该消息前,无法在 BaseConnect.GetRecvMessageChannel 中接收到消息
type Client ¶
type Client struct { BaseConnect // 会话信息 可在不同服务器之间同步的 *session.Session // contains filtered or unexported fields }
Client 一个客户端连接,一般由 Gateway 创建
type ClientPool ¶
ClientPool 客户端连接池
func (*ClientPool) LoadOrStore ¶
func (cp *ClientPool) LoadOrStore(k string, v *Client) (*Client, bool)
LoadOrStore 加载或存储一个客户端连接
func (*ClientPool) NewTCPClient ¶
func (cp *ClientPool) NewTCPClient(conn net.Conn, connHook IConnectHook) (*Client, error)
NewTCPClient 使用TCP连接新建一个Client
func (*ClientPool) Range ¶
func (cp *ClientPool) Range( cb func(*Client) bool)
Range 遍历连接池中的所有连接,如果 cb() 返回 false 则中止遍历
func (*ClientPool) Remove ¶
func (cp *ClientPool) Remove(tempid string)
Remove 根据连接的 TmpID 从连接池移除一个连接
func (*ClientPool) SetLogger ¶
func (cp *ClientPool) SetLogger(l *log.Logger)
SetLogger 设置客户端连接池的Logger
type IConnectHook ¶
type IConnectHook interface { OnRecvConnectMessage(*Client, *msg.MessageBinary) OnConnectClose(*Client) }
IConnectHook 连接事件钩子需要满足的接口
type IConnection ¶
type IConnection interface { IsAlive() bool Shutdown() error RemoteAddr() string Read(toData []byte) (int, error) StartRecv() GetRecvMessageChannel() chan *msg.MessageBinary SendMessageBinary(msgbinary *msg.MessageBinary) error SendBytes(cmdid uint16, protodata []byte) error Write(data []byte) (int, error) HookProtocal(p baseio.Protocal) SetBanAutoResize(value bool) GetMsgCodec() msg.IMsgCodec SetMsgCodec(msg.IMsgCodec) }
IConnection 模块间/客户端 连接都实现了该接口
func NewChan ¶
func NewChan(sendChan chan *msg.MessageBinary, recvChan chan *msg.MessageBinary, l *log.Logger) IConnection
NewChan 新建一个 chan 连接
type Ping ¶
type Ping struct {
// contains filtered or unexported fields
}
Ping 连接的 Ping 信息 可以主动设置发送时间,或者使用内部的三次握手实现双端 Ping
type Server ¶
type Server struct { BaseConnect // 建立连接优先级 ConnectPriority int64 // 该连接对方服务器信息 ModuleInfo *servercomm.ModuleInfo // contains filtered or unexported fields }
Server 服务器连接,在一个模块的 SubnetManager 中,连接至该模块的任何模块都在该模块中 存在一个 Server 连接。
func (*Server) InitChan ¶
func (s *Server) InitChan(sctype TServerSCType, sendChan chan *msg.MessageBinary, recvChan chan *msg.MessageBinary, onRecv func(*Server, *msg.MessageBinary), onClose func(*Server))
InitChan 初始化一个新的服务器连接 sctype: 连接的 客户端/服务器 类型 sendChan: 发送消息管道 recvChan: 接收消息管道
func (*Server) InitTCP ¶
func (s *Server) InitTCP(sctype TServerSCType, netconn net.Conn, onRecv func(*Server, *msg.MessageBinary), onClose func(*Server))
InitTCP 初始化一个新的服务器连接 sctype: 连接的 客户端/服务器 类型 netconn: 连接的net.Conn对象
type ServerPool ¶
ServerPool 服务器连接池
func (*ServerPool) AddServer ¶
func (sp *ServerPool) AddServer(connct *Server, tmpid string)
AddServer 在本连接池中新增一个服务器连接,并且指定该连接的 TmpID
func (*ServerPool) AddServerAuto ¶
func (sp *ServerPool) AddServerAuto(connct *Server)
AddServerAuto 在本连接池中新增一个服务器连接
func (*ServerPool) BroadcastByType ¶
func (sp *ServerPool) BroadcastByType(servertype string, v msg.IMsgStruct)
BroadcastByType 将一条消息广播至指定类型的所有连接
func (*ServerPool) BroadcastCmd ¶
func (sp *ServerPool) BroadcastCmd(v msg.IMsgStruct)
BroadcastCmd 广播消息到本连接池中的所有连接
func (*ServerPool) ChangeServerTempid ¶
func (sp *ServerPool) ChangeServerTempid(tcptask *Server, newTmpID string) error
ChangeServerTempid 修改链接的 TmpID ,目标 TmpID 不可已存在于该连接池,否则返回 error
func (*ServerPool) GetLatestVersionByType ¶
func (sp *ServerPool) GetLatestVersionByType(servertype string) uint64
GetLatestVersionByType 获取指定类型服务器的最新版本
func (*ServerPool) GetMinLoadServer ¶
func (sp *ServerPool) GetMinLoadServer(servertype string) *Server
GetMinLoadServer 获取指定类型负载最小的一个连接
func (*ServerPool) GetMinLoadServerLatestVersion ¶
func (sp *ServerPool) GetMinLoadServerLatestVersion(servertype string) *Server
GetMinLoadServerLatestVersion 获取指定类型服务器的最新版本负载最小的一个连接
func (*ServerPool) GetRandomServer ¶
func (sp *ServerPool) GetRandomServer(servertype string) *Server
GetRandomServer 随机获取指定类型的一个连接
func (*ServerPool) GetServer ¶
func (sp *ServerPool) GetServer(tmpid string) *Server
GetServer 根据连接的 TmpID 获取一个连接
func (*ServerPool) NewChanServer ¶
func (sp *ServerPool) NewChanServer(sctype TServerSCType, sendChan chan *msg.MessageBinary, recvChan chan *msg.MessageBinary, moduleid string, onRecv func(*Server, *msg.MessageBinary), onClose func(*Server)) *Server
NewChanServer 使用 chan 创建一个服务器连接
func (*ServerPool) NewTCPServer ¶
func (sp *ServerPool) NewTCPServer(sctype TServerSCType, conn net.Conn, moduleid string, onRecv func(*Server, *msg.MessageBinary), onClose func(*Server)) *Server
NewTCPServer 使用 TCP 创建一个服务器连接
func (*ServerPool) RangeServer ¶
func (sp *ServerPool) RangeServer( callback func(*Server) bool)
RangeServer 遍历连接池中的所有连接
func (*ServerPool) RemoveServer ¶
func (sp *ServerPool) RemoveServer(tmpid string)
RemoveServer 通过连接的 TmpID 从该连接池移除一个服务器连接
type TServerSCType ¶
type TServerSCType uint32
TServerSCType server server-client type
const ( ServerSCTypeNone TServerSCType = 1 ServerSCTypeTask TServerSCType = 2 ServerSCTypeClient TServerSCType = 3 )
TServerSCType