This commit is contained in:
aozhiwei 2020-10-27 21:14:37 +08:00
parent ddc3ea0925
commit 0860ec00d8
7 changed files with 5 additions and 275 deletions

View File

@ -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()

View File

@ -7,7 +7,6 @@ import (
type GlobalVar struct {
MetaMgr *MetaMgr
HttpServer *f5.HttpServer
StatMgr *StatMgr
GameLog *GameLog
}

View File

@ -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())
}

View File

@ -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
}

View File

@ -1,4 +1,4 @@
module statserver
module analyseapi
go 1.11

View File

@ -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"

View File

@ -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)))
}