mqrpc

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Copyright 2014 loolgame Author. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package mqrpc rpc接口定义

Index

Constants

This section is empty.

Variables

View Source
var (
	NULL    = "null"    // nil null
	BOOL    = "bool"    // bool
	INT     = "int"     // int
	LONG    = "long"    // int64
	FLOAT   = "float"   // float32
	DOUBLE  = "double"  // float64
	BYTES   = "bytes"   // []byte
	STRING  = "string"  // string
	JSMAP   = "map"     // map[string]any
	CONTEXT = "context" // context
	MARSHAL = "marshal" // mqrpc.Marshaler
	MSGPACK = "msgpack" // msgpack
)
View Source
var ErrNil = errors.New("mqrpc: nil returned")

ErrNil ErrNil

Functions

func ArgToData

func ArgToData(arg any) (string, []byte, error)

func Bool

func Bool(reply any, err error) (bool, error)

func Bytes

func Bytes(reply any, err error) ([]byte, error)

Bytes is a helper that converts a command reply to a slice of bytes. If err is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts the reply to a slice of bytes as follows:

Reply type      Result
bulk string     reply, nil
simple string   []byte(reply), nil
nil             nil, ErrNil
other           nil, error

func ContextWithValue

func ContextWithValue(ctx context.Context, key string, val any) context.Context

使用此WithValue方法才能通过Context传递数据

func DataToArg

func DataToArg(argType string, argData []byte) (any, error)

func Float64

func Float64(reply any, err error) (float64, error)

Float64 is a helper that converts a command reply to 64 bit float. If err is not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts the reply to an int as follows:

Reply type    Result
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Int

func Int(reply any, err error) (int, error)

Int Int

func Int64

func Int64(reply any, err error) (int64, error)

Int64 is a helper that converts a command reply to 64 bit integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the reply to an int64 as follows:

Reply type    Result
integer       reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func JsMap

func JsMap(reply any, err error) (map[string]any, error)

JsMap JsMap

func Marshal

func Marshal(pObj any, ret callResult) error

Marshal Marshal

func MsgJson added in v1.2.0

func MsgJson(reply any, err error) (string, error)

MsgJson MsgJson

func MsgPack

func MsgPack(pObj any, ret callResult) error

MsgPack MsgPack

func RegTranslatableCtxKey added in v1.2.0

func RegTranslatableCtxKey(key string, makeFun FunMakeCtxValue)

提前注册复合类型的Context val数据(基本类型不需要注册)

func RpcResult

func RpcResult(reply any, err error) callResult

拼装CallResult

func String

func String(reply any, err error) (string, error)

String is a helper that converts a command reply to a string. If err is not equal to nil, then String returns "", err. Otherwise String converts the reply to a string as follows:

Reply type      Result
bulk string     string(reply), nil
simple string   reply, nil
nil             "",  ErrNil
other           "",  error

Types

type CallInfo

type CallInfo struct {
	RPCInfo  *core.RPCInfo
	Result   *core.ResultInfo
	Props    map[string]any
	ExecTime int64
	Agent    IMQServer //代理者  AMQPServer / LocalServer 都继承 Callback(callinfo CallInfo)(error) 方法
}

CallInfo RPC的请求信息

type FunMakeCtxValue added in v1.2.0

type FunMakeCtxValue func() IMarshaler

Context value 复合类型的创建函数

type IGoroutineControl added in v1.2.0

type IGoroutineControl interface {
	Wait() error
	Finish()
}

IGoroutineControl 服务协程数量控制

type IMQServer added in v1.2.0

type IMQServer interface {
	Callback(callinfo *CallInfo) error
}

IMQServer 代理者

type IMarshaler added in v1.2.0

type IMarshaler interface {
	Marshal() ([]byte, error)
	Unmarshal([]byte) error
	String() string
}

IMarshaler is a simple encoding interface used for the broker/transport where headers are not supported by the underlying implementation.

type IRPCClient added in v1.2.0

type IRPCClient interface {
	Done() (err error)
	Call(ctx context.Context, _func string, params ...any) (any, error)
	CallArgs(ctx context.Context, _func string, argTypes []string, args [][]byte) (any, error) // ctx参数必须装进args中
	CallNR(ctx context.Context, _func string, params ...any) error
	CallNRArgs(ctx context.Context, _func string, argTypes []string, args [][]byte) error // ctx参数必须装进args中
}

IRPCClient 客户端定义

type IRPCListener added in v1.2.0

type IRPCListener interface {
	// 当未找到请求的Method时会触发该方法(返回的MethodInfo会被执行,为nil时必须返回error)
	OnMethodNotFound(fn string) (*MethodInfo, error)

	// 会对method执行之前做一些前置处理,如:检查当前玩家是否已登录,打印统计日志等(当error不为nil时将直接返回改错误信息而不会再执行后续调用)
	OnBeforeHandle(fn string, callInfo *CallInfo) error

	// 当方法执行超时时会触发该回调
	OnTimeOut(fn string, Expired int64)

	// 当方法执行异常时会触发该回调
	OnError(fn string, callInfo *CallInfo, err error)

	// 当方法执行完成时会触发该回调(时间单位为 Nano 纳秒  1000000纳秒等于1毫秒)
	OnComplete(fn string, callInfo *CallInfo, result *core.ResultInfo, execTime int64)
}

IRPCListener 事件监听器

type IRPCSerialize added in v1.2.0

type IRPCSerialize interface {
	/**
	序列化 结构体-->[]byte
	param 需要序列化的参数值
	@return ptype 当能够序列化这个值,并且正确解析为[]byte时 返回改值正确的类型,否则返回 ""即可
	@return p 解析成功得到的数据, 如果无法解析该类型,或者解析失败 返回nil即可
	@return err 无法解析该类型,或者解析失败 返回错误信息
	*/
	Serialize(param any) (ptype string, p []byte, err error)
	/**
	反序列化 []byte-->结构体
	ptype 参数类型 与Serialize函数中ptype 对应
	b   参数的字节流
	@return param 解析成功得到的数据结构
	@return err 无法解析该类型,或者解析失败 返回错误信息
	*/
	Deserialize(ptype string, b []byte) (param any, err error)
	/**
	返回这个接口能够处理的所有类型
	*/
	GetTypes() []string
}

IRPCSerialize 自定义参数序列化接口

type IRPCServer added in v1.2.0

type IRPCServer interface {
	Addr() string
	SetListener(listener IRPCListener) // 设置监听器
	SetGoroutineControl(control IGoroutineControl)
	GetExecuting() int64
	Register(id string, f any)   // 注册RPC方法,f第一个参数必须为context.Context(单线程)
	RegisterGO(id string, f any) // 注册RPC方法,f第一个参数必须为context.Context(多线程)
	Done() (err error)
}

IRPCServer 服务定义

type MethodInfo added in v1.2.0

type MethodInfo struct {
	Function  reflect.Value
	FuncType  reflect.Type
	InType    []reflect.Type
	Goroutine bool
}

MethodInfo 方法信息

type ParamOption

type ParamOption func() []any

ParamOption ParamOption

func Param

func Param(params ...any) ParamOption

Param 请求参数包装器

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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