From 51f179c1de1b1018bc8a8cff2a097a6f24d555e8 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 7 Sep 2020 16:57:59 +0800 Subject: [PATCH] 1 --- app.go | 61 +++++++++++++++++++++++++++++++++++++++-- f5types.go | 13 +++++++++ metamgr.go | 32 +++++++++++++--------- sysutils.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 15 deletions(-) create mode 100644 f5types.go create mode 100644 sysutils.go diff --git a/app.go b/app.go index c763f0b..2df5d03 100644 --- a/app.go +++ b/app.go @@ -1,14 +1,23 @@ package f5 +import "flag" +import "sync" +import "time" import "q5" type App_ struct { - nodeId int32 - instanceId int32 + nodeId int + instanceId int terminated bool flags map[int32]int32 servicing bool contextHash map[int32]q5.XParams + loopCond sync.Cond + imTopNode *IMMsgNode + imBotNode *IMMsgNode + imWorkNode *IMMsgNode + imMsgMutex sync.RWMutex + chGoLoopTimerExit chan int } func (this *App_) Init() { @@ -18,9 +27,15 @@ func (this *App_) Init() { nil, 1000 * 60, 5000) + flag.IntVar(&this.nodeId, "n", 0, "node id") + flag.IntVar(&this.instanceId, "i", 0, "instance id") + flag.Parse() + this.chGoLoopTimerExit = make(chan int) + go this.goLoopTimer() } func (this *App_) UnInit() { + this.chGoLoopTimerExit <- 1 _Timer.UnInit() _Timer = nil } @@ -28,6 +43,7 @@ func (this *App_) UnInit() { func (this *App_) Run() { for !this.terminated { _Timer.Update() + this.loopCond.Wait() } } @@ -35,7 +51,48 @@ func (this *App_) NewUuid() int64 { return 0 } +func (this *App_) GetInstanceId() uint32 { + return uint32(this.instanceId) +} + +func (this *App_) GetNodeId() uint32 { + return uint32(this.nodeId) +} + func (this *App_) HasFlag(flag int32) bool { _, ok := this.flags[flag] return ok } + +func (this *App_) AddIMMsg(msgId int16, params *q5.XParams) { + p := new(IMMsgNode) + p.msgId = msgId + p.params = params + + { + this.imMsgMutex.Lock() + defer this.imMsgMutex.Unlock() + if this.imBotNode != nil { + this.imBotNode.next = p + this.imBotNode = p + } else { + this.imTopNode = p + this.imBotNode = p + } + } + this.loopCond.Broadcast() +} + +func (this *App_) RegisterIMMsgHandle(msgId int16, handle func(int16,*q5.XParams)) { +} + +func (this *App_) goLoopTimer() { + for { + select { + case <-this.chGoLoopTimerExit: + return + case <-time.After(time.Millisecond): + this.loopCond.Broadcast() + } + } +} diff --git a/f5types.go b/f5types.go new file mode 100644 index 0000000..1673529 --- /dev/null +++ b/f5types.go @@ -0,0 +1,13 @@ +package f5 + +import "q5" + +type MsgNode struct { + +} + +type IMMsgNode struct { + msgId int16 + params *q5.XParams + next *IMMsgNode +} diff --git a/metamgr.go b/metamgr.go index 04c2d09..54babfe 100644 --- a/metamgr.go +++ b/metamgr.go @@ -18,6 +18,7 @@ type MetaClass struct { WrapMeta interface{} PrimKey string SecKey string + IsObject bool } type MetaMgr struct { @@ -50,7 +51,7 @@ func (this *MetaMgr) Load() { } func (this *MetaMgr) loadRawMetaTable() { - for key, val := range *this.metaClasses { + for _, val := range *this.metaClasses { rawMeta, _ := this.loadJson(&val) values := rawMeta.Elem().FieldByName("Values") wrapMetaType := reflect.TypeOf(val.WrapMeta) @@ -66,32 +67,32 @@ func (this *MetaMgr) loadRawMetaTable() { wrapMetaList = reflect.Append(wrapMetaList, wrapMeta) } - this.rawList[key] = rawMeta - this.wrapList[key] = wrapMetaList.Interface() - this.wrapIdHash[key] = map[int64]interface{}{} - this.wrapNameHash[key] = map[string]interface{}{} + this.rawList[val.Idx] = rawMeta + this.wrapList[val.Idx] = wrapMetaList.Interface() + this.wrapIdHash[val.Idx] = map[int64]interface{}{} + this.wrapNameHash[val.Idx] = map[string]interface{}{} } } func (this *MetaMgr) bindPrimKey() { - for key, val := range *this.metaClasses { - wrapMetaList := reflect.ValueOf(this.wrapList[key]) + for _, val := range *this.metaClasses { + wrapMetaList := reflect.ValueOf(this.wrapList[val.Idx]) for i := 0; i < wrapMetaList.Len(); i++ { wrapMeta := wrapMetaList.Index(i) if val.PrimKey == "" { - this.wrapIdHash[key][int64(i + 1)] = wrapMeta.Interface() + this.wrapIdHash[val.Idx][int64(i + 1)] = wrapMeta.Interface() continue } primVal := wrapMeta.Elem().FieldByName(val.PrimKey) secVal := wrapMeta.Elem().FieldByName(val.SecKey) if fieldVal, ok := primVal.Interface().(*string); ok { - this.wrapNameHash[key][*fieldVal] = wrapMeta + this.wrapNameHash[val.Idx][*fieldVal] = wrapMeta } else if fieldVal, ok := primVal.Interface().(*int32); ok { if val.SecKey == "" { - this.wrapIdHash[key][int64(*fieldVal)] = wrapMeta.Interface() + this.wrapIdHash[val.Idx][int64(*fieldVal)] = wrapMeta.Interface() } else { if subFieldVal, ok := secVal.Interface().(*int32); ok { - this.wrapIdHash[key][int64(*subFieldVal)] = wrapMeta.Interface() + this.wrapIdHash[val.Idx][int64(*subFieldVal)] = wrapMeta.Interface() } else { } @@ -105,8 +106,13 @@ func (this *MetaMgr) bindPrimKey() { func (this *MetaMgr) loadJson(metaClass *MetaClass) (reflect.Value, error) { f, _ := os.Open(metaClass.FileName) data, _ := bufio.NewReader(f).ReadString(0) - data = "{\"values\":" + data - data += "}" + if metaClass.IsObject { + data = "{\"values\":[" + data + data += "]}" + } else { + data = "{\"values\":" + data + data += "}" + } u := jsonpb.Unmarshaler{AllowUnknownFields: true} msgType := reflect.TypeOf(metaClass.RawMeta).Elem() diff --git a/sysutils.go b/sysutils.go new file mode 100644 index 0000000..b060891 --- /dev/null +++ b/sysutils.go @@ -0,0 +1,79 @@ +package f5 + +import "os" +import "strings" +import "q5" + +const ( + ONLINE_ENV = 0 + TEST_ENV + DEV_ENV +) + +var serverEnv int32 + +func IsTestEnv() bool { + return serverEnv == TEST_ENV +} + +func IsOnlineEnv() bool { + return serverEnv == ONLINE_ENV +} + +func IsDevEnv() bool { + return serverEnv == DEV_ENV +} + +func IsValidSessionId(accountId string, sessionId string, key string) bool { + tmpStrings := strings.Split(sessionId, "_") + if len(tmpStrings) < 4 { + return false + } + sessionCreateTime := new(q5.XValue).SetString(tmpStrings[0]).GetString() + registerTime := new(q5.XValue).SetString(tmpStrings[1]).GetString() + md5Str := new(q5.XValue).SetString(tmpStrings[2]).GetString() + return q5.Md5Str(accountId + registerTime + sessionCreateTime) == md5Str +} + +func ExtractRegisterTimeFromSessionId(sessionId string) int32 { + tmpStrings := strings.Split(sessionId, "_") + if len(tmpStrings) < 4 { + return 0 + } + return new(q5.XValue).SetString(tmpStrings[1]).GetInt32() +} + +func ExtractGameIdFromAccountId(accountId string) int32 { + tmpStrings := strings.Split(accountId, "_") + if len(tmpStrings) < 2 { + return 0 + } + return new(q5.XValue).SetString(tmpStrings[1]).GetInt32() +} + +func ExtractChannelFromAccountId(accountId string) int32 { + tmpStrings := strings.Split(accountId, "_") + if len(tmpStrings) < 2 { + return 0 + } + return new(q5.XValue).SetString(tmpStrings[0]).GetInt32() +} + +func ExtractOpenIdFromAccountId(accountId string) string { + tmpStrings := strings.Split(accountId, "_") + if len(tmpStrings) < 3 { + return "" + } + return strings.Join(tmpStrings[2:], "_") +} + +func init() { + switch os.Getenv("SERVER_ENV") { + case "TEST": + serverEnv = TEST_ENV + case "DEBUG": + serverEnv = DEV_ENV + default: + serverEnv = ONLINE_ENV + } +}