agent

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Index

Constants

View Source
const (
	MetaClientId   = "Client-Id"   // 客户端连接ID
	MetaClientIp   = "Client-Ip"   // IPv4地址
	MetaClientVer  = "Client-Ver"  // 客户端版本
	MetaAccountId  = "Account-Id"  // 账户ID
	MetaChannelUid = "Channel-Uid" // 渠道账户
	MetaServerId   = "Server-Id"   // 服务器ID
	MetaRoleId     = "Role-Id"     // 角色ID
)

Variables

View Source
var (
	Opts = &struct {
		Type              string // 网关类型, websocket / tcp / quic
		Host              string // 主机IP地址
		Port              string // 网关监听端口
		ConnMaxNum        uint   // 网关最大连接
		HeartbeatInterval int64  // 网关 WebSocket 心跳间隔
		HeartbeatDeadline int64  // 网关 WebSocket 心跳等待
		AuthTime          int64  // 网关 鉴权认证 有效时间
	}{}

	Flags = []cli.Flag{
		&cli.StringFlag{
			Name:        "agent_type",
			Value:       "websocket",
			Usage:       "设置网关类型. 目前支持 websocket, tcp, quic",
			EnvVars:     []string{"GAME_AGENT_TYPE"},
			Destination: &Opts.Type,
			Required:    true,
		},
		&cli.StringFlag{
			Name:        "agent_host",
			Value:       "",
			Usage:       "设置当前网关的外网IP地址",
			EnvVars:     []string{"GAME_AGENT_HOST"},
			Destination: &Opts.Host,
			Required:    true,
		},
		&cli.StringFlag{
			Name:        "agent_port",
			Value:       ":0",
			Usage:       "设置当前网关的端口. default :0",
			Destination: &Opts.Port,
		},
		&cli.Int64Flag{
			Name:        "agent_heartbeat_interval",
			Value:       30,
			Usage:       "设置当前网关的心跳的时间间隔 (单位秒)",
			EnvVars:     []string{"GAME_AGENT_HEARTBEAT_INTERVAL"},
			Destination: &Opts.HeartbeatInterval,
		},
		&cli.Int64Flag{
			Name:        "agent_heartbeat_deadline",
			Value:       10,
			Usage:       "设置当前网关的心跳的等待时间 (单位秒)",
			EnvVars:     []string{"GAME_AGENT_HEARTBEAT_DEADLINE"},
			Destination: &Opts.HeartbeatDeadline,
		},
		&cli.UintFlag{
			Name:        "agent_conn_max",
			Value:       0,
			Usage:       "设置当前网关的最大连接数",
			EnvVars:     []string{"GAME_AGENT_CONN_MAX"},
			Destination: &Opts.ConnMaxNum,
		},
		&cli.Int64Flag{
			Name:        "agent_auth_time",
			Value:       5,
			Usage:       "设置当前网关的鉴权认证有效时间 (单位秒)",
			EnvVars:     []string{"GAME_AGENT_AUTH_TIME"},
			Destination: &Opts.AuthTime,
		},
	}
)
View Source
var (
	DefaultReadTimeout  = 15 * time.Second // 默认请求超时时间
	DefaultWriteTimeout = 15 * time.Second // 默认请求超时时间
)
View Source
var (
	RegisterAgent = map[string]func(agent *Agent) Server{}
)

Functions

This section is empty.

Types

type Agent

type Agent struct {
	sync.RWMutex

	OnReceive    func(Client, *codec.ClientHead, []byte) (*codec.ServerHead, []byte, error) // 收到数据调用
	OnDisconnect func(Client)                                                               // 连接断开时调用
	// contains filtered or unexported fields
}

func NewAgent

func NewAgent(codecMix []uint8, opts ...Option) *Agent

NewAgent

func (*Agent) Address

func (g *Agent) Address() string

Address 网关地址

func (*Agent) All

func (g *Agent) All() map[string]Client

全部客户端

func (*Agent) Broadcast

func (g *Agent) Broadcast(msg []byte, filter func(client Client) bool)

广播

func (*Agent) ClientCodec

func (g *Agent) ClientCodec() *codec.Client

func (*Agent) Close

func (g *Agent) Close()

关闭网关服务

func (*Agent) Count

func (g *Agent) Count() int

连接数

func (*Agent) Filter

func (g *Agent) Filter(filter func(client Client) bool) map[string]Client

过滤角色对象

func (*Agent) ForEach

func (g *Agent) ForEach(fn func(client Client) bool)

迭代客户端

func (*Agent) GetClient

func (g *Agent) GetClient(val string, by ...string) Client

获取客户端连接对象

func (*Agent) Opts

func (g *Agent) Opts() *Options

func (*Agent) Run

func (g *Agent) Run() error

启动网关服务

func (*Agent) Server

func (g *Agent) Server() Server

func (*Agent) ServerCodec

func (g *Agent) ServerCodec() *codec.Server

func (*Agent) SetOnDisconnect

func (g *Agent) SetOnDisconnect(fn func(Client))

func (*Agent) SetOnReceive

func (g *Agent) SetOnReceive(fn func(Client, *codec.ClientHead, []byte) (*codec.ServerHead, []byte, error))

func (*Agent) StartClient

func (g *Agent) StartClient(client Client)

type Client

type Client interface {
	Id() string                               // Client Id
	Server() Server                           // 关联服务端
	Meta() *Meta                              // 客户端上下文
	Log() *logger.Helper                      // 日志对象
	Closed() bool                             // 判断是否关闭
	Read() (*codec.ClientHead, []byte, error) // 读取消息
	Write([]byte)                             // 发送消息
	Close()                                   // 关闭连接
	Destroy()                                 // 销毁连接 (丢弃任何未发送或未确认的数据)
	SetAuthState(state bool)                  // 设置认证状态 (建立Socket连接后, 需要发送Token进行认证)
}

客户端

type Meta

type Meta struct {
	*meta.Meta
}

网关上下文

func FromMeta

func FromMeta(ctx context.Context) (*Meta, error)

FromMeta 从Meta解析metadata数据

func NewMeta

func NewMeta(clientId string) *Meta

NewMeta 实例化metadata数据

func (*Meta) AccountId

func (ctx *Meta) AccountId() int64

func (*Meta) ChannelUid

func (ctx *Meta) ChannelUid() string

func (*Meta) ClientId

func (ctx *Meta) ClientId() string

func (*Meta) ClientIp

func (ctx *Meta) ClientIp() string

func (*Meta) ClientVer

func (ctx *Meta) ClientVer() string

func (*Meta) IsOnline

func (ctx *Meta) IsOnline() bool

func (*Meta) RoleId

func (ctx *Meta) RoleId() int64

func (*Meta) ServerId

func (ctx *Meta) ServerId() int32

type Option

type Option func(o *Options)

func WithAddress

func WithAddress(addr string) Option

func WithHeartbeatDeadline

func WithHeartbeatDeadline(t time.Duration) Option

func WithHeartbeatInterval

func WithHeartbeatInterval(t time.Duration) Option

func WithMaxConnNum

func WithMaxConnNum(num uint) Option

func WithReadTimeout

func WithReadTimeout(t time.Duration) Option

func WithWaitAuthTime

func WithWaitAuthTime(t time.Duration) Option

func WithWriteTimeout

func WithWriteTimeout(t time.Duration) Option

type Options

type Options struct {
	Address           string        // 服务器监听地址
	MaxConnNum        uint          // 最大连接数限制
	WaitAuthTime      time.Duration // 等待认证时间
	HeartbeatInterval time.Duration // 心跳间隔
	HeartbeatDeadline time.Duration // 心跳等待
	ReadTimeout       time.Duration // 读超时
	WriteTimeout      time.Duration // 写超时
}

服务器参数结果提

func NewOptions

func NewOptions(opts ...Option) *Options

func (*Options) Init

func (o *Options) Init(opts ...Option)

type Server

type Server interface {
	Name() string   // 服务器名称
	Agent() *Agent  // 关联服务端
	Opts() *Options // 参数
	Port() int      // 监听端口
	Run() error     // 启动服务
	Close()         // 停止服务
}

网关网络服务接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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