128 lines
3.6 KiB
Go
128 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"sync"
|
|
|
|
"go_dreamfactory/lego/base"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"go_dreamfactory/lego/sys/log"
|
|
)
|
|
|
|
/*
|
|
用户代理对象管理组件
|
|
*/
|
|
|
|
type AgentMgrComp struct {
|
|
cbase.ModuleCompBase
|
|
service base.IRPCXService
|
|
agents *sync.Map
|
|
}
|
|
|
|
func (this *AgentMgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.ModuleCompBase.Init(service, module, comp, options)
|
|
this.service = service.(base.IRPCXService)
|
|
this.agents = new(sync.Map)
|
|
return
|
|
}
|
|
|
|
//加入新的用户
|
|
func (this *AgentMgrComp) Connect(a IAgent) {
|
|
this.agents.Store(a.SessionId(), a)
|
|
}
|
|
|
|
//移除断开的用户
|
|
func (this *AgentMgrComp) DisConnect(a IAgent) {
|
|
this.agents.Delete(a.SessionId())
|
|
if a.UserId() != "" { //登录用户 通知业务服务处理玩家离线相关
|
|
reply := &pb.RPCMessageReply{}
|
|
if _, err := this.service.RpcGo(context.Background(), fmt.Sprintf("%s/%s", comm.Service_Worker, a.WorkerId()), string(comm.Rpc_NoticeUserClose), &pb.NoticeUserCloseReq{
|
|
UserSessionId: a.SessionId(),
|
|
UserId: a.UserId(),
|
|
}, reply); err != nil {
|
|
log.Errorf(" uId:%s Rpc_NoticeUserClose err:%v", a.UserId(), err)
|
|
}
|
|
}
|
|
}
|
|
|
|
//用户登录绑定Id
|
|
func (this *AgentMgrComp) Bind(ctx context.Context, args *pb.AgentBuildReq, reply *pb.RPCMessageReply) error {
|
|
if a, ok := this.agents.Load(args.UserSessionId); ok {
|
|
a.(IAgent).Bind(args.UserId, args.WorkerId)
|
|
} else {
|
|
reply.Code = pb.ErrorCode_UserSessionNobeing
|
|
reply.ErrorMessage = pb.GetErrorCodeMsg(pb.ErrorCode_UserSessionNobeing)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//用户登录解绑Id
|
|
func (this *AgentMgrComp) UnBind(ctx context.Context, args *pb.AgentUnBuildReq, reply *pb.RPCMessageReply) error {
|
|
if a, ok := this.agents.Load(args.UserSessionId); ok {
|
|
a.(IAgent).UnBind()
|
|
} else {
|
|
reply.Code = pb.ErrorCode_UserSessionNobeing
|
|
reply.ErrorMessage = pb.GetErrorCodeMsg(pb.ErrorCode_UserSessionNobeing)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//向用户发送消息
|
|
func (this *AgentMgrComp) SendMsgToAgent(ctx context.Context, args *pb.AgentSendMessageReq, reply *pb.RPCMessageReply) error {
|
|
if a, ok := this.agents.Load(args.UserSessionId); ok {
|
|
a.(IAgent).WriteMsg(&pb.UserMessage{
|
|
MainType: args.MainType,
|
|
SubType: args.SubType,
|
|
Data: args.Data,
|
|
})
|
|
} else {
|
|
reply.Code = pb.ErrorCode_UserSessionNobeing
|
|
reply.ErrorMessage = pb.GetErrorCodeMsg(pb.ErrorCode_UserSessionNobeing)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//向多个户发送消息
|
|
func (this *AgentMgrComp) SendMsgToAgents(ctx context.Context, args *pb.BatchMessageReq, reply *pb.RPCMessageReply) error {
|
|
msg := &pb.UserMessage{
|
|
MainType: args.MainType,
|
|
SubType: args.SubType,
|
|
Data: args.Data,
|
|
}
|
|
for _, v := range args.UserSessionIds {
|
|
if a, ok := this.agents.Load(v); ok {
|
|
a.(IAgent).WriteMsg(msg)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//向所有户发送消息
|
|
func (this *AgentMgrComp) SendMsgToAllAgent(ctx context.Context, args *pb.BroadCastMessageReq, reply *pb.RPCMessageReply) error {
|
|
msg := &pb.UserMessage{
|
|
MainType: args.MainType,
|
|
SubType: args.SubType,
|
|
Data: args.Data,
|
|
}
|
|
this.agents.Range(func(key, value any) bool {
|
|
value.(IAgent).WriteMsg(msg)
|
|
return true
|
|
})
|
|
return nil
|
|
}
|
|
|
|
//关闭代理
|
|
func (this *AgentMgrComp) CloseAgent(ctx context.Context, args *pb.AgentCloseeReq, reply *pb.RPCMessageReply) error {
|
|
if a, ok := this.agents.Load(args.UserSessionId); ok {
|
|
a.(IAgent).Close()
|
|
} else {
|
|
reply.Code = pb.ErrorCode_UserSessionNobeing
|
|
reply.ErrorMessage = pb.GetErrorCodeMsg(pb.ErrorCode_UserSessionNobeing)
|
|
}
|
|
return nil
|
|
}
|