go_dreamfactory/comm/core.go
2022-06-15 11:12:58 +08:00

108 lines
3.2 KiB
Go

package comm
import (
"context"
"go_dreamfactory/pb"
"reflect"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
)
const (
SC_ServiceGateRouteComp core.S_Comps = "SC_GateRouteComp" //s_comps.ISC_GateRouteComp
)
const (
Service_Gateway = "gateway"
Service_Worker = "worker"
)
//模块名定义处
const (
SM_GateModule core.M_Modules = "gateway" //gate模块 网关服务模块
SM_WebModule core.M_Modules = "web" //web模块
SM_UserModule core.M_Modules = "user" //用户模块
SM_PackModule core.M_Modules = "pack" //背包模块
SM_MailModule core.M_Modules = "mail" //邮件模块
SM_FriendModule core.M_Modules = "friend" //好友模块
SM_LogModelModule core.M_Modules = "model" //日志模块
)
//RPC服务接口定义处
const ( //Rpc
Rpc_GatewayRoute core.Rpc_Key = "Rpc_GatewayRoute" //网关路由
Rpc_GatewayAgentBind core.Rpc_Key = "Rpc_GatewayAgentBind" //代理绑定 绑定用户Id
Rpc_GatewayAgentUnBind core.Rpc_Key = "Rpc_GatewayAgentUnBind" //代理解绑 解绑用户Id
Rpc_GatewayAgentSendMsg core.Rpc_Key = "Rpc_GatewayAgentSendMsg" //代理发送消息 向用户发送消息
Rpc_GatewaySendBatchMsg core.Rpc_Key = "Rpc_GatewaySendBatchMsg" //向多个用户发送消息
Rpc_GatewaySendRadioMsg core.Rpc_Key = "Rpc_GatewaySendRadioMsg" //广播消息
Rpc_GatewayAgentClose core.Rpc_Key = "Rpc_GatewayAgentClose" //代理关闭 关闭用户连接
)
//事件类型定义处
const (
Event_UserLogin core.Event_Key = "Event_UserLogin" //登录事件
Event_CreateUser core.Event_Key = "Event_CreateUser" //创建角色事件
Event_UserOffline core.Event_Key = "Event_UserOffline" //用户离线事件
)
// 服务网关组件接口定义
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, fn reflect.Method)
RegisterRouteCheck(methodName string, comp reflect.Value, msg reflect.Type, fn reflect.Method)
}
type Autogenerated struct {
ID string `json:"ID,omitempty" bson:"_id"`
UID string `json:"uid"`
Act string `json:"act"` // insert update delete
D []interface{}
}
type LogHandleType string
const (
LogHandleType_Insert LogHandleType = "insert"
LogHandleType_Update LogHandleType = "update"
LogHandleType_Delete LogHandleType = "delete"
)
//用户会话
type IUserSession interface {
GetSessionId() string
GetUserId() string
GetIP() string
GetGatewayServiceId() string
IsLogin() bool
Bind(uid string, wokerId string) (err error)
UnBind() (err error)
SendMsg(mainType, subType string, code pb.ErrorCode, msg proto.Message) (err error)
Close() (err error)
ToString() string
}
func ProtoDecode(msg *pb.UserMessage, req proto.Message) (ok bool) {
err := ptypes.UnmarshalAny(msg.Data, req)
if err != nil {
log.Errorf("%s.%s %v", msg.MainType, msg.SubType, err)
return
}
return true
}
func ProtoEncode(rsp proto.Message, msg *pb.UserMessage) (ok bool) {
data, err := ptypes.MarshalAny(rsp)
if err != nil {
log.Errorf("%s.%s %v", msg.MainType, msg.SubType, err)
return
}
msg.Data = data
return true
}