Apply .gitignore rules

This commit is contained in:
皮蛋13361098506
2025-01-06 16:21:36 +08:00
parent 1b77f62820
commit ccd2c530cf
580 changed files with 69806 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
package qcloud
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"goutil/mathUtil"
"goutil/webUtil"
)
const (
VOICE_CAPTCHA_URL = "https://cloud.tim.qq.com/v5/tlsvoicesvr/sendcvoice"
VOICE_NOTIFICATION_URL = "https://cloud.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt"
VOICE_TEMPLATE_NOTIFICATION_URL = "https://cloud.tim.qq.com/v5/tlsvoicesvr/sendtvoice"
)
// calculate sign-string for phone numbers
func calcSig(appKey string, rand int, timeStamp int64, mobile string) string {
sum := sha256.Sum256([]byte(fmt.Sprintf("appkey=%s&random=%d&time=%d&mobile=%s", appKey, rand, timeStamp, mobile)))
return hex.EncodeToString(sum[:])
}
// do the http request and parse the response
func request(url string, data map[string]interface{}, appId string, rand int) (success bool, err error) {
url = fmt.Sprintf("%s?sdkappid=%s&random=%d", url, appId, rand)
fmt.Printf("url:%s\n", url)
contentBytes, err := json.Marshal(data)
if err != nil {
return
}
fmt.Printf("data:%v\n", string(contentBytes))
_, retBytes, err := webUtil.PostByteData2(url, contentBytes, nil, nil)
if err != nil {
return
}
var responseObj *response
err = json.Unmarshal(retBytes, &responseObj)
if err != nil {
return
}
if responseObj.Result != 0 {
err = fmt.Errorf(responseObj.ErrMsg)
return
}
success = true
return
}
func SendVoiceCaptcha(appId, appKey, nation, mobile, captcha string, playTimes int) (success bool, err error) {
rand := mathUtil.GetRand().GetRandRangeInt(100000, 999999)
timeStamp := time.Now().Unix()
data := make(map[string]interface{})
data["playtimes"] = playTimes
data["sig"] = calcSig(appKey, rand, timeStamp, mobile)
data["tel"] = newTelField(nation, mobile)
data["time"] = timeStamp
data["ext"] = ""
// dedicated param
data["msg"] = captcha
success, err = request(VOICE_CAPTCHA_URL, data, appId, rand)
return
}
func SendVoiceNotification(appId, appKey, nation, mobile, prompt string, playTimes int) (success bool, err error) {
rand := mathUtil.GetRand().GetRandRangeInt(100000, 999999)
timeStamp := time.Now().Unix()
promptType := 2
data := make(map[string]interface{})
data["playtimes"] = playTimes
data["sig"] = calcSig(appKey, rand, timeStamp, mobile)
data["tel"] = newTelField(nation, mobile)
data["time"] = timeStamp
data["ext"] = ""
// dedicated param
data["promptfile"] = prompt
data["prompttype"] = promptType
success, err = request(VOICE_NOTIFICATION_URL, data, appId, rand)
return
}
func SendVoiceTemplateNotification(appId, appKey, nation, mobile string, templateId int, params []string, playTimes int) (success bool, err error) {
rand := mathUtil.GetRand().GetRandRangeInt(100000, 999999)
timeStamp := time.Now().Unix()
data := make(map[string]interface{})
data["playtimes"] = playTimes
data["sig"] = calcSig(appKey, rand, timeStamp, mobile)
data["tel"] = newTelField(nation, mobile)
data["time"] = timeStamp
data["ext"] = ""
// dedicated param
data["tpl_id"] = templateId
data["params"] = params
success, err = request(VOICE_TEMPLATE_NOTIFICATION_URL, data, appId, rand)
return
}

View File

@@ -0,0 +1,79 @@
package gameServerMgr
import (
"time"
. "Framework/managecenterModel"
"goutil/timeUtil"
"goutil/typeUtil"
)
var (
mServerGroupObj *ServerGroup
)
//解析服务器组信息
func ParseServerGroupInfo(serverGroupObj *ServerGroup) {
mServerGroupObj = serverGroupObj
}
//获取服务器组对象
func GetServerGroup() (serverGroupObj *ServerGroup) {
serverGroupObj = mServerGroupObj
return
}
//检查服务器是否在维护
func CheckMaintainStatus() (maintainMessage string, isMaintaining bool) {
serverGroupObj := GetServerGroup()
nowTick := time.Now().Unix()
if serverGroupObj.GroupState == int32(Con_GroupState_Maintain) || (serverGroupObj.MaintainBeginTimeTick <= nowTick && nowTick <= serverGroupObj.MaintainBeginTimeTick+int64(60*serverGroupObj.MaintainMinutes)) {
maintainMessage = serverGroupObj.MaintainMessage
isMaintaining = true
return
}
return
}
//获取服务器维护开始时间时间戳
func GetMaintainBeginTime() (maintainBeginTimeTick int64) {
serverGroupObj := GetServerGroup()
maintainBeginTimeTick = serverGroupObj.MaintainBeginTimeTick
return
}
//获取服务器维护持续时间 单位分钟
func GetMaintainMinutes() (maintainMinutes int32) {
serverGroupObj := GetServerGroup()
maintainMinutes = serverGroupObj.MaintainMinutes
return
}
//获取服务器开服日期 时间戳
func GetServerOpenDate() (openTimeTick int64) {
serverGroupObj := GetServerGroup()
openTimeTick = serverGroupObj.OpenTimeTick
return
}
//当前服务器已开服天数(开服第几天)
//当前开服天数 计算公式:(当前日期 - 开服日期)的总天数 + 1
func ServerOpenDays() (days int32) {
serverGroupObj := GetServerGroup()
if serverGroupObj.IsOpen() == false {
return 0
}
//(当前日期 - 开服日期)的总天数 + 1
openTimeTick := serverGroupObj.OpenTimeTick
openDate, _ := typeUtil.DateTime(openTimeTick)
days = int32(timeUtil.SubDay(time.Now(), openDate) + 1)
return
}

View File

@@ -0,0 +1,225 @@
package syncUtil
import (
"context"
"fmt"
"sync"
"testing"
"time"
)
func TestMutex(t *testing.T) {
mu := NewMutex()
mu.Lock()
defer mu.UnLock()
if mu.TryLock() {
t.Errorf("cannot fetch mutex !!!")
}
}
func TestMutexTryLockTimeout(t *testing.T) {
fmt.Println("start")
mu := NewMutex()
mu.Lock()
go func() {
time.Sleep(20 * time.Second)
mu.UnLock()
}()
// if !mu.TryLockTimeout(500 * time.Microsecond) {
// t.Errorf("cannot fetch mutex in 500us !!!")
// }
if !mu.TryLockTimeout(15 * time.Second) {
t.Errorf("should fetch mutex in 5ms !!!")
}
mu.UnLock()
}
func TestMutexUnlockTwice(t *testing.T) {
mu := NewMutex()
mu.Lock()
defer func() {
if x := recover(); x != nil {
if x != "unlock of unlocked mutex" {
t.Errorf("unexpect panic")
}
} else {
t.Errorf("should panic after unlock twice")
}
}()
mu.UnLock()
mu.UnLock()
}
func TestMutexTryLockContext(t *testing.T) {
mu := NewMutex()
ctx, cancel := context.WithCancel(context.Background())
mu.Lock()
go func() {
time.Sleep(10 * time.Millisecond)
cancel()
}()
if mu.TryLockContext(ctx) {
t.Errorf("cannot fetch mutex !!!")
}
}
func BenchmarkMutex(b *testing.B) {
mu := NewMutex()
a := 0
c := 0
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
mu.Lock()
a++
mu.UnLock()
mu.Lock()
c = a
mu.UnLock()
}
})
_ = a
_ = c
}
func TestMutexGroup(t *testing.T) {
mu := NewMutexGroup()
mu.Lock("g")
defer mu.UnLock("g")
if mu.TryLock("g") {
t.Errorf("cannot fetch mutex !!!")
}
}
func TestMutexGroupMutliWaitLock(t *testing.T) {
var (
wg sync.WaitGroup
mu = NewMutexGroup()
cn = 3
)
for i := 0; i < cn; i++ {
wg.Add(1)
go func() {
mu.Lock("h")
time.Sleep(1e7)
mu.UnLock("h")
wg.Done()
}()
}
wg.Wait()
for i := 0; i < cn; i++ {
wg.Add(1)
go func() {
mu.Lock("g")
time.Sleep(1e7)
mu.UnLockAndFree("g")
wg.Done()
}()
}
wg.Wait()
}
func TestMutexGroupUnLockAndFree(t *testing.T) {
var (
wg sync.WaitGroup
mu = NewMutexGroup()
mg = mu.(*mutexGroup)
)
for j := 1; j < 5; j++ {
for i := 0; i < j; i++ {
wg.Add(1)
go func() {
mu.Lock("h")
time.Sleep(1e6)
mu.UnLockAndFree("h")
wg.Done()
}()
}
wg.Wait()
mg.mu.Lock()
if _, ok := mg.group["h"]; ok {
t.Error("h mutex exist after UnLockAndFree")
}
mg.mu.Unlock()
}
}
func TestMutexGroupTryLockFailedAndUnLockAndFree(t *testing.T) {
var (
wg sync.WaitGroup
mu = NewMutexGroup()
mg = mu.(*mutexGroup)
)
for j := 1; j < 5; j++ {
for i := 0; i < j; i++ {
wg.Add(1)
go func() {
if mu.TryLock("h") {
time.Sleep(1e6)
mu.UnLockAndFree("h")
}
wg.Done()
}()
}
wg.Wait()
mg.mu.Lock()
if _, ok := mg.group["h"]; ok {
t.Error("h mutex exist after UnLockAndFree")
}
mg.mu.Unlock()
}
}
func TestMutexGroupTryLockTimeout(t *testing.T) {
mu := NewMutexGroup()
mu.Lock("g")
go func() {
time.Sleep(1 * time.Millisecond)
mu.UnLock("g")
}()
if mu.TryLockTimeout("g", 500*time.Microsecond) {
t.Errorf("cannot fetch mutex in 500us !!!")
}
if !mu.TryLockTimeout("g", 5*time.Millisecond) {
t.Errorf("should fetch mutex in 5ms !!!")
}
mu.UnLock("g")
}
func TestMutexGroupTryLockContext(t *testing.T) {
mu := NewMutexGroup()
ctx, cancel := context.WithCancel(context.Background())
mu.Lock("g")
go func() {
time.Sleep(10 * time.Millisecond)
cancel()
}()
if mu.TryLockContext("g", ctx) {
t.Errorf("cannot fetch mutex !!!")
}
}
func BenchmarkMutexGroup(b *testing.B) {
mu := NewMutexGroup()
a := 0
c := 0
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
mu.Lock("g")
a++
mu.UnLock("g")
mu.Lock("g")
c = a
mu.UnLock("g")
}
})
_ = a
_ = c
}