4 Star 5 Fork 4

Plato / Service-Box-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
message_handle.go 2.82 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dennis-kk/service-box-go.git
git@gitee.com:dennis-kk/service-box-go.git
dennis-kk
service-box-go
Service-Box-go
v0.5.17

搜索帮助

344bd9b3 5694891 D2dac590 5694891