From 0860ec00d8e1a32b8196bfb620bf991ad42eb6c7 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 27 Oct 2020 21:14:37 +0800 Subject: [PATCH] 1 --- server/analyseapi/app.go | 4 +- server/analyseapi/g.go | 1 - server/analyseapi/gamelog.go | 31 ------ server/analyseapi/gclistener.go | 71 -------------- server/analyseapi/go.mod | 2 +- server/analyseapi/makefile | 7 +- server/analyseapi/statmgr.go | 164 -------------------------------- 7 files changed, 5 insertions(+), 275 deletions(-) delete mode 100644 server/analyseapi/gclistener.go delete mode 100644 server/analyseapi/statmgr.go diff --git a/server/analyseapi/app.go b/server/analyseapi/app.go index 455d20e..77f3fcf 100644 --- a/server/analyseapi/app.go +++ b/server/analyseapi/app.go @@ -12,18 +12,16 @@ var App = new (App_) func (this *App_) Init() { f5.App = &this.App_ - f5.App.SetPkgName("statserver") + f5.App.SetPkgName("analyseapi") this.App_.Init() G.MetaMgr = new(MetaMgr).Init() G.HttpServer = new(f5.HttpServer).Init() G.GameLog = new(GameLog).Init() - G.StatMgr = new(StatMgr).Init() G.HttpServer.Start(G.MetaMgr.GetServer(1).GetListenPort()); } func (this *App_) UnInit() { - G.StatMgr.UnInit() G.GameLog.UnInit() G.HttpServer.UnInit() G.MetaMgr.UnInit() diff --git a/server/analyseapi/g.go b/server/analyseapi/g.go index 2d4f933..6c32773 100644 --- a/server/analyseapi/g.go +++ b/server/analyseapi/g.go @@ -7,7 +7,6 @@ import ( type GlobalVar struct { MetaMgr *MetaMgr HttpServer *f5.HttpServer - StatMgr *StatMgr GameLog *GameLog } diff --git a/server/analyseapi/gamelog.go b/server/analyseapi/gamelog.go index a839753..3fcc9f0 100644 --- a/server/analyseapi/gamelog.go +++ b/server/analyseapi/gamelog.go @@ -1,7 +1,6 @@ package main import ( - "q5" "f5" ) @@ -16,33 +15,3 @@ func (this *GameLog) Init() *GameLog { func (this *GameLog) UnInit() { } - -func (this *GameLog) PlayerOnline(remoteAddr string, accountId string, registerTime int64) { - gameId := f5.ExtractGameIdFromAccountId(accountId) - channel := f5.ExtractChannelFromAccountId(accountId) - - prop := q5.NewMxoObject() - prop.SetXValue("channel", q5.NewXInt32(channel)) - prop.SetXValue("ad_channel", q5.NewXString("")) - prop.SetXValue("account_register_date", q5.NewXString(q5.FormatUnixDateTime(registerTime))) - prop.SetXValue("gameid", q5.NewXInt32(gameId)) - - f5.TgLog().AddTrackLog(gameId, accountId, remoteAddr, 21, 1, prop.AsXObject()) -} - -func (this *GameLog) PlayerOffline(remoteAddr string, accountId string, registerTime int64, onlineTime int64) { - gameId := f5.ExtractGameIdFromAccountId(accountId) - channel := f5.ExtractChannelFromAccountId(accountId) - - prop := q5.NewMxoObject() - prop.SetXValue("channel", q5.NewXInt32(channel)) - prop.SetXValue("ad_channel", q5.NewXString("")) - prop.SetXValue("account_register_date", q5.NewXString(q5.FormatUnixDateTime(registerTime))) - prop.SetXValue("gameid", q5.NewXInt32(gameId)) - - prop.SetXValue("online_time", q5.NewXString(q5.FormatUnixDateTime(onlineTime))) - prop.SetXValue("online_duration", q5.NewXString( - q5.FormatUnixDateTime(f5.App.NowUnix() - onlineTime))) - - f5.TgLog().AddTrackLog(gameId, accountId, remoteAddr, 21, 2, prop.AsXObject()) -} diff --git a/server/analyseapi/gclistener.go b/server/analyseapi/gclistener.go deleted file mode 100644 index f19fc83..0000000 --- a/server/analyseapi/gclistener.go +++ /dev/null @@ -1,71 +0,0 @@ -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 -} diff --git a/server/analyseapi/go.mod b/server/analyseapi/go.mod index 4a501ff..632d09a 100644 --- a/server/analyseapi/go.mod +++ b/server/analyseapi/go.mod @@ -1,4 +1,4 @@ -module statserver +module analyseapi go 1.11 diff --git a/server/analyseapi/makefile b/server/analyseapi/makefile index 63d6a58..6b70d5b 100644 --- a/server/analyseapi/makefile +++ b/server/analyseapi/makefile @@ -1,10 +1,9 @@ - compile: @. /etc/profile protoc --proto_path=../tools/protobuild --go_out=./mt ../tools/protobuild/mt.proto @export GOPROXY=https://goproxy.io - @go build -gcflags=all="-N -l" -o ../bin/statserver + @go build -gcflags=all="-N -l" -o ../bin/analyseapi @echo "compile done" debug: @@ -12,9 +11,9 @@ debug: protoc --proto_path=../tools/protobuild --go_out=./mt ../tools/protobuild/mt.proto @export GOPROXY=https://goproxy.io - @go build -gcflags=all="-N -l" -ldflags "-X q5.optDebug=1" -o ../bin/statserver + @go build -gcflags=all="-N -l" -ldflags "-X q5.optDebug=1" -o ../bin/analyseapi @echo "compile done" clean: - @rm -f ../bin/statserver + @rm -f ../bin/analyseapi @echo "clean done" diff --git a/server/analyseapi/statmgr.go b/server/analyseapi/statmgr.go deleted file mode 100644 index bffdeb6..0000000 --- a/server/analyseapi/statmgr.go +++ /dev/null @@ -1,164 +0,0 @@ -package main - -import ( - "net/http" - "sync" - "fmt" - "q5" - "f5" -) - -type OnlineUser struct { - accountId string - sessionId string - timer *q5.TimerList -} - -type StatMgr struct { - onlineUserHash map[string]*OnlineUser - - onlineNumHash map[int64]int32 - onlineNumMutex sync.RWMutex -} - -func (this *StatMgr) Init() *StatMgr { - this.onlineUserHash = make(map[string]*OnlineUser) - this.onlineNumHash = make(map[int64]int32) - - f5.App.RegisterIMMsgHandle(IM_UPDATE_SESSION, this._IMUpdateSession) - G.HttpServer.RegisterHandle("Stat", "updateSession", this.__statUpdateSession) - G.HttpServer.RegisterHandle("Stat", "getRealTimeOnline", this.__statGetRealTimeOnline) - - return this -} - -func (this *StatMgr) UnInit() { - -} - -func (this *StatMgr) _IMUpdateSession(msgId int16, params* q5.XParams) { - /*if q5.Debug() { - f5.SysLog().Info("StatMgr._IMUpdateSession") - }*/ - accountId := params.Sender.GetString() - sessionId := params.Param1.GetString() - remoteAddr := params.Param2.GetString() - tryCount := params.Param3.GetInt32() - if tryCount > 0 { - return - } - instanceId := this.allocInstanceId(accountId) - if instanceId == f5.App.GetInstanceId() { - userInfo, ok := this.onlineUserHash[accountId] - if !ok { - this.playerOnline(remoteAddr, accountId, sessionId) - } else { - if userInfo.sessionId != sessionId { - if !f5.IsValidSessionId(accountId, sessionId, SESSION_KEY) { - f5.SysLog().Warning("invalid sessionid1 %s %s %s", - remoteAddr, - accountId, - sessionId) - return - } - userInfo.sessionId = sessionId - } - gameId := f5.ExtractGameIdFromAccountId(accountId) - f5.Timer().ModifyTimer( - userInfo.timer, - G.MetaMgr.GetConf().GetHeartBeatTimeout(gameId)) - } - } -} - -func (this *StatMgr) GetRealTimeOnline(gameId int32, channel int32) int32 { - this.onlineNumMutex.Lock() - defer this.onlineNumMutex.Unlock() - if v, ok := this.onlineNumHash[q5.MkInt64(gameId, channel)]; ok { - return v - } else { - return 0 - } -} - -func (this *StatMgr) incGameChannelOnline(gameId int32, channel int32) { - this.onlineNumMutex.Lock() - defer this.onlineNumMutex.Unlock() - this.onlineNumHash[q5.MkInt64(gameId, channel)] += 1 - this.onlineNumHash[q5.MkInt64(gameId, 0)] += 1 -} - -func (this *StatMgr) decGameChannelOnline(gameId int32, channel int32) { - this.onlineNumMutex.Lock() - defer this.onlineNumMutex.Unlock() - this.onlineNumHash[q5.MkInt64(gameId, channel)] -= 1 - this.onlineNumHash[q5.MkInt64(gameId, 0)] -= 1 -} - -func (this *StatMgr) allocInstanceId(accountId string) uint32 { - return 1 - //return q5.Crc32(accountId) -} - -func (this *StatMgr) playerOnline(remoteAddr string, accountId string, sessionId string) { - if q5.Debug() { - f5.SysLog().Info("playerOnline %s %s %s", - remoteAddr, - accountId, - sessionId) - } - if !f5.IsValidSessionId(accountId, sessionId, SESSION_KEY) { - f5.SysLog().Warning("invalid sessionid2 %s %s %s", - remoteAddr, - accountId, - sessionId) - return - } - gameId := f5.ExtractGameIdFromAccountId(accountId) - channel := f5.ExtractChannelFromAccountId(accountId) - registerTime := f5.ExtractRegisterTimeFromSessionId(sessionId) - userInfo := new(OnlineUser) - userInfo.timer = f5.Timer().AddDeadLineTimer( - G.MetaMgr.GetConf().GetHeartBeatTimeout(gameId), - func (params *q5.XParams) { - params.Sender.SetString("") - params.Param1.SetString(accountId) - params.Param3.SetInt32(registerTime) - }, - func (params *q5.XParams) { - this.playerOffline(remoteAddr, accountId, registerTime) - }) - this.onlineUserHash[accountId] = userInfo - this.incGameChannelOnline(gameId, channel) - G.GameLog.PlayerOnline(remoteAddr, accountId, int64(registerTime)) -} - -func (this *StatMgr) playerOffline(remoteAddr string, accountId string, registerTime int32) { - if q5.Debug() { - f5.SysLog().Info("playerOffline %s %s", - remoteAddr, - accountId) - } - gameId := f5.ExtractGameIdFromAccountId(accountId) - channel := f5.ExtractChannelFromAccountId(accountId) - delete(this.onlineUserHash, accountId) - this.decGameChannelOnline(gameId, channel) - G.GameLog.PlayerOffline(remoteAddr, accountId, int64(registerTime), 0) -} - -func (this *StatMgr) __statUpdateSession(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, "_try_count").GetString()) - })) - w.Write([]byte(`{"errcode":0, "errmsg":""}`)) -} - -func (this *StatMgr) __statGetRealTimeOnline(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))) -}