package comm import ( "context" "go_dreamfactory/pb" "reflect" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" ) // 服务网关组件接口定义 type ISC_GateRouteComp interface { core.IServiceComp ReceiveMsg(ctx context.Context, args *pb.AgentMessage, reply *pb.RPCMessageReply) error RegisterRoute(methodName string, comp reflect.Value, msg reflect.Type, handle reflect.Method) GetUserSession(udata *pb.CacheUser) (session IUserSession) PutUserSession(session IUserSession) } //游戏类资源类型 const ( AttrType = "attr" //用户属性资源 例如货币 经验 之类的 ItemType = "item" //道具物品资源 CardType = "card" //卡片资源 例如英雄卡,检验卡 EquipmentType = "equi" //武器资源 ) type Autogenerated struct { ID string `json:"ID,omitempty" bson:"_id"` UID string `json:"uid"` Act string `json:"act"` // insert update delete D []interface{} } const ( HeroStarLvRatio int32 = 10 // 卡牌等级上限系数(星级*10) ) //Api Check 错误返回结构 type ErrorCode struct { Code pb.ErrorCode Data interface{} } //用户会话 type IUserSession interface { SetSession(ip, sessionId, gatewayServiceId, uid string) GetSessionId() string GetUserId() string GetIP() string GetGatewayServiceId() string IsLogin() bool Bind(uid string, wokerId string) (err error) UnBind() (err error) SendMsg(mainType, subType string, msg proto.Message) (err error) Polls() []*pb.UserMessage Push() (err error) //警告 api传递过来的会话禁用此接口 Close() (err error) Reset() ToString() string } //对protobuf格式的数据进行反序列化 func ProtoUnmarshal(msg *pb.UserMessage, req proto.Message) (ok bool) { err := msg.Data.UnmarshalTo(req) if err != nil { log.Errorf("UnmarshalTo %s.%s %v", msg.MainType, msg.SubType, err) return } return true } //对protobuf格式的数据进行序列化 func ProtoMarshal(rsp proto.Message, msg *pb.UserMessage) (ok bool) { any, err := anypb.New(rsp) if err != nil { log.Errorf("Any New %s.%s %v", msg.MainType, msg.SubType, err) return } msg.Data = any return true }