代码拉取完成,页面将自动刷新
package sbox
import (
"sync"
"gitee.com/dennis-kk/rpc-go-backend/idlrpc/pkg/protocol"
"gitee.com/dennis-kk/rpc-go-backend/idlrpc/pkg/transport"
)
type (
// LoggedHandler User logout notification handle
LoggedHandler func(index protocol.GlobalIndexType)
// RawMsgHandler raw message handle
RawMsgHandler func(trans transport.ITransport, msg []byte) error
// IdentityChannelMap channel cache
IdentityChannelMap map[string]transport.ITransport
// ServiceMessageHandle The RPC message handling handle
ServiceMessageHandle struct {
rw sync.RWMutex
loggedCb LoggedHandler
channelMap IdentityChannelMap
rawMsgHandler RawMsgHandler
}
)
func (mh *ServiceMessageHandle) Init() {
mh.channelMap = make(IdentityChannelMap)
mh.loggedCb = nil
mh.rw = sync.RWMutex{}
}
// OnLoggedOut This function is triggered when the external connection is broken.
func (mh *ServiceMessageHandle) OnLoggedOut(index protocol.GlobalIndexType) {
if mh.loggedCb != nil {
// 修改为在协程中执行
go mh.loggedCb(index)
}
}
// SetLoggedOutHandle Call in the main coroutine and set the user logout response handle to ensure coroutine security.
func (mh *ServiceMessageHandle) SetLoggedOutHandle(cb LoggedHandler) {
mh.loggedCb = cb
}
// OnIdentityNotify This function is triggered when the other connection notifies it of its identity
func (mh *ServiceMessageHandle) OnIdentityNotify(trans transport.ITransport, identityID string, identityTag string) {
if trans == nil {
return
}
// 更新channel 的身份信息
trans.SetIdentityID(identityID)
trans.SetIdentityTag(identityTag)
// 缓存身份信息与channel映射关系
mh.rw.Lock()
defer mh.rw.Unlock()
mh.channelMap[identityID] = trans
}
// SetRawMsgHandle Call in the main coroutine and set the raw message handle to ensure coroutine security.
func (mh *ServiceMessageHandle) SetRawMsgHandle(handler RawMsgHandler) {
mh.rawMsgHandler = handler
}
// OnRawMessage This function is triggered when the other connection sends a message
func (mh *ServiceMessageHandle) OnRawMessage(trans transport.ITransport, msg []byte) {
if trans == nil {
return
}
if mh.rawMsgHandler != nil {
if err := mh.rawMsgHandler(trans, msg); err != nil {
return
}
}
}
func (mh *ServiceMessageHandle) onClose(trans transport.ITransport) {
if trans == nil {
return
}
// 无身份信息,不做处理
if len(trans.IdentityID()) == 0 {
return
}
mh.rw.Lock()
defer mh.rw.Unlock()
delete(mh.channelMap, trans.IdentityID())
}
// GetTransport getting the transport by identity id
func (mh *ServiceMessageHandle) GetTransport(identityID string) transport.ITransport {
mh.rw.RLock()
defer mh.rw.RUnlock()
trans, ok := mh.channelMap[identityID]
if !ok || trans.IsClose() {
return nil
}
return trans
}
func (mh *ServiceMessageHandle) UnInit() {
mh.channelMap = nil
mh.loggedCb = nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。