rpc

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 38 Imported by: 7

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",
		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",
	}
)
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 BorderAllServiceRPCMessageByContextNotCheck added in v1.0.11

func BorderAllServiceRPCMessageByContextNotCheck[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 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 NewRPCXServerHandler added in v1.0.9

func NewRPCXServerHandler() server.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 SendNoReplyRPCMessageByAddress added in v1.0.9

func SendNoReplyRPCMessageByAddress(moduleName, address, serviceName string, args interface{}) 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 SendRPCMessageByStr added in v1.0.11

func SendRPCMessageByStr(ct context.Context, moduleName, serviceName string, args, reply interface{}) error

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

func ServeAdmin added in v1.0.9

func ServeAdmin(port string) (r chan bool)

Types

type Admin added in v1.0.9

type Admin struct {
	Module
	// contains filtered or unexported fields
}

func (*Admin) Destroy added in v1.0.9

func (a *Admin) Destroy(sub IService)

func (*Admin) GetName added in v1.0.9

func (a *Admin) GetName() string

func (*Admin) GetUserHook added in v1.0.9

func (a *Admin) GetUserHook() IUserHook

func (*Admin) GetVersion added in v1.0.9

func (a *Admin) GetVersion() string

func (*Admin) L added in v1.0.9

func (a *Admin) L(ctx context.Context, args *string, reply *string) (err error)

func (*Admin) Startup added in v1.0.9

func (a *Admin) Startup() (bool, error)

type Args

type Args[T protoreflect.ProtoMessage] 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 ConsulServerInfo added in v1.0.9

type ConsulServerInfo struct {
	State      string
	Version    int32
	SubVersion int32
	NodeId     string
}

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)
	GetUserHook() IUserHook
	Destroy(sub IService)
	GetLogicSyncMethod() []string
}

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 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
	State   client.ConsulServerState
	// contains filtered or unexported fields
}

func (*Module) Destroy

func (m *Module) Destroy(sub IService)

func (*Module) GetLogicSyncMethod added in v1.0.12

func (m *Module) GetLogicSyncMethod() []string

func (*Module) GetName added in v1.0.16

func (m *Module) GetName() string

func (*Module) GetUserHook added in v1.0.7

func (m *Module) GetUserHook() IUserHook

func (*Module) GetVersion added in v1.0.16

func (m *Module) GetVersion() string

func (*Module) StateHandler added in v1.0.9

func (m *Module) StateHandler(ctx context.Context, args *client.ConsulServerState, reply *string) (err error)

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 OfflineReq struct {
	UserId string
	//是否重复登录踢人行为
	Replace bool
}

type OfflineRes

type OfflineRes struct {
	ErrorCode int32
}

type Optional

type Optional func(*Server)

type Reply

type Reply[T protoreflect.ProtoMessage] 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
	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) 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) *Server

func (*Server) WithGatewayWSS added in v1.0.9

func (s *Server) WithGatewayWSS(port, path, key, cert string) *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) 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) 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) 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) 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"`
	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 {
}

func (*XClientHandler) PostCall added in v1.0.9

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

func (*XClientHandler) PreCall added in v1.0.9

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

type XServerHandler added in v1.0.9

type XServerHandler struct {
}

func (*XServerHandler) PostCall added in v1.0.9

func (r *XServerHandler) PostCall(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, err error) (result interface{}, e error)

func (*XServerHandler) PostReadRequest added in v1.0.9

func (r *XServerHandler) PostReadRequest(ctx context.Context, m *protocol.Message, e error) error

PostReadRequest counts read

func (*XServerHandler) PreCall added in v1.0.9

func (r *XServerHandler) PreCall(ctx context.Context, serviceName, methodName string, args interface{}) (result interface{}, e error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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