node

package
v0.0.0-...-457ba4f Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

* User: cr-mao Date: 2023/8/19 20:16 Email: crmao@qq.com Desc: config_const.go

* User: cr-mao Date: 2023/8/21 15:03 Email: crmao@qq.com Desc: gate_client.go

* User: cr-mao Date: 2023/8/21 21:34 Email: crmao@qq.com Desc: node_grpc_client.go

* User: cr-mao Date: 2023/8/21 20:47 Email: crmao@qq.com Desc: request.go

* User: cr-mao Date: 2023/8/21 21:07 Email: crmao@qq.com Desc: types.go

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidArgument    = errors.New("ErrInvalidArgument")
	ErrNotFoundUserSource = errors.New("not found user source")
	ErrInvalidGID         = errors.New("invalid gate id")
	ErrInvalidSessionKind = errors.New("invalid session kind")
	ErrInvalidMessage     = errors.New("invalid message")
	ErrInvalidNID         = errors.New("invalid node id")
	ErrReceiveTargetEmpty = errors.New("the receive target is empty")
)

Functions

This section is empty.

Types

type BroadcastArgs

type BroadcastArgs struct {
	Kind    session.Kind // 会话类型,session.Conn 或 session.User
	Message *Message     // 消息
}

type Context

type Context struct {
	Proxy      *Proxy
	Request    *Request
	Middleware *Middleware
	// contains filtered or unexported fields
}

func (*Context) BindGate

func (c *Context) BindGate(uid int64) error

BindGate 绑定网关

func (*Context) BindNode

func (c *Context) BindNode() error

BindNode 绑定节点

func (*Context) Context

func (c *Context) Context() context.Context

Context 获取上线文

func (*Context) Disconnect

func (c *Context) Disconnect(isForce ...bool) error

Disconnect 关闭来自网关的连接

func (*Context) GetIP

func (c *Context) GetIP() (string, error)

GetIP 获取来自网关的连接IP地址

func (*Context) GetNodeId

func (c *Context) GetNodeId() string

func (*Context) Response

func (c *Context) Response(message interface{}) error

Response 对来自网关或节点的请求进行响应(C/S模型) 使用此方法响应的(消息序列号和消息路由号)与请求的(消息序列号和消息路由号)保持一致

func (*Context) UnbindGate

func (c *Context) UnbindGate() error

UnbindGate 解绑网关

func (*Context) UnbindNode

func (c *Context) UnbindNode() error

UnbindNode 解绑节点

type DeliverArgs

type DeliverArgs struct {
	NID     string   // 接收节点。存在接收节点时,消息会直接投递给接收节点;不存在接收节点时,系统定位用户所在节点,然后投递。
	UID     int64    // 用户ID
	Message *Message // 消息
}

type DeliverArgs2

type DeliverArgs2 struct {
	NID     string      // 接收节点。存在接收节点时,消息会直接投递给接收节点;不存在接收节点时,系统定位用户所在节点,然后投递。
	CID     int64       // 连接ID
	UID     int64       // 用户ID
	Message interface{} // 消息
}

type DisconnectArgs

type DisconnectArgs struct {
	GID     string       // 网关ID,会话类型为用户时可忽略此参数
	Kind    session.Kind // 会话类型,session.Conn 或 session.User
	Target  int64        // 会话目标,CID 或 UID
	IsForce bool         // 是否强制断开
}

type Event

type Event struct {
	Proxy *Proxy
	Event int32
	GID   string
	CID   int64
	UID   int64
}

type EventHandler

type EventHandler func(event *Event)

type Events

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

func (*Events) AddEventHandler

func (e *Events) AddEventHandler(event int32, handler EventHandler)

AddEventHandler 添加事件处理器

type GateGrpcClient

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

func NewGateClient

func NewGateClient(cc *grpc.ClientConn) *GateGrpcClient

func (*GateGrpcClient) Bind

func (c *GateGrpcClient) Bind(ctx context.Context, cid, uid int64) (miss bool, err error)

Bind 绑定用户与连接

func (*GateGrpcClient) Broadcast

func (c *GateGrpcClient) Broadcast(ctx context.Context, kind session.Kind, message *packet.Message) (int64, error)

Broadcast 推送广播消息

func (*GateGrpcClient) Disconnect

func (c *GateGrpcClient) Disconnect(ctx context.Context, kind session.Kind, target int64, isForce bool) (miss bool, err error)

Disconnect 断开连接

func (*GateGrpcClient) GetIP

func (c *GateGrpcClient) GetIP(ctx context.Context, kind session.Kind, target int64) (ip string, miss bool, err error)

GetIP 获取客户端IP

func (*GateGrpcClient) Multicast

func (c *GateGrpcClient) Multicast(ctx context.Context, kind session.Kind, targets []int64, message *packet.Message) (int64, error)

Multicast 推送组播消息

func (*GateGrpcClient) Push

func (c *GateGrpcClient) Push(ctx context.Context, kind session.Kind, target int64, message *packet.Message) (miss bool, err error)

Push 推送消息

func (*GateGrpcClient) Stat

func (c *GateGrpcClient) Stat(ctx context.Context, kind session.Kind) (int64, error)

Stat 统计会话总数

func (*GateGrpcClient) Unbind

func (c *GateGrpcClient) Unbind(ctx context.Context, uid int64) (miss bool, err error)

Unbind 解绑用户与连接

type GetIPArgs

type GetIPArgs struct {
	GID    string       // 网关ID,会话类型为用户时可忽略此参数
	Kind   session.Kind // 会话类型,session.Conn 或 session.User
	Target int64        // 会话目标,CID 或 UID
}

type Message

type Message struct {
	Seq   int32       // 序列号
	Route int32       // 路由ID
	Data  interface{} // 消息数据,接收json、proto、[]byte
}

type Middleware

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

func (*Middleware) Next

func (m *Middleware) Next(ctx *Context)

Next 下一个中间件

func (*Middleware) Skip

func (m *Middleware) Skip(ctx *Context, skip int)

Skip 跳过N个中间件

type MiddlewareHandler

type MiddlewareHandler func(ctx *Context)

type MulticastArgs

type MulticastArgs struct {
	GID     string       // 网关ID,会话类型为用户时可忽略此参数
	Kind    session.Kind // 会话类型,session.Conn 或 session.User
	Targets []int64      // 会话目标,CID 或 UID
	Message *Message     // 消息
}

type Node

type Node struct {
	component.Base
	// contains filtered or unexported fields
}

func NewNode

func NewNode(opts ...Option) *Node

func (*Node) Destroy

func (n *Node) Destroy()

Destroy 销毁node服务器

func (*Node) Init

func (n *Node) Init()

Init 初始化节点

func (*Node) Name

func (n *Node) Name() string

Name 组件名称

func (*Node) Proxy

func (n *Node) Proxy() *Proxy

Proxy 获取节点代理

func (*Node) Start

func (n *Node) Start()

Start 启动节点

type NodeGrpcClient

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

func NewNodeClient

func NewNodeClient(cc *grpc.ClientConn) *NodeGrpcClient

func (*NodeGrpcClient) Deliver

func (c *NodeGrpcClient) Deliver(ctx context.Context, args *cluster.DeliverArgs) (miss bool, err error)

Deliver 投递消息

func (*NodeGrpcClient) Trigger

func (c *NodeGrpcClient) Trigger(ctx context.Context, args *cluster.TriggerArgs) (miss bool, err error)

Trigger 触发事件

type Option

type Option func(o *options)

func WithCodec

func WithCodec(codec encoding.Codec) Option

WithCodec 设置编解码器

func WithContext

func WithContext(ctx context.Context) Option

WithContext 设置上下文

func WithID

func WithID(id string) Option

WithID 设置实例ID

func WithLocator

func WithLocator(locator locate.Locator) Option

WithLocator 设置定位器

func WithName

func WithName(name string) Option

WithName 设置实例名称

func WithRegistry

func WithRegistry(r registry.Registry) Option

WithRegistry 设置服务注册器

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置RPC调用超时时间

func WithTransporter

func WithTransporter(transporter *Transport) Option

WithTransporter 设置消息传输器

func WithWeight

func WithWeight(weight int) Option

WithWeight 设置服务权重

type Proxy

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

func (*Proxy) AskGate

func (p *Proxy) AskGate(ctx context.Context, uid int64, gid string) (string, bool, error)

AskGate 检测用户是否在给定的网关上

func (*Proxy) AskNode

func (p *Proxy) AskNode(ctx context.Context, uid int64, nid string) (string, bool, error)

AskNode 检测用户是否在给定的节点上

func (*Proxy) BindGate

func (p *Proxy) BindGate(ctx context.Context, uid int64, gid string, cid int64) error

BindGate 绑定网关

func (*Proxy) BindNode

func (p *Proxy) BindNode(ctx context.Context, uid int64, nid ...string) error

BindNode 绑定节点 单个用户只能被绑定到某一台节点服务器上,多次绑定会直接覆盖上次绑定 绑定操作会通过发布订阅方式同步到网关服务器和其他相关节点服务器上 NID 为需要绑定的节点ID,默认绑定到当前节点上

func (*Proxy) Broadcast

func (p *Proxy) Broadcast(ctx context.Context, args *BroadcastArgs) (int64, error)

Broadcast 推送广播消息

func (*Proxy) Deliver

func (p *Proxy) Deliver(ctx context.Context, args *DeliverArgs) error

Deliver投递消息给节点处理

func (*Proxy) Deliver2

func (p *Proxy) Deliver2(ctx context.Context, args *DeliverArgs2) error

Deliver 投递消息给节点处理

func (*Proxy) Disconnect

func (p *Proxy) Disconnect(ctx context.Context, args *DisconnectArgs) error

Disconnect 断开连接

func (*Proxy) Events

func (p *Proxy) Events() *Events

Events 事件分发器

func (*Proxy) FetchGateList

func (p *Proxy) FetchGateList(ctx context.Context, states ...int32) ([]*registry.ServiceInstance, error)

FetchGateList 拉取网关列表

func (*Proxy) FetchNodeIdListByRoute

func (p *Proxy) FetchNodeIdListByRoute(ctx context.Context, routeIds ...int32) ([]string, error)

获得路由相关的node列表

func (*Proxy) FetchNodeList

func (p *Proxy) FetchNodeList(ctx context.Context, states ...int32) ([]*registry.ServiceInstance, error)

FetchNodeList 拉取节点列表

func (*Proxy) FetchServiceList

func (p *Proxy) FetchServiceList(ctx context.Context, kind string, states ...int32) ([]*registry.ServiceInstance, error)

FetchServiceList 拉取服务列表

func (*Proxy) GetIP

func (p *Proxy) GetIP(ctx context.Context, args *GetIPArgs) (string, error)

GetIP 获取客户端IP

func (*Proxy) GetNodeID

func (p *Proxy) GetNodeID() string

GetNodeID 获取当前节点ID

func (*Proxy) GetNodeName

func (p *Proxy) GetNodeName() string

GetNodeName 获取当前节点名称

func (*Proxy) GetNodeState

func (p *Proxy) GetNodeState() int32

GetNodeState 获取当前节点状态

func (*Proxy) LocateGate

func (p *Proxy) LocateGate(ctx context.Context, uid int64) (string, error)

LocateGate 定位用户所在网关

func (*Proxy) LocateNode

func (p *Proxy) LocateNode(ctx context.Context, uid int64) (string, error)

LocateNode 定位用户所在节点

func (*Proxy) Multicast

func (p *Proxy) Multicast(ctx context.Context, args *MulticastArgs) (int64, error)

Multicast 推送组播消息

func (*Proxy) Push

func (p *Proxy) Push(ctx context.Context, args *PushArgs) error

Push 推送消息

func (*Proxy) Response

func (p *Proxy) Response(ctx context.Context, req *Request, data interface{}) error

Response 响应消息

func (*Proxy) Router

func (p *Proxy) Router() *Router

Router 路由器

func (*Proxy) SetNodeState

func (p *Proxy) SetNodeState(state int32)

SetNodeState 设置当前节点状态

func (*Proxy) UnbindGate

func (p *Proxy) UnbindGate(ctx context.Context, uid int64) error

UnbindGate 解绑网关

func (*Proxy) UnbindNode

func (p *Proxy) UnbindNode(ctx context.Context, uid int64, nid ...string) error

UnbindNode 解绑节点 解绑时会对解绑节点ID进行校验,不匹配则解绑失败 解绑操作会通过发布订阅方式同步到网关服务器和其他相关节点服务器上 NID 为需要解绑的节点ID,默认解绑当前节点

func (*Proxy) WatchServiceInstance

func (p *Proxy) WatchServiceInstance(ctx context.Context, kinds ...string)

WatchServiceInstance 监听服务实例

type PushArgs

type PushArgs struct {
	GID     string       // 网关ID,会话类型为用户时可忽略此参数
	Kind    session.Kind // 会话类型,session.Conn 或 session.User
	Target  int64        // 会话目标,CID 或 UID
	Message *Message     // 消息
}

type Request

type Request struct {
	GID     string   // 来源网关ID
	NID     string   // 来源节点ID
	CID     int64    // 连接ID
	UID     int64    // 用户ID
	Message *Message // 请求消息
	// contains filtered or unexported fields
}

Request 请求数据

func (*Request) Parse

func (r *Request) Parse(v interface{}) error

Parse 解析消息

type RouteHandler

type RouteHandler func(ctx *Context)

type Router

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

func (*Router) AddRouteHandler

func (r *Router) AddRouteHandler(route int32, stateful bool, handler RouteHandler, middlewares ...MiddlewareHandler)

AddRouteHandler 添加路由处理器

func (*Router) CheckRouteStateful

func (r *Router) CheckRouteStateful(route int32) (stateful bool, exist bool)

CheckRouteStateful 是否为有状态路由

func (*Router) Group

func (r *Router) Group(groups ...func(group *RouterGroup)) *RouterGroup

Group 路由组

func (*Router) HasDefaultRouteHandler

func (r *Router) HasDefaultRouteHandler() bool

HasDefaultRouteHandler 是否存在默认路由处理器

func (*Router) SetDefaultRouteHandler

func (r *Router) SetDefaultRouteHandler(handler RouteHandler)

SetDefaultRouteHandler 设置默认路由处理器,所有未注册的路由均走默认路由处理器

type RouterGroup

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

func (*RouterGroup) AddRouteHandler

func (g *RouterGroup) AddRouteHandler(route int32, stateful bool, handler RouteHandler, middlewares ...MiddlewareHandler) *RouterGroup

AddRouteHandler 添加路由处理器

func (*RouterGroup) Middleware

func (g *RouterGroup) Middleware(middlewares ...MiddlewareHandler) *RouterGroup

Middleware 添加中间件

type ServerOptions

type ServerOptions struct {
	Addr       string
	ServerOpts []mygrpc.ServerOption
}

type TransOptionFunc

type TransOptionFunc func(o *transOptions)

func WithClientDialOptions

func WithClientDialOptions(opts ...grpc.DialOption) TransOptionFunc

WithClientDialOptions 设置客户端拨号选项

func WithClientPoolSize

func WithClientPoolSize(size int) TransOptionFunc

WithClientPoolSize 设置客户端连接池大小

func WithServerListenAddr

func WithServerListenAddr(addr string) TransOptionFunc

WithServerListenAddr 设置服务器监听地址

func WithServerOptions

func WithServerOptions(opts ...mygrpc.ServerOption) TransOptionFunc

WithServerOptions 设置服务器选项

type Transport

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

func NewTransport

func NewTransport(opts ...TransOptionFunc) *Transport

func (*Transport) NewGateClient

func (t *Transport) NewGateClient(ep *endpoint.Endpoint) (*GateGrpcClient, error)

NewGateClient 新建网关客户端

func (*Transport) NewNodeClient

func (t *Transport) NewNodeClient(ep *endpoint.Endpoint) (*NodeGrpcClient, error)

func (*Transport) NewNodeServer

func (t *Transport) NewNodeServer(provider *provider) (*mygrpc.Server, error)

NewNodeServer 新建节点服务器

Jump to

Keyboard shortcuts

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