xserver

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2022 License: GPL-2.0 Imports: 41 Imported by: 0

Documentation

Overview

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

---------------------------------------------------------------------//

GNU GENERAL PUBLIC LICENSE                       //
   Version 2, June 1991                          //
                                                 //

Copyright (C) Wells Hsu, wellshsu@outlook.com, All rights reserved. // Everyone is permitted to copy and distribute verbatim copies // of this license document, but changing it is not allowed. //

SEE LICENSE.md FOR MORE DETAILS.                   //

---------------------------------------------------------------------//

Index

Constants

View Source
const (
	CONSUL_RESP_OK    = "ok"     // Consul响应200
	CONSUL_CHECK_PATH = "/check" // Consul心跳检测
)
View Source
const (
	LAN_CIN_MAX_FRAME  = 50000
	LAN_COUT_MAX_FRAME = 50000
)
View Source
const (
	EVT_SERVER_STARTED = -1 // 服务就绪(配置就绪 & 日志就绪 & DB就绪 & Redis就绪 & Lan就绪)
	EVT_SERVER_CHANGED = -2 // 服务变更(参数类型:[]interface{}{added map[string][]string, removed map[string][]string})
	EVT_SERVER_PREQUIT = -3 // 服务即将退出
)
View Source
const (
	SERVER_SLEEP time.Duration = 10 * time.Millisecond
)
View Source
const (
	UPDATE_SLEEP time.Duration = 10 * time.Millisecond
)

Variables

View Source
var (
	CslClt *consulapi.Client // Consul连接
	CslCfg *consulapi.Config // Consul配置
)
View Source
var (
	GMsg = xevt.NewEvtMgr(true)  // Msg消息中心
	GRpc = xevt.NewEvtMgr(false) // Rpc消息中心
	GCgi = xevt.NewEvtMgr(false) // Cgi消息中心
	GEvt = xevt.NewEvtMgr(true)  // Evt消息中心
)
View Source
var (
	ERR_SEND_CHAN_FULL  = errors.New("send chan is full")
	ERR_NO_ROUTE_FOUND  = errors.New("no route found")
	ERR_RPC_TIMEOUT     = errors.New("rpc call timeout")
	ERR_CGI_TIMEOUT     = errors.New("cgi call timeout")
	ERR_RPC_INTERRUPTED = errors.New("rpc call has been interrupted, see log context for more details.")
	ERR_CGI_INTERRUPTED = errors.New("cgi call has been interrupted, see log context for more details.")
)
View Source
var (
	GWrap   *Wrap
	GServer IServer
)
View Source
var CGIROUTEMAP map[int]*CgiRoute // cgi路由
View Source
var MSGROUTEMAP map[int]*MsgRoute // msg路由
View Source
var (
	MainTID int64 = -1
)
View Source
var RPCROUTEMAP map[int]*RpcRoute // rpc路由

Functions

func BackupLan

func BackupLan() int

func ClearInterval

func ClearInterval(id int, tid ...int64)

取消间歇调用(务必在逻辑线程中调用或指定线程ID[tid])

func ClearTimeout

func ClearTimeout(id int, tid ...int64)

取消超时调用(务必在逻辑线程中调用或指定线程ID[tid])

func CloseLan

func CloseLan()

func MonitorLan

func MonitorLan()

func NotifyCgi

func NotifyCgi(id int, req *xproto.CgiReq, resp *xproto.CgiResp) bool

广播Cgi消息(用于客户端和服务器之间交互)(全局)

func NotifyEvt

func NotifyEvt(id int, param interface{}) bool

广播Evt消息(用于服务器内部)(全局)

func NotifyMsg

func NotifyMsg(id int, mreq *xproto.MsgReq) bool

广播Msg消息(用于客户端和服务器之间交互)(全局)

func NotifyRpc

func NotifyRpc(id int, req *xproto.RpcReq, resp *xproto.RpcResp) bool

广播Rpc消息(用于服务器之间交互)(全局)

func PauseLan

func PauseLan()

func PostKV

func PostKV(key string, value string, version string, block ...bool) bool

推送KV(键值对)至Consul Storage(version-版本号,以'VERSION_'为前缀,block-是否阻塞)

func PullKV

func PullKV(key string) []byte

从Consul Storage中拉取KV(键值对)(阻塞)

func RecvLan

func RecvLan()

func RegCgi

func RegCgi(id int, fun func(req *xproto.CgiReq, resp *xproto.CgiResp)) int

注册Cgi消息(用于客户端和服务器之间交互)(全局)

func RegCgiRoute

func RegCgiRoute(_map map[int]*CgiRoute)

func RegEvt

func RegEvt(id int, fun func(interface{})) int

注册Evt消息(用于服务器内部)(全局)

func RegMsg

func RegMsg(id int, fun func(*xproto.MsgReq)) int

注册Msg消息(用于客户端和服务器之间交互)(全局)

func RegMsgRoute

func RegMsgRoute(_map map[int]*MsgRoute)

func RegRpc

func RegRpc(id int, fun func(rreq *xproto.RpcReq, rresp *xproto.RpcResp)) int

注册Rpc消息(用于服务器之间交互)(全局)

func RegRpcRoute

func RegRpcRoute(_map map[int]*RpcRoute)

func RestoreLan

func RestoreLan()

func ResumeLan

func ResumeLan()

func RunIn

func RunIn(tid int64, fun func()) chan bool

在指定逻辑线程中调用(返回的chan可用于阻塞当前线程)

func RunInMain

func RunInMain(fun func()) chan bool

在逻辑主线程中调用(返回的chan可用于阻塞当前线程)

func SendAsync

func SendAsync(id int, uid int, req proto.Message, addr string, callback func(frame *xproto.RpcResp, err error), offsetAndTimeout ...int)

[发送Rpc消息(异步)] [id-消息ID] [uid-用户ID(负载均衡)] [req-请求结构体] [addr-目标服务器] [callback-回调函数] [offset-目标协程ID偏移(基于protocol中定义)] [timeout-超时时长]

func SendCgi

func SendCgi(id int, uid int, req *http.Request, addr string, timeout ...int) (cresp *xproto.CgiResp, err error)

[发送Cgi消息(同步,否则ResponseWriter无法输出)] [id-消息ID] [uid-用户ID(负载均衡)] [req-请求结构体] [addr-目标服务器] [timeout-超时时长]

func SendFrame

func SendFrame(frame xproto.IFrame) bool

[发送网络帧(根据UID负载均衡)]

func SendMsg

func SendMsg(id int, msg proto.Message, mreq *xproto.MsgReq) bool

[发送Msg消息] [id-消息ID] [msg-结构体] [mreq-msg帧]

func SendSync

func SendSync(id int, uid int, req proto.Message, resp proto.Message, addr string, offsetAndTimeout ...int) error

[发送Rpc消息(同步)] [id-消息ID] [uid-用户ID(负载均衡)] [req-请求结构体] [resp-返回结构体] [addr-目标服务器] [offset-目标协程偏移(基于protocol中定义)] [timeout-超时时长]

func Start

func Start(svr IServer)

func StartLan

func StartLan(lanCfg *LanCfg, handleMsg func(*xproto.MsgReq),
	handleRpc func(*xproto.RpcReq, *xproto.RpcResp),
	handleCgi func(*xproto.CgiReq, *xproto.CgiResp))

func Stop

func Stop()

func SubKV

func SubKV(key string, interval int, onUpdate func(data []byte))

订阅Consul Storage中的KV(键值对)(阻塞)(interval-间歇时间)(注意订阅的Key需要设置版本,以'VERSION_'为前缀)

func UnregCgi

func UnregCgi(id int, hid int) bool

注销Cgi消息(用于客户端和服务器之间交互)(全局)

func UnregEvt

func UnregEvt(id int, hid int) bool

注销Evt消息(用于服务器内部)(全局)

func UnregMsg

func UnregMsg(id int, hid int) bool

注销Msg消息(用于客户端和服务器之间交互)(全局)

func UnregRpc

func UnregRpc(id int, hid int) bool

注销Rpc消息(用于服务器之间交互)(全局)

func WatchSignal

func WatchSignal() <-chan string

Types

type CgiFunc

type CgiFunc func(req *xproto.CgiReq, resp *xproto.CgiResp)

func (CgiFunc) Handle

func (this CgiFunc) Handle(reply *xevt.EvtReply, req interface{}, resp interface{})

type CgiRoute

type CgiRoute struct {
	Route
	Method  []string
	Timeout int
}

type EvtFunc

type EvtFunc func(interface{})

func (EvtFunc) Handle

func (this EvtFunc) Handle(reply *xevt.EvtReply, receiver interface{}, param interface{})

type IServer

type IServer interface {
	Init() bool                                         // 初始化
	Start()                                             // 服务启动
	Update(delta float32)                               // 服务循环
	Destroy()                                           // 服务结束
	PreQuit()                                           // 服务即将退出
	Name() string                                       // 服务名称
	InitConfig() bool                                   // 读取配置
	GetConfig() *SvrCfg                                 // 获取配置
	GetFPS() int                                        // 获取帧率
	UpdateTitle() string                                // 更新标题
	GetTitle() string                                   // 获取标题
	RecvMsg(mreq *xproto.MsgReq)                        // 接收Msg消息
	RecvRpc(rreq *xproto.RpcReq, rresp *xproto.RpcResp) // 接收Rpc消息
	RecvCgi(creq *xproto.CgiReq, cresp *xproto.CgiResp) // 接收Cgi消息
}

type LanCfg

type LanCfg struct {
	Name     string // 名称
	Addr     string // tcp://$ip:$port
	Raw      string // $ip:$port
	IP       string // IP
	Port     int    // 端口
	GO       int    // 逻辑线程数
	MaxRx    int    // 最大接收字节数(KB)
	MsgProto string // msg消息协议类型,可选pb/json,默认pb
	CgiProto string // cgi消息协议类型,可选pb/json,默认json
}

func NewLanCfg

func NewLanCfg(name, addr string) *LanCfg

func (*LanCfg) ServerID

func (this *LanCfg) ServerID() string

服务器ID($name@tcp://$ip:$port)

type LanClt

type LanClt struct {
	*LanCfg
	Sockets []mangos.Socket
}

func NewLanClt

func NewLanClt(cfg *LanCfg) *LanClt

func (*LanClt) Close

func (this *LanClt) Close()

func (*LanClt) Send

func (this *LanClt) Send(bytes []byte, idx int) error

type LanSvr

type LanSvr struct {
	*LanCfg
	mangos.Socket
	Clients  sync.Map // map[string][]*LanClt
	ClientID sync.Map // map[string]*LanClt
	SClosed  bool
}
var (
	GLan  *LanSvr
	GProc []*Proc
)

func NewLanSvr

func NewLanSvr(cfg *LanCfg) *LanSvr

func (*LanSvr) Close

func (this *LanSvr) Close()

func (*LanSvr) Recv

func (this *LanSvr) Recv() ([]byte, error)

func (*LanSvr) SelectAll

func (this *LanSvr) SelectAll(svr string) []*LanClt

func (*LanSvr) SelectRand

func (this *LanSvr) SelectRand(svr string) *LanClt

func (*LanSvr) SendData

func (this *LanSvr) SendData(svr string, bytes []byte, idx int) error

func (*LanSvr) Update

func (this *LanSvr) Update(smap map[string][]string)

type MsgFunc

type MsgFunc func(*xproto.MsgReq)

func (MsgFunc) Handle

func (this MsgFunc) Handle(reply *xevt.EvtReply, param1 interface{}, param2 interface{})

type MsgRoute

type MsgRoute struct {
	Route
}

type Proc

type Proc struct {
	TID   int64              // 线路的GoID
	Num   int                // 线路线程总数
	CIN   chan xproto.IFrame // 输入队列
	COUT  chan xproto.IFrame // 输出队列
	Loop  bool
	Pause bool
	Resp  sync.Map // map[int64]chan *xproto.RpcReq/*xproto.CgiFrame

}

func NewProc

func NewProc() *Proc

func (*Proc) MaxID

func (this *Proc) MaxID() int64

func (*Proc) PopCIN

func (this *Proc) PopCIN() (xproto.IFrame, bool)

func (*Proc) PushCIN

func (this *Proc) PushCIN(frame xproto.IFrame) bool

type Route

type Route struct {
	ID   int      // 路由ID
	Name string   // 路由名称
	GoL  int      // 协程ID(左)
	GoR  int      //协程ID(右)
	RW   bool     // 可读可写(默认true)
	Log  int      // 日志层级(参考xlog的LogLevel)
	Dst  []string // 目标
}

func (*Route) GetLog

func (this *Route) GetLog() int

获取日志层级,若未指定则使用全局日志层级

type RpcFunc

type RpcFunc func(rreq *xproto.RpcReq, rresp *xproto.RpcResp)

func (RpcFunc) Handle

func (this RpcFunc) Handle(reply *xevt.EvtReply, param1 interface{}, param2 interface{})

type RpcRoute

type RpcRoute struct {
	Route
}

type Server

type Server struct {
	xobj.OBJECT
	REAL   IServer
	Config *SvrCfg
	FPS    int
	Title  string
}

func (*Server) CTOR

func (this *Server) CTOR(CHILD interface{})

func (*Server) Destroy

func (this *Server) Destroy()

服务结束

func (*Server) GetConfig

func (this *Server) GetConfig() *SvrCfg

获取配置

func (*Server) GetFPS added in v1.2.0

func (this *Server) GetFPS() int

获取帧率

func (*Server) GetTitle added in v1.2.0

func (this *Server) GetTitle() string

获取标题

func (*Server) Init

func (_this *Server) Init() bool

初始化

func (*Server) InitConfig

func (this *Server) InitConfig() bool

读取配置

func (*Server) Name

func (this *Server) Name() string

服务名称

func (*Server) PreQuit

func (_this *Server) PreQuit()

func (*Server) RecvCgi

func (this *Server) RecvCgi(rreq *xproto.CgiReq, rresp *xproto.CgiResp)

接收Cgi消息

func (*Server) RecvMsg

func (this *Server) RecvMsg(mreq *xproto.MsgReq)

func (*Server) RecvRpc

func (this *Server) RecvRpc(rreq *xproto.RpcReq, rresp *xproto.RpcResp)

接收Rpc消息

func (*Server) Start

func (this *Server) Start()

服务启动

func (*Server) Update

func (this *Server) Update(delta float32)

func (*Server) UpdateTitle

func (this *Server) UpdateTitle() string

type SvrCfg

type SvrCfg struct {
	Raw              xconfig.Configer
	Env              string // 环境标识: 测试,内测,生产
	LanCfg           *LanCfg
	LinkServer       string // 需要连接的内部服务器
	ConsulAddr       string // Consul中心地址
	ConsulHttp       string // Consul检测地址
	ConsulTimeout    string // Consul超时时间
	ConsulInterval   string // Consul访问间隔
	ConsulDeregister string // Consul延迟注销
}

func (*SvrCfg) Init

func (this *SvrCfg) Init(config string) bool

func (*SvrCfg) IsDebugEnv

func (this *SvrCfg) IsDebugEnv() bool

func (*SvrCfg) SvrID

func (this *SvrCfg) SvrID() string

func (*SvrCfg) SvrName

func (this *SvrCfg) SvrName() string

type TimerEntity

type TimerEntity struct {
	ID      int
	Func    func()
	Time    int
	RawTime int
	Repeat  bool
	Crash   bool
	RW      bool
	Tag     interface{}
	Log     int
}

func RunInNext

func RunInNext(fun func()) *TimerEntity

在当前逻辑线程中的下一帧调用

func SetInterval

func SetInterval(fun func(), interval float32, tid ...int64) *TimerEntity

设置间歇调用(周期以秒为单位)(务必在逻辑线程中调用或指定线程ID[tid])

func SetTimeout

func SetTimeout(fun func(), timeout float32, tid ...int64) *TimerEntity

设置超时调用(时间以秒为单位)(务必在逻辑线程中调用或指定线程ID[tid])

func (*TimerEntity) SetLog

func (this *TimerEntity) SetLog(log int) *TimerEntity

设置会话的日志层级

func (*TimerEntity) SetRW

func (this *TimerEntity) SetRW(sig bool) *TimerEntity

设置会话的可读性(默认为可读可写)

func (*TimerEntity) SetTag

func (this *TimerEntity) SetTag(tag interface{}) *TimerEntity

设置会话的标签

type TimerRecord

type TimerRecord struct {
	Timers   sync.Map
	TimerID  int64
	LastTime int
}

func (*TimerRecord) MaxID

func (this *TimerRecord) MaxID() int64

type Wrap

type Wrap struct {
	Svr    IServer
	ChQuit chan bool // 阻塞chan
}

func NewWrap

func NewWrap(s IServer) *Wrap

func (*Wrap) Destroy

func (this *Wrap) Destroy()

func (*Wrap) Init

func (this *Wrap) Init() bool

func (*Wrap) Run

func (this *Wrap) Run()

func (*Wrap) Stop

func (this *Wrap) Stop()

Jump to

Keyboard shortcuts

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