rpc

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2023 License: Apache-2.0 Imports: 34 Imported by: 9

Documentation

Overview

Package rpc @Description: rpc的封装,使用了rpcx框架 @Link: https://doc.rpcx.io/part4/alias.html rpcx框架 @Ref:

Index

Constants

This section is empty.

Variables

View Source
var (
	UploadUserNodeInfo = &ServiceAPI[*UploadUserNodeInfoReq, *UploadUserNodeInfoRes]{
		ModuleName: Gate.Name,
		Name:       "UploadUserNodeInfo",
	}

	ToUser = &ServiceAPI[*ToUserReq, *ToUserRes]{
		ModuleName: Gate.Name,
		Name:       "ToUser",
	}

	Login = &ServiceAPI[*LoginReq, *LoginRes]{
		ModuleName: Gate.Name,
		Name:       "Login",
	}

	Offline = &ServiceAPI[*OfflineReq, *OfflineRes]{
		ModuleName: Gate.Name,
		Name:       "Offline",
	}
)
View Source
var File_Users_AUSA_Documents_IdeaProject_tgf_example_kit_proto_ws_proto protoreflect.FileDescriptor
View Source
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 GetNodeId

func GetNodeId(ctx context.Context) string

func GetTemplateUserId

func GetTemplateUserId(ctx context.Context) string

func GetUserId

func GetUserId(ctx context.Context) string

func NewBindRPCContext

func NewBindRPCContext(userId ...string) context.Context

NewBindRPCContext @Description: instantiate rpc context with binding, all user binging same node id. @param userId @return context.Context

func NewCacheUserContext

func NewCacheUserContext(userId string) context.Context

func NewCustomSelector

func NewCustomSelector(moduleName string) client2.Selector

func NewRPCContext

func NewRPCContext() context.Context

func NewRPCXClientHandler

func NewRPCXClientHandler() client2.PostCallPlugin

func NewUserRPCContext

func NewUserRPCContext(userId string) context.Context

NewUserRPCContext @Description: instantiate rpc context with user id @param userId @return context.Context

func SendNoReplyRPCMessage

func SendNoReplyRPCMessage[Req any, Res any](ct context.Context, api *ServiceAPI[Req, Res]) error

SendNoReplyRPCMessage [Req any, Res any]

@Description: 发送无需等待返回的rpc消息
@param ct
@param api
@param Res
@return error

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

func SendToGate(ct context.Context, messageType string, pbMessage proto.Message) error

SendToGate @Description: 发送消息到用户所在的网关 @param ct @param pbMessage @return error

func SendToGateByUserId

func SendToGateByUserId(userId, messageType string, pbMessage proto.Message) error

SendToGateByUserId @Description: 根据用户id发送消息到用户所在的网关 @param userId @param pbMessage @return error

Types

type Args

type Args[T protoiface.MessageV1] struct {
	ByteData []byte
}

func (*Args[T]) GetData

func (a *Args[T]) GetData() (res T)

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

func (*Call) Done

func (this *Call) Done() error

Done

@Description: 会阻塞
@receiver this
@return error

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) AddWhiteService

func (c *Client) AddWhiteService(serviceName string) *Client

func (*Client) CheckWhiteList

func (c *Client) CheckWhiteList(serviceName string) bool

type ClientOptional

type ClientOptional struct {
}

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) Login

func (g *GateService) Login(ctx context.Context, args *LoginReq, reply *LoginRes) error

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) ToUser

func (g *GateService) ToUser(ctx context.Context, args *ToUserReq, reply *ToUserRes) 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 ILoginCheck interface {
	CheckLogin(token string) (bool, string)
}

type IService

type IService interface {
	GetName() string
	GetVersion() string
	Startup() (bool, error)
	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
}

type ITCPService

type ITCPService interface {
	Run()
	UpdateUserNodeInfo(userId, servicePath, nodeId string) bool
	ToUser(userId, messageType string, data []byte)
	DoLogin(userId, templateUserId string) (err error)

	Offline(userId string, replace bool) (exists bool)
}

type IUserConnectData

type IUserConnectData interface {
	UpdateUserNodeId(servicePath, nodeId string)
	GetContextData() *share.Context
	GetChannel() chan *client.Call
	Offline(userHook IUserHook)
	Send(data []byte)
	IsLogin() bool
	Login(userId string)
	Stop()
}

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 LoginReq

type LoginReq struct {
	UserId         string
	TemplateUserId string
}

type LoginRes

type LoginRes struct {
	ErrorCode int32
}

func UserLogin added in v1.0.2

func UserLogin(ctx context.Context, userId string) (*LoginRes, error)

type Module

type Module struct {
	Name    string
	Version string
}

func (*Module) Destroy

func (m *Module) Destroy(sub IService)

type OfflineReq

type OfflineReq struct {
	UserId string
	//是否重复登录踢人行为
	Replace bool
}

type OfflineRes

type OfflineRes struct {
	ErrorCode int32
}

type Optional

type Optional func(*Server)

type RPCXClientHandler

type RPCXClientHandler struct {
}

func (*RPCXClientHandler) PostCall

func (r *RPCXClientHandler) PostCall(ctx context2.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, err error) error

func (*RPCXClientHandler) PreCall

func (r *RPCXClientHandler) PreCall(ctx context2.Context, serviceName, methodName string, args interface{}) (interface{}, error)

type Reply

type Reply[T protoiface.MessageV1] struct {
	ByteData []byte
	Code     int32
}

func (*Reply[T]) SetCode

func (r *Reply[T]) SetCode(code int32)

func (*Reply[T]) SetData

func (r *Reply[T]) SetData(data T) (err error)

type RequestData

type RequestData struct {
	User          *UserConnectData
	RequestMethod string
	Module        string
	Data          []byte
	MessageType   HeaderMessageType
	ReqId         int32
}

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) Destroy

func (s *Server) Destroy()

func (*Server) Run

func (s *Server) Run() chan bool

func (*Server) WithCache

func (s *Server) WithCache(module tgf.CacheModule) *Server

func (*Server) WithCustomServiceAddress

func (s *Server) WithCustomServiceAddress()

WithCustomServiceAddress @Description: 开启自定义地址注册,通过常量ServiceAddress注册绑定的ip @receiver this

func (*Server) WithGameConfig

func (s *Server) WithGameConfig(path string) *Server

func (*Server) WithGateway

func (s *Server) WithGateway(port string, hook IUserHook) *Server

func (*Server) WithGatewayWS

func (s *Server) WithGatewayWS(port, path string, hook IUserHook) *Server

func (*Server) WithProfileDebug

func (s *Server) WithProfileDebug() *Server

func (*Server) WithRandomServicePort

func (s *Server) WithRandomServicePort(minPort, maxPort int32) *Server

func (*Server) WithServerPool

func (s *Server) WithServerPool(maxWorkers, maxCapacity int) *Server

func (*Server) WithService

func (s *Server) WithService(service IService) *Server

func (*Server) WithWhiteService

func (s *Server) WithWhiteService(serviceName string) *Server

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) 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) WriteBufferSize

func (s *ServerConfig) WriteBufferSize() int

func (*ServerConfig) WsPath

func (s *ServerConfig) WsPath() 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]

type TCPServer

type TCPServer struct {
	// contains filtered or unexported fields
}

func (*TCPServer) DoLogin

func (t *TCPServer) DoLogin(userId, templateUserId string) (err error)

func (*TCPServer) Offline

func (t *TCPServer) Offline(userId string, replace bool) (exists bool)

func (*TCPServer) Run

func (t *TCPServer) Run()

func (*TCPServer) SetUserHook

func (t *TCPServer) SetUserHook(userHook IUserHook)

func (*TCPServer) ToUser

func (t *TCPServer) ToUser(userId, messageType string, data []byte)

func (*TCPServer) Update

func (t *TCPServer) Update()

func (*TCPServer) UpdateUserNodeInfo

func (t *TCPServer) UpdateUserNodeInfo(userId, servicePath, nodeId string) bool

type ToUserReq

type ToUserReq struct {
	Data        []byte
	UserId      string
	MessageType string
}

type ToUserRes

type ToUserRes struct {
	ErrorCode int32
}

type UploadUserNodeInfoReq

type UploadUserNodeInfoReq struct {
	UserId      string
	NodeId      string
	ServicePath string
}

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) 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) Descriptor() ([]byte, []int)

Deprecated: Use WSMessage.ProtoReflect.Descriptor instead.

func (*WSMessage) GetData

func (x *WSMessage) GetData() []byte

func (*WSMessage) GetModule

func (x *WSMessage) GetModule() string

func (*WSMessage) GetReqId

func (x *WSMessage) GetReqId() int32

func (*WSMessage) GetServiceName

func (x *WSMessage) GetServiceName() string

func (*WSMessage) ProtoMessage

func (*WSMessage) ProtoMessage()

func (*WSMessage) ProtoReflect

func (x *WSMessage) ProtoReflect() protoreflect.Message

func (*WSMessage) Reset

func (x *WSMessage) Reset()

func (*WSMessage) String

func (x *WSMessage) String() string

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"`
	// 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) 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL