From 0913b3f3e66ba7ca4a86e17c70aaaa647f127ae4 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 22 Oct 2020 14:57:07 +0800 Subject: [PATCH] 1 --- server/analyseapi/app.go | 19 ++++----- server/analyseapi/g.go | 1 + server/analyseapi/gclistener.go | 71 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 server/analyseapi/gclistener.go diff --git a/server/analyseapi/app.go b/server/analyseapi/app.go index 84300c2..cbfd30c 100644 --- a/server/analyseapi/app.go +++ b/server/analyseapi/app.go @@ -1,9 +1,7 @@ package main -import "q5" import "f5" import "fmt" -import "time" type App_ struct { f5.App_ @@ -13,17 +11,16 @@ var App *App_ func (this *App_) Init() { f5.App = &this.App_ + f5.App.SetPkgName("analyseapi") this.App_.Init() - G.MetaMgr = &MetaMgr{} + G.MetaMgr = new(MetaMgr) G.MetaMgr.Init() - f5.Timer().AddRepeatTimer( - 1000 * 5, - func (params *q5.XParams) { - - }, - func (params *q5.XParams) { - fmt.Println("hello", time.Now()) - }) + G.MetaMgr.Load() + G.GCListener = new(GCListener) + G.GCListener.Init() + fmt.Println( + G.MetaMgr.GetConf().GetGameHeartbeat()[0], + G.MetaMgr.GetServer(1)) } func (this *App_) UnInit() { diff --git a/server/analyseapi/g.go b/server/analyseapi/g.go index 97a7b14..ffd872e 100644 --- a/server/analyseapi/g.go +++ b/server/analyseapi/g.go @@ -2,6 +2,7 @@ package main type GlobalVar struct { MetaMgr *MetaMgr + GCListener *GCListener } var G *GlobalVar = &GlobalVar{} diff --git a/server/analyseapi/gclistener.go b/server/analyseapi/gclistener.go new file mode 100644 index 0000000..f19fc83 --- /dev/null +++ b/server/analyseapi/gclistener.go @@ -0,0 +1,71 @@ +package main + +import ( + "net/http" + "fmt" + "sync" + "q5" + "f5" +) + +type GCListener struct { + handlersMutex sync.RWMutex + handlers map[string]func(http.ResponseWriter, *http.Request) +} + +func (this *GCListener) Init() { + this.handlers = make(map[string]func(http.ResponseWriter, *http.Request)) + fmt.Println("GCListener.Init\n") + http.HandleFunc("/webapp/index.php", this.dispatchRequest) + this.RegisterHandle("Ops", "selfChecking", func(w http.ResponseWriter,r *http.Request) { + w.Write([]byte(`{"errcode":0, "errmsg":"", "health":1, "max_rundelay": 10}`)) + }) + this.RegisterHandle("Stat", "updateSession", func(w http.ResponseWriter,r *http.Request) { + f5.App.AddIMMsg(IM_UPDATE_SESSION, new(q5.XParams).Init(func (params *q5.XParams){ + params.Sender.SetString(q5.Request(r, "account_id").GetString()) + params.Param1.SetString(q5.Request(r, "session_id").GetString()) + params.Param2.SetString(q5.GetRequestRemoteAddr(r)) + params.Param3.SetString(q5.Request(r, "count").GetString()) + })) + w.Write([]byte(`{"errcode":0, "errmsg":""`)) + }) + this.RegisterHandle("Stat", "getRealTimeOnline", func(w http.ResponseWriter,r *http.Request) { + onlineNum := G.StatMgr.GetRealTimeOnline( + q5.Request(r, "game_id").GetInt32(), + q5.Request(r, "channel").GetInt32()) + w.Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "num":%d}`, onlineNum))) + }) + go this.listenAndServe(G.MetaMgr.GetServer(1).GetListenPort()) +} + +func (this *GCListener) UnInit() { + fmt.Println("GCListener.UnInit\n") +} + +func (this *GCListener) listenAndServe(listen_port int32) { + http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", listen_port), nil) +} + +func (this *GCListener) dispatchRequest(w http.ResponseWriter, r *http.Request) { + handleName := q5.Request(r, "c").GetString() + "$" + q5.Request(r, "a").GetString() + handle := this.getHandle(handleName) + if handle != nil { + handle(w, r) + } else { + w.Write([]byte(`{"errcode":404, "errmsg":"接口不存在"}`)) + } +} + +func (this *GCListener) getHandle(handleName string) (func(http.ResponseWriter, *http.Request)) { + this.handlersMutex.Lock() + defer this.handlersMutex.Unlock() + handle, _ := this.handlers[handleName] + return handle +} + +func (this *GCListener) RegisterHandle(c string, a string, handle func(http.ResponseWriter, *http.Request)) { + this.handlersMutex.Lock() + defer this.handlersMutex.Unlock() + handleName := c + "$" + a + this.handlers[handleName] = handle +}