From f51f492164d4d481739def608ae2ab783c79b5f2 Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Tue, 18 Oct 2022 10:15:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=A8=E6=9C=8Dsession?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/imodule.go | 4 +- modules/friend/api.go | 22 ++++--- modules/friend/api_randlist.go | 6 -- modules/user/model_session.go | 15 +++-- modules/user/module.go | 40 ++++++++++-- pb/comm.pb.go | 115 +++++++++++++++++++++++++-------- 6 files changed, 149 insertions(+), 53 deletions(-) diff --git a/comm/imodule.go b/comm/imodule.go index bb2097ee2..485ad9346 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -67,8 +67,10 @@ type ( //玩家 IUser interface { - //获取用户数据 + //获取本服用户数据 GetUser(uid string) *pb.DBUser + // 获取跨服用户数据 + GetCrossUser(uid string) (*pb.DBUser, error) //获取用户回话 GetUserSession(uid string) *pb.CacheUser //查询用户属性值 例如 金币 经验 diff --git a/modules/friend/api.go b/modules/friend/api.go index 657618c26..4dcf2daa3 100644 --- a/modules/friend/api.go +++ b/modules/friend/api.go @@ -38,16 +38,20 @@ func (this *apiComp) Init(service core.IService, module core.IModule, comp core. } func (this *apiComp) setDefaultFriendUserBaseInfo(userId string) *pb.FriendBase { - user := this.moduleFriend.ModuleUser.GetUser(userId) - if user != nil { - return &pb.FriendBase{ - ServerId: user.Sid, - UserId: userId, - NickName: user.Name, - Level: user.Lv, - Avatar: user.Avatar, - OfflineTime: user.Offlinetime, + if user, err := this.moduleFriend.ModuleUser.GetCrossUser(userId); err != nil { + return nil + } else { + if user != nil { + return &pb.FriendBase{ + ServerId: user.Sid, + UserId: userId, + NickName: user.Name, + Level: user.Lv, + Avatar: user.Avatar, + OfflineTime: user.Offlinetime, + } } } + return nil } diff --git a/modules/friend/api_randlist.go b/modules/friend/api_randlist.go index 5c0c26b99..cfde86e76 100644 --- a/modules/friend/api_randlist.go +++ b/modules/friend/api_randlist.go @@ -71,12 +71,6 @@ func (this *apiComp) Randlist(session comm.IUserSession, req *pb.FriendRandlistR continue } - // // 获取在线好友的信息 - // user := this.moduleFriend.ModuleUser.GetUser(uid) - // if user == nil { - // continue - // } - base := this.setDefaultFriendUserBaseInfo(uid) if base == nil { continue diff --git a/modules/user/model_session.go b/modules/user/model_session.go index b456fe138..c4f74c030 100644 --- a/modules/user/model_session.go +++ b/modules/user/model_session.go @@ -1,6 +1,7 @@ package user import ( + "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" @@ -19,7 +20,12 @@ func (this *ModelSession) Init(service core.IService, module core.IModule, comp this.module = module.(*User) this.TableName = comm.TableSession this.Expired = 0 //不自动过期 - this.clean() + return +} + +func (this *ModelSession) Start() (err error) { + this.MCompModel.Start() + this.clean(this.module.service.GetTag()) return } @@ -45,15 +51,12 @@ func (this *ModelSession) setUserSession(uid string, session comm.IUserSession) log.Debugf("setUserSession err:%v", err) return } - - // this.module.service.AcrossClusterRpcGo(context.TODO(), - // this.module.service.GetTag(), comm.Service_Worker, serviceMethod string, args interface{}, reply interface{}) return } // 启动时清理session -func (this *ModelSession) clean() { - keys, err := this.Redis.Keys("session:*") +func (this *ModelSession) clean(key string) { + keys, err := this.Redis.Keys(fmt.Sprintf("session:%s-", key)) if err != nil { log.Errorf("redis keys err:%v", err) return diff --git a/modules/user/module.go b/modules/user/module.go index d9a26f6b1..f6a7d26d8 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -7,6 +7,7 @@ import ( "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/db" + "go_dreamfactory/utils" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" @@ -17,7 +18,10 @@ import ( ) const ( + // 跨服玩家 Rpc_GetAllOnlineUser string = "Rpc_GetAllOnlineUser" + // 跨服用户 + Rpc_GetCrossUser string = "Rpc_GetCrossUser" ) var _ comm.IUser = (*User)(nil) @@ -51,7 +55,8 @@ func (this *User) Init(service core.IService, module core.IModule, options core. func (this *User) Start() (err error) { err = this.ModuleBase.Start() event.RegisterGO(comm.EventUserOffline, this.CleanSession) - this.service.RegisterFunctionName(Rpc_GetAllOnlineUser, this.GetAllOnlineUser) + this.service.RegisterFunctionName(Rpc_GetAllOnlineUser, this.RpcGetAllOnlineUser) + this.service.RegisterFunctionName(Rpc_GetCrossUser, this.RpcGetCrossUser) return } @@ -73,6 +78,14 @@ func (this *User) GetUser(uid string) *pb.DBUser { return user } +// 获取跨服用户数据 +func (this *User) GetCrossUser(uid string) (*pb.DBUser, error) { + reply := &pb.DBUser{} + err := this.service.AcrossClusterRpcCall(context.Background(), this.GetCrossTag(), + comm.Service_Worker, Rpc_GetCrossUser, &pb.UIdReq{Uid: uid}, reply) + return reply, err +} + //获取用户会话 func (this *User) GetUserSession(uid string) *pb.CacheUser { return this.modelSession.getUserSession(uid) @@ -101,7 +114,7 @@ func (this *User) UserOnlineList() ([]*pb.CacheUser, error) { // 跨服玩家列表 func (this *User) CrossUserOnlineList() ([]*pb.CacheUser, error) { reply := &pb.UserOnlineResp{} - err := this.service.AcrossClusterRpcCall(context.Background(), this.service.GetTag(), + err := this.service.AcrossClusterRpcCall(context.Background(), this.GetCrossTag(), comm.Service_Worker, Rpc_GetAllOnlineUser, nil, reply) return reply.Users, err } @@ -275,8 +288,8 @@ func (this *User) ChangeUserExpand(uid string, value map[string]interface{}) err return this.modelExpand.ChangeUserExpand(uid, value) } -func (this *User) GetAllOnlineUser(ctx context.Context, args *pb.EmptyReq, reply *pb.UserOnlineResp) error { - conn, err := db.Cross() +func (this *User) RpcGetAllOnlineUser(ctx context.Context, args *pb.EmptyReq, reply *pb.UserOnlineResp) error { + conn, err := db.Local() if err != nil { log.Errorf("cross db err: %v", err) return err @@ -289,3 +302,22 @@ func (this *User) GetAllOnlineUser(ctx context.Context, args *pb.EmptyReq, reply reply.Users = cache return nil } + +func (this *User) RpcGetCrossUser(ctx context.Context, req *pb.UIdReq, reply *pb.DBUser) error { + sid, _, ok := utils.UIdSplit(req.Uid) + if !ok { + return errors.New("sid split error") + } + conn, err := db.ServerDBConn(sid) + if err != nil { + log.Errorf("cross db err: %v", err) + return err + } + model := db.NewDBModel(comm.TableUser, 0, conn) + + if err := model.Get(req.Uid, reply); err != nil { + return err + } + + return nil +} diff --git a/pb/comm.pb.go b/pb/comm.pb.go index e83278621..619aac825 100644 --- a/pb/comm.pb.go +++ b/pb/comm.pb.go @@ -1020,6 +1020,53 @@ func (x *RtaskParam) GetParam3() int32 { return 0 } +type UIdReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid"` +} + +func (x *UIdReq) Reset() { + *x = UIdReq{} + if protoimpl.UnsafeEnabled { + mi := &file_comm_proto_msgTypes[14] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UIdReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UIdReq) ProtoMessage() {} + +func (x *UIdReq) ProtoReflect() protoreflect.Message { + mi := &file_comm_proto_msgTypes[14] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UIdReq.ProtoReflect.Descriptor instead. +func (*UIdReq) Descriptor() ([]byte, []int) { + return file_comm_proto_rawDescGZIP(), []int{14} +} + +func (x *UIdReq) GetUid() string { + if x != nil { + return x.Uid + } + return "" +} + type EmptyReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -1029,7 +1076,7 @@ type EmptyReq struct { func (x *EmptyReq) Reset() { *x = EmptyReq{} if protoimpl.UnsafeEnabled { - mi := &file_comm_proto_msgTypes[14] + mi := &file_comm_proto_msgTypes[15] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1042,7 +1089,7 @@ func (x *EmptyReq) String() string { func (*EmptyReq) ProtoMessage() {} func (x *EmptyReq) ProtoReflect() protoreflect.Message { - mi := &file_comm_proto_msgTypes[14] + mi := &file_comm_proto_msgTypes[15] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1055,7 +1102,7 @@ func (x *EmptyReq) ProtoReflect() protoreflect.Message { // Deprecated: Use EmptyReq.ProtoReflect.Descriptor instead. func (*EmptyReq) Descriptor() ([]byte, []int) { - return file_comm_proto_rawDescGZIP(), []int{14} + return file_comm_proto_rawDescGZIP(), []int{15} } type EmptyResp struct { @@ -1067,7 +1114,7 @@ type EmptyResp struct { func (x *EmptyResp) Reset() { *x = EmptyResp{} if protoimpl.UnsafeEnabled { - mi := &file_comm_proto_msgTypes[15] + mi := &file_comm_proto_msgTypes[16] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1080,7 +1127,7 @@ func (x *EmptyResp) String() string { func (*EmptyResp) ProtoMessage() {} func (x *EmptyResp) ProtoReflect() protoreflect.Message { - mi := &file_comm_proto_msgTypes[15] + mi := &file_comm_proto_msgTypes[16] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1093,7 +1140,7 @@ func (x *EmptyResp) ProtoReflect() protoreflect.Message { // Deprecated: Use EmptyResp.ProtoReflect.Descriptor instead. func (*EmptyResp) Descriptor() ([]byte, []int) { - return file_comm_proto_rawDescGZIP(), []int{15} + return file_comm_proto_rawDescGZIP(), []int{16} } var File_comm_proto protoreflect.FileDescriptor @@ -1210,14 +1257,15 @@ var file_comm_proto_rawDesc = []byte{ 0x61, 0x72, 0x61, 0x6d, 0x31, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x32, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x32, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x33, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, - 0x61, 0x72, 0x61, 0x6d, 0x33, 0x22, 0x0a, 0x0a, 0x08, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, - 0x71, 0x22, 0x0b, 0x0a, 0x09, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x73, 0x70, 0x2a, 0x43, - 0x0a, 0x12, 0x48, 0x65, 0x72, 0x6f, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, - 0x54, 0x79, 0x70, 0x65, 0x12, 0x06, 0x0a, 0x02, 0x48, 0x70, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, - 0x41, 0x74, 0x6b, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x44, 0x65, 0x66, 0x10, 0x02, 0x12, 0x09, - 0x0a, 0x05, 0x53, 0x70, 0x65, 0x65, 0x64, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x72, 0x69, - 0x74, 0x10, 0x04, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x61, 0x72, 0x61, 0x6d, 0x33, 0x22, 0x1a, 0x0a, 0x06, 0x55, 0x49, 0x64, 0x52, 0x65, 0x71, 0x12, + 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, + 0x64, 0x22, 0x0a, 0x0a, 0x08, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x71, 0x22, 0x0b, 0x0a, + 0x09, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x65, 0x73, 0x70, 0x2a, 0x43, 0x0a, 0x12, 0x48, 0x65, + 0x72, 0x6f, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x54, 0x79, 0x70, 0x65, + 0x12, 0x06, 0x0a, 0x02, 0x48, 0x70, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x74, 0x6b, 0x10, + 0x01, 0x12, 0x07, 0x0a, 0x03, 0x44, 0x65, 0x66, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x53, 0x70, + 0x65, 0x65, 0x64, 0x10, 0x03, 0x12, 0x08, 0x0a, 0x04, 0x43, 0x72, 0x69, 0x74, 0x10, 0x04, 0x42, + 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -1233,7 +1281,7 @@ func file_comm_proto_rawDescGZIP() []byte { } var file_comm_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_comm_proto_msgTypes = make([]protoimpl.MessageInfo, 16) +var file_comm_proto_msgTypes = make([]protoimpl.MessageInfo, 17) var file_comm_proto_goTypes = []interface{}{ (HeroAttributesType)(0), // 0: HeroAttributesType (*UserMessage)(nil), // 1: UserMessage @@ -1250,20 +1298,21 @@ var file_comm_proto_goTypes = []interface{}{ (*UserAssets)(nil), // 12: UserAssets (*TaskParam)(nil), // 13: TaskParam (*RtaskParam)(nil), // 14: RtaskParam - (*EmptyReq)(nil), // 15: EmptyReq - (*EmptyResp)(nil), // 16: EmptyResp - (*anypb.Any)(nil), // 17: google.protobuf.Any - (ErrorCode)(0), // 18: ErrorCode + (*UIdReq)(nil), // 15: UIdReq + (*EmptyReq)(nil), // 16: EmptyReq + (*EmptyResp)(nil), // 17: EmptyResp + (*anypb.Any)(nil), // 18: google.protobuf.Any + (ErrorCode)(0), // 19: ErrorCode } var file_comm_proto_depIdxs = []int32{ - 17, // 0: UserMessage.data:type_name -> google.protobuf.Any - 17, // 1: AgentMessage.Message:type_name -> google.protobuf.Any - 18, // 2: RPCMessageReply.Code:type_name -> ErrorCode - 17, // 3: RPCMessageReply.ErrorData:type_name -> google.protobuf.Any + 18, // 0: UserMessage.data:type_name -> google.protobuf.Any + 18, // 1: AgentMessage.Message:type_name -> google.protobuf.Any + 19, // 2: RPCMessageReply.Code:type_name -> ErrorCode + 18, // 3: RPCMessageReply.ErrorData:type_name -> google.protobuf.Any 1, // 4: RPCMessageReply.Reply:type_name -> UserMessage 1, // 5: AgentSendMessageReq.Reply:type_name -> UserMessage - 17, // 6: BatchMessageReq.Data:type_name -> google.protobuf.Any - 17, // 7: BroadCastMessageReq.Data:type_name -> google.protobuf.Any + 18, // 6: BatchMessageReq.Data:type_name -> google.protobuf.Any + 18, // 7: BroadCastMessageReq.Data:type_name -> google.protobuf.Any 8, // [8:8] is the sub-list for method output_type 8, // [8:8] is the sub-list for method input_type 8, // [8:8] is the sub-list for extension type_name @@ -1447,7 +1496,7 @@ func file_comm_proto_init() { } } file_comm_proto_msgTypes[14].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*EmptyReq); i { + switch v := v.(*UIdReq); i { case 0: return &v.state case 1: @@ -1459,6 +1508,18 @@ func file_comm_proto_init() { } } file_comm_proto_msgTypes[15].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*EmptyReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_comm_proto_msgTypes[16].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*EmptyResp); i { case 0: return &v.state @@ -1477,7 +1538,7 @@ func file_comm_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_comm_proto_rawDesc, NumEnums: 1, - NumMessages: 16, + NumMessages: 17, NumExtensions: 0, NumServices: 0, },