Documentation
¶
Overview ¶
Package rpc @Description: rpc的封装,使用了rpcx框架 @Link: https://doc.rpcx.io/part4/alias.html rpcx框架 @Ref:
Index ¶
- Variables
- func BorderAllServiceRPCMessageByContext[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res])
- func BorderRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res])
- func CorsMiddleware(handler http.HandlerFunc) http.HandlerFunc
- func GetNodeId(ctx context.Context) string
- func GetTemplateUserId(ctx context.Context) string
- func GetUserId(ctx context.Context) string
- func NewBindRPCContext(userId ...string) context.Context
- func NewCacheUserContext(userId string) context.Context
- func NewCustomSelector(moduleName string) client2.Selector
- func NewRPCContext() context.Context
- func NewRPCXClientHandler() client2.PostCallPlugin
- func NewRPCXServerHandler() server.PostCallPlugin
- func NewUserRPCContext(userId string) context.Context
- func SendNoReplyRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res]) error
- func SendNoReplyRPCMessageByAddress(moduleName, address, serviceName string, args interface{}) error
- func SendRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res]) (res Res, err error)
- func SendToGate(ct context.Context, messageType string, pbMessage proto.Message) error
- func SendToGateByUserId(userId, messageType string, pbMessage proto.Message) error
- func ServeAdmin()
- type Admin
- type Args
- type Call
- type Client
- type ClientOptional
- type ConsulServerInfo
- type CustomSelector
- type DefaultArgs
- type DefaultBool
- type DefaultReply
- type EmptyReply
- type GateService
- func (g *GateService) GetName() string
- func (g *GateService) GetVersion() string
- func (g *GateService) Login(ctx context.Context, args *LoginReq, reply *LoginRes) error
- func (g *GateService) Offline(ctx context.Context, args *OfflineReq, reply *OfflineRes) error
- func (g *GateService) Startup() (bool, error)
- func (g *GateService) ToUser(ctx context.Context, args *ToUserReq, reply *ToUserRes) error
- func (g *GateService) UploadUserNodeInfo(ctx context.Context, args *UploadUserNodeInfoReq, reply *UploadUserNodeInfoRes) error
- type HeaderMessageType
- type ILoginCheck
- type IService
- type ITCPBuilder
- type ITCPService
- type IUserConnectData
- type IUserHook
- type LoginReq
- type LoginRes
- type Module
- type MonitorService
- type OfflineReq
- type OfflineRes
- type Optional
- type Reply
- type RequestData
- type RequestHeader
- type ResponseHeader
- type Server
- func (s *Server) Destroy()
- func (s *Server) Run() chan bool
- func (s *Server) WithCache(module tgf.CacheModule) *Server
- func (s *Server) WithCustomServiceAddress()
- func (s *Server) WithGameConfig(path string) *Server
- func (s *Server) WithGateway(port string, hook IUserHook) *Server
- func (s *Server) WithGatewayWS(port, path string) *Server
- func (s *Server) WithGatewayWSS(port, path, key, cert string) *Server
- func (s *Server) WithProfileDebug() *Server
- func (s *Server) WithRandomServicePort(minPort, maxPort int32) *Server
- func (s *Server) WithServerPool(maxWorkers, maxCapacity int) *Server
- func (s *Server) WithService(service IService) *Server
- func (s *Server) WithWhiteService(serviceName string) *Server
- type ServerConfig
- func (s *ServerConfig) Address() string
- func (s *ServerConfig) DeadLineTime() time.Duration
- func (s *ServerConfig) IsWebSocket() bool
- func (s *ServerConfig) IsWss() bool
- func (s *ServerConfig) MaxConnections() int32
- func (s *ServerConfig) Port() string
- func (s *ServerConfig) ReadBufferSize() int
- func (s *ServerConfig) SetUserHook(userHook IUserHook)
- func (s *ServerConfig) UserHook() IUserHook
- func (s *ServerConfig) WithBuffer(readBuffer, writeBuffer int) ITCPBuilder
- func (s *ServerConfig) WithPort(port string) ITCPBuilder
- func (s *ServerConfig) WithWSPath(path string) ITCPBuilder
- func (s *ServerConfig) WithWss(certFile, keyFile string) ITCPBuilder
- func (s *ServerConfig) WriteBufferSize() int
- func (s *ServerConfig) WsPath() string
- func (s *ServerConfig) WssCertFile() string
- func (s *ServerConfig) WssKeyFile() string
- type ServiceAPI
- type TCPServer
- func (t *TCPServer) DoLogin(userId, templateUserId string) (err error)
- func (t *TCPServer) Offline(userId string, replace bool) (exists bool)
- func (t *TCPServer) Run()
- func (t *TCPServer) SetUserHook(userHook IUserHook)
- func (t *TCPServer) ToUser(userId, messageType string, data []byte)
- func (t *TCPServer) Update()
- func (t *TCPServer) UpdateUserNodeInfo(userId, servicePath, nodeId string) bool
- type ToUserReq
- type ToUserRes
- type UploadUserNodeInfoReq
- type UploadUserNodeInfoRes
- type UserConnectData
- func (u *UserConnectData) GetChannel() chan *client.Call
- func (u *UserConnectData) GetContextData() *share.Context
- func (u *UserConnectData) IsLogin() bool
- func (u *UserConnectData) Login(userId string)
- func (u *UserConnectData) Offline(userHook IUserHook)
- func (u *UserConnectData) Send(data []byte)
- func (u *UserConnectData) StartReq()
- func (u *UserConnectData) Stop()
- func (u *UserConnectData) UpdateUserNodeId(servicePath, nodeId string)
- type UserHook
- func (u *UserHook) AddLoginHook(hook *ServiceAPI[*DefaultArgs, *EmptyReply]) IUserHook
- func (u *UserHook) AddOfflineHook(hook *ServiceAPI[*DefaultArgs, *EmptyReply]) IUserHook
- func (u *UserHook) GetLoginHooks() []*ServiceAPI[*DefaultArgs, *EmptyReply]
- func (u *UserHook) GetOfflineHooks() []*ServiceAPI[*DefaultArgs, *EmptyReply]
- type WSMessage
- func (*WSMessage) Descriptor() ([]byte, []int)deprecated
- func (x *WSMessage) GetData() []byte
- func (x *WSMessage) GetModule() string
- func (x *WSMessage) GetReqId() int32
- func (x *WSMessage) GetServiceName() string
- func (*WSMessage) ProtoMessage()
- func (x *WSMessage) ProtoReflect() protoreflect.Message
- func (x *WSMessage) Reset()
- func (x *WSMessage) String() string
- type WSResponse
- func (*WSResponse) Descriptor() ([]byte, []int)deprecated
- func (x *WSResponse) GetCode() int32
- func (x *WSResponse) GetData() []byte
- func (x *WSResponse) GetMessageType() string
- func (x *WSResponse) GetReqId() int32
- func (x *WSResponse) GetZip() bool
- func (*WSResponse) ProtoMessage()
- func (x *WSResponse) ProtoReflect() protoreflect.Message
- func (x *WSResponse) Reset()
- func (x *WSResponse) String() string
- type XClientHandler
- type XServerHandler
- func (r *XServerHandler) PostCall(ctx context.Context, servicePath, serviceMethod string, args interface{}, ...) (result interface{}, e error)
- func (r *XServerHandler) PostReadRequest(ctx context.Context, m *protocol.Message, e error) error
- func (r *XServerHandler) PreCall(ctx context.Context, serviceName, methodName string, args interface{}) (result interface{}, e error)
Constants ¶
This section is empty.
Variables ¶
var ( UploadUserNodeInfo = &ServiceAPI[*UploadUserNodeInfoReq, *UploadUserNodeInfoRes]{ ModuleName: Gate.Name, Name: "UploadUserNodeInfo", MessageType: Gate.Name + "." + "UploadUserNodeInfo", } ToUser = &ServiceAPI[*ToUserReq, *ToUserRes]{ ModuleName: Gate.Name, Name: "ToUser", MessageType: Gate.Name + "." + "ToUser", } Login = &ServiceAPI[*LoginReq, *LoginRes]{ ModuleName: Gate.Name, Name: "Login", MessageType: Gate.Name + "." + "Login", } Offline = &ServiceAPI[*OfflineReq, *OfflineRes]{ ModuleName: Gate.Name, Name: "Offline", MessageType: Gate.Name + "." + "Offline", } )
var File_ws_proto protoreflect.FileDescriptor
var Gate = &Module{Name: "gate", Version: "1.0"}
Functions ¶
func BorderAllServiceRPCMessageByContext ¶
func BorderAllServiceRPCMessageByContext[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res])
func BorderRPCMessage ¶
func BorderRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res])
BorderRPCMessage [Req any, Res any]
@Description: 推送消息到所有服务节点 @param ct @param api @param Res]
func CorsMiddleware ¶ added in v1.0.9
func CorsMiddleware(handler http.HandlerFunc) http.HandlerFunc
func GetTemplateUserId ¶
func NewBindRPCContext ¶
NewBindRPCContext @Description: instantiate rpc context with binding, all user binging same node id. @param userId @return context.Context
func NewCacheUserContext ¶
func NewCustomSelector ¶
func NewRPCContext ¶
func NewRPCXClientHandler ¶
func NewRPCXClientHandler() client2.PostCallPlugin
func NewRPCXServerHandler ¶ added in v1.0.9
func NewRPCXServerHandler() server.PostCallPlugin
func NewUserRPCContext ¶
NewUserRPCContext @Description: instantiate rpc context with user id @param userId @return context.Context
func SendNoReplyRPCMessage ¶
SendNoReplyRPCMessage [Req any, Res any]
@Description: 发送无需等待返回的rpc消息 @param ct @param api @param Res @return error
func SendNoReplyRPCMessageByAddress ¶ added in v1.0.9
func SendRPCMessage ¶
func SendRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res]) (res Res, err error)
SendRPCMessage [Req, Res any]
@Description: 远程rpc调用 @param ct @param api @param Res @return res @return err
func SendToGate ¶
SendToGate @Description: 发送消息到用户所在的网关 @param ct @param pbMessage @return error
func SendToGateByUserId ¶
SendToGateByUserId @Description: 根据用户id发送消息到用户所在的网关 @param userId @param pbMessage @return error
func ServeAdmin ¶ added in v1.0.9
func ServeAdmin()
Types ¶
type Admin ¶ added in v1.0.9
type Admin struct { Module // contains filtered or unexported fields }
func (*Admin) GetUserHook ¶ added in v1.0.9
func (*Admin) GetVersion ¶ added in v1.0.9
type Args ¶
type Args[T protoreflect.ProtoMessage] struct { ByteData []byte }
type Call ¶
type Call struct {
// contains filtered or unexported fields
}
func SendAsyncRPCMessage ¶
func SendAsyncRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res]) (*Call, error)
SendAsyncRPCMessage [Req, Res any] @Description: 异步rpc请求,使用该接口时,需要确保call中的chan被消费, 避免chan的泄露 @param ct @param api @return *client.Call @return error
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) AddWhiteService ¶
func (*Client) CheckWhiteList ¶
type ClientOptional ¶
type ClientOptional struct { }
type ConsulServerInfo ¶ added in v1.0.9
type CustomSelector ¶
type CustomSelector struct {
// contains filtered or unexported fields
}
func (*CustomSelector) Select ¶
func (c *CustomSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) (selected string)
func (*CustomSelector) UpdateServer ¶
func (c *CustomSelector) UpdateServer(servers map[string]string)
type DefaultArgs ¶
type DefaultArgs struct {
C string
}
type DefaultBool ¶
type DefaultBool struct {
C bool
}
type DefaultReply ¶
type DefaultReply struct {
C int32
}
type EmptyReply ¶
type EmptyReply struct { }
type GateService ¶
type GateService struct { Module // contains filtered or unexported fields }
GateService @Description: 默认网关
func (*GateService) GetName ¶
func (g *GateService) GetName() string
func (*GateService) GetVersion ¶
func (g *GateService) GetVersion() string
func (*GateService) Offline ¶
func (g *GateService) Offline(ctx context.Context, args *OfflineReq, reply *OfflineRes) error
func (*GateService) Startup ¶
func (g *GateService) Startup() (bool, error)
func (*GateService) UploadUserNodeInfo ¶
func (g *GateService) UploadUserNodeInfo(ctx context.Context, args *UploadUserNodeInfoReq, reply *UploadUserNodeInfoRes) error
type HeaderMessageType ¶
type HeaderMessageType byte
const ( Heartbeat HeaderMessageType = iota + 1 Logic ReplaceLogin )
type ILoginCheck ¶
type IService ¶
type IService interface { GetName() string GetVersion() string Startup() (bool, error) GetUserHook() IUserHook Destroy(sub IService) }
IService
@Description: 逻辑服务接口
func GatewayService ¶
func GatewayService(tcpBuilder ITCPBuilder) IService
type ITCPBuilder ¶
type ITCPBuilder interface { WithPort(port string) ITCPBuilder WithBuffer(readBuffer, writeBuffer int) ITCPBuilder WithWSPath(path string) ITCPBuilder Address() string Port() string WsPath() string MaxConnections() int32 DeadLineTime() time.Duration ReadBufferSize() int WriteBufferSize() int IsWebSocket() bool SetUserHook(userHook IUserHook) UserHook() IUserHook WithWss(certFile, keyFile string) ITCPBuilder IsWss() bool WssCertFile() string WssKeyFile() string }
type ITCPService ¶
type IUserConnectData ¶
type IUserHook ¶
type IUserHook interface { GetLoginHooks() []*ServiceAPI[*DefaultArgs, *EmptyReply] GetOfflineHooks() []*ServiceAPI[*DefaultArgs, *EmptyReply] AddLoginHook(hook *ServiceAPI[*DefaultArgs, *EmptyReply]) IUserHook AddOfflineHook(hook *ServiceAPI[*DefaultArgs, *EmptyReply]) IUserHook }
func NewUserHook ¶
func NewUserHook() IUserHook
type Module ¶
type Module struct { Name string Version string State client.ConsulServerState // contains filtered or unexported fields }
func (*Module) GetUserHook ¶ added in v1.0.7
func (*Module) StateHandler ¶ added in v1.0.9
type MonitorService ¶ added in v1.0.9
type MonitorService struct {
Module
}
func (*MonitorService) ASyncMonitor ¶ added in v1.0.9
func (m *MonitorService) ASyncMonitor(ctx context.Context, args *string, reply *admin.NodeMonitorData) (err error)
func (*MonitorService) GetName ¶ added in v1.0.9
func (m *MonitorService) GetName() string
func (*MonitorService) GetVersion ¶ added in v1.0.9
func (m *MonitorService) GetVersion() string
func (*MonitorService) Startup ¶ added in v1.0.9
func (m *MonitorService) Startup() (bool, error)
type OfflineReq ¶
type OfflineRes ¶
type OfflineRes struct {
ErrorCode int32
}
type Reply ¶
type Reply[T protoreflect.ProtoMessage] struct { ByteData []byte Code int32 }
type RequestData ¶
type RequestData struct { User *UserConnectData RequestMethod string Module string Data []byte MessageType HeaderMessageType ReqId int32 StartTime time.Time }
type RequestHeader ¶
type RequestHeader []byte
RequestHeader [1][1][2][2][n][n] magic number|message type|request method name size|data size|method name|data
type ResponseHeader ¶
type ResponseHeader []byte
ResponseHeader [1][1][2][4][n][n] message type|compress|request method name size|data size|method name|data
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server @Description:
func NewRPCServer ¶
func NewRPCServer() *Server
func (*Server) WithCustomServiceAddress ¶
func (s *Server) WithCustomServiceAddress()
WithCustomServiceAddress @Description: 开启自定义地址注册,通过常量ServiceAddress注册绑定的ip @receiver this
func (*Server) WithGameConfig ¶
func (*Server) WithGatewayWS ¶
func (*Server) WithGatewayWSS ¶ added in v1.0.9
func (*Server) WithProfileDebug ¶
func (*Server) WithRandomServicePort ¶
func (*Server) WithServerPool ¶
func (*Server) WithService ¶
func (*Server) WithWhiteService ¶
type ServerConfig ¶
type ServerConfig struct {
// contains filtered or unexported fields
}
func (*ServerConfig) Address ¶
func (s *ServerConfig) Address() string
func (*ServerConfig) DeadLineTime ¶
func (s *ServerConfig) DeadLineTime() time.Duration
func (*ServerConfig) IsWebSocket ¶
func (s *ServerConfig) IsWebSocket() bool
func (*ServerConfig) IsWss ¶ added in v1.0.9
func (s *ServerConfig) IsWss() bool
func (*ServerConfig) MaxConnections ¶
func (s *ServerConfig) MaxConnections() int32
func (*ServerConfig) Port ¶
func (s *ServerConfig) Port() string
func (*ServerConfig) ReadBufferSize ¶
func (s *ServerConfig) ReadBufferSize() int
func (*ServerConfig) SetUserHook ¶
func (s *ServerConfig) SetUserHook(userHook IUserHook)
func (*ServerConfig) UserHook ¶
func (s *ServerConfig) UserHook() IUserHook
func (*ServerConfig) WithBuffer ¶
func (s *ServerConfig) WithBuffer(readBuffer, writeBuffer int) ITCPBuilder
func (*ServerConfig) WithPort ¶
func (s *ServerConfig) WithPort(port string) ITCPBuilder
func (*ServerConfig) WithWSPath ¶
func (s *ServerConfig) WithWSPath(path string) ITCPBuilder
func (*ServerConfig) WithWss ¶ added in v1.0.9
func (s *ServerConfig) WithWss(certFile, keyFile string) ITCPBuilder
func (*ServerConfig) WriteBufferSize ¶
func (s *ServerConfig) WriteBufferSize() int
func (*ServerConfig) WsPath ¶
func (s *ServerConfig) WsPath() string
func (*ServerConfig) WssCertFile ¶ added in v1.0.9
func (s *ServerConfig) WssCertFile() string
func (*ServerConfig) WssKeyFile ¶ added in v1.0.9
func (s *ServerConfig) WssKeyFile() string
type ServiceAPI ¶
type ServiceAPI[Req, Res any] struct { ModuleName string Name string MessageType string Des string // contains filtered or unexported fields }
func (*ServiceAPI[Req, Res]) GetResult ¶
func (s *ServiceAPI[Req, Res]) GetResult() Res
func (*ServiceAPI[Req, Res]) New ¶
func (s *ServiceAPI[Req, Res]) New(req Req, res Res) *ServiceAPI[Req, Res]
func (*ServiceAPI[Req, Res]) NewEmpty ¶
func (s *ServiceAPI[Req, Res]) NewEmpty() *ServiceAPI[Req, Res]
func (*ServiceAPI[Req, Res]) NewRPC ¶ added in v1.0.9
func (s *ServiceAPI[Req, Res]) NewRPC(req Req) *ServiceAPI[Req, Res]
type TCPServer ¶
type TCPServer struct {
// contains filtered or unexported fields
}
func (*TCPServer) SetUserHook ¶
func (*TCPServer) UpdateUserNodeInfo ¶
type UploadUserNodeInfoReq ¶
type UploadUserNodeInfoRes ¶
type UploadUserNodeInfoRes struct {
ErrorCode int32
}
type UserConnectData ¶
type UserConnectData struct {
// contains filtered or unexported fields
}
func (*UserConnectData) GetChannel ¶
func (u *UserConnectData) GetChannel() chan *client.Call
func (*UserConnectData) GetContextData ¶
func (u *UserConnectData) GetContextData() *share.Context
func (*UserConnectData) IsLogin ¶
func (u *UserConnectData) IsLogin() bool
func (*UserConnectData) Login ¶
func (u *UserConnectData) Login(userId string)
func (*UserConnectData) Offline ¶
func (u *UserConnectData) Offline(userHook IUserHook)
func (*UserConnectData) Send ¶
func (u *UserConnectData) Send(data []byte)
func (*UserConnectData) StartReq ¶ added in v1.0.9
func (u *UserConnectData) StartReq()
func (*UserConnectData) Stop ¶
func (u *UserConnectData) Stop()
func (*UserConnectData) UpdateUserNodeId ¶
func (u *UserConnectData) UpdateUserNodeId(servicePath, nodeId string)
type UserHook ¶
type UserHook struct {
// contains filtered or unexported fields
}
func (*UserHook) AddLoginHook ¶
func (u *UserHook) AddLoginHook(hook *ServiceAPI[*DefaultArgs, *EmptyReply]) IUserHook
func (*UserHook) AddOfflineHook ¶
func (u *UserHook) AddOfflineHook(hook *ServiceAPI[*DefaultArgs, *EmptyReply]) IUserHook
func (*UserHook) GetLoginHooks ¶
func (u *UserHook) GetLoginHooks() []*ServiceAPI[*DefaultArgs, *EmptyReply]
func (*UserHook) GetOfflineHooks ¶
func (u *UserHook) GetOfflineHooks() []*ServiceAPI[*DefaultArgs, *EmptyReply]
type WSMessage ¶
type WSMessage struct { Module string `protobuf:"bytes,1,opt,name=module,proto3" json:"module,omitempty"` ServiceName string `protobuf:"bytes,2,opt,name=serviceName,proto3" json:"serviceName,omitempty"` Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` ReqId int32 `protobuf:"varint,4,opt,name=reqId,proto3" json:"reqId,omitempty"` // contains filtered or unexported fields }
func (*WSMessage) Descriptor
deprecated
func (*WSMessage) GetServiceName ¶
func (*WSMessage) ProtoMessage ¶
func (*WSMessage) ProtoMessage()
func (*WSMessage) ProtoReflect ¶
func (x *WSMessage) ProtoReflect() protoreflect.Message
type WSResponse ¶
type WSResponse struct { MessageType string `protobuf:"bytes,1,opt,name=messageType,proto3" json:"messageType,omitempty"` Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` ReqId int32 `protobuf:"varint,3,opt,name=reqId,proto3" json:"reqId,omitempty"` Code int32 `protobuf:"varint,4,opt,name=code,proto3" json:"code,omitempty"` Zip bool `protobuf:"varint,5,opt,name=zip,proto3" json:"zip,omitempty"` // contains filtered or unexported fields }
func (*WSResponse) Descriptor
deprecated
func (*WSResponse) Descriptor() ([]byte, []int)
Deprecated: Use WSResponse.ProtoReflect.Descriptor instead.
func (*WSResponse) GetCode ¶
func (x *WSResponse) GetCode() int32
func (*WSResponse) GetData ¶
func (x *WSResponse) GetData() []byte
func (*WSResponse) GetMessageType ¶
func (x *WSResponse) GetMessageType() string
func (*WSResponse) GetReqId ¶
func (x *WSResponse) GetReqId() int32
func (*WSResponse) GetZip ¶ added in v1.0.5
func (x *WSResponse) GetZip() bool
func (*WSResponse) ProtoMessage ¶
func (*WSResponse) ProtoMessage()
func (*WSResponse) ProtoReflect ¶
func (x *WSResponse) ProtoReflect() protoreflect.Message
func (*WSResponse) Reset ¶
func (x *WSResponse) Reset()
func (*WSResponse) String ¶
func (x *WSResponse) String() string
type XClientHandler ¶ added in v1.0.9
type XClientHandler struct { }
type XServerHandler ¶ added in v1.0.9
type XServerHandler struct { }
func (*XServerHandler) PostReadRequest ¶ added in v1.0.9
PostReadRequest counts read