Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package httpServer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
// 处理方法集合
|
||||
// Key:API名
|
||||
handlerData = make(map[string]*ApiHandler)
|
||||
|
||||
// 文档方法
|
||||
remarkFunc func(w http.ResponseWriter, r *http.Request)
|
||||
)
|
||||
|
||||
// RegisterRemarkFunc
|
||||
// @description: 注册文档api方法
|
||||
// parameter:
|
||||
// @_remarkFunc: _remarkFunc
|
||||
// return:
|
||||
func RegisterRemarkFunc(_remarkFunc func(w http.ResponseWriter, r *http.Request)) {
|
||||
remarkFunc = _remarkFunc
|
||||
}
|
||||
|
||||
// RegisterHandleFunc
|
||||
// @description: 注册处理方法
|
||||
// parameter:
|
||||
// @apiName: API名称
|
||||
// @callback: 回调方法
|
||||
// @paramNames: 参数名称集合
|
||||
// return:
|
||||
func RegisterHandleFunc(apiName string, callback HandleFunc, paramNames ...string) {
|
||||
apiFullName := fmt.Sprintf("/API/%s", apiName)
|
||||
|
||||
if _, exist := handlerData[apiFullName]; exist {
|
||||
panic(fmt.Errorf("重复注册处理函数:%s", apiFullName))
|
||||
}
|
||||
|
||||
handlerData[apiFullName] = newApiHandler(apiFullName, callback, paramNames...)
|
||||
}
|
||||
|
||||
// GetHandleFunc
|
||||
// @description: 获取请求方法
|
||||
// parameter:
|
||||
// @apiFullName: 方法名称
|
||||
// return:
|
||||
// @*ApiHandler: 请求方法
|
||||
// @bool: 是否存在
|
||||
func GetHandleFunc(apiFullName string) (*ApiHandler, bool) {
|
||||
if requestFuncObj, exists := handlerData[apiFullName]; exists {
|
||||
return requestFuncObj, exists
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
package mathUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//SafeRand 安全的随机
|
||||
type SafeRand struct {
|
||||
*rand.Rand
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func GetSafeRand() *SafeRand {
|
||||
return &SafeRand{
|
||||
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
}
|
||||
}
|
||||
|
||||
func GetSafeRandInSeed(seed int64) *SafeRand {
|
||||
return &SafeRand{
|
||||
Rand: rand.New(rand.NewSource(seed)),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *SafeRand) Int() int {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Int()
|
||||
}
|
||||
|
||||
func (this *SafeRand) Intn(n int) int {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Intn(n)
|
||||
}
|
||||
|
||||
func (this *SafeRand) Int31() int32 {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Int31()
|
||||
}
|
||||
|
||||
func (this *SafeRand) Int31n(n int32) int32 {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Int31n(n)
|
||||
}
|
||||
|
||||
func (this *SafeRand) Int63() int64 {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Int63()
|
||||
}
|
||||
|
||||
func (this *SafeRand) Int63n(n int64) int64 {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Int63n(n)
|
||||
}
|
||||
|
||||
func (this *SafeRand) Float64() float64 {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Float64()
|
||||
}
|
||||
|
||||
func (this *SafeRand) Float32() float32 {
|
||||
this.mu.Lock()
|
||||
defer this.mu.Unlock()
|
||||
|
||||
return this.Rand.Float32()
|
||||
}
|
||||
|
||||
// 获取指定区间的随机数[lower, upper)
|
||||
// lower:区间下限
|
||||
// upper:区间上限
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandRangeInt(lower, upper int) int {
|
||||
return lower + this.Intn(upper-lower)
|
||||
}
|
||||
|
||||
// 获取指定区间的随机数[lower, upper)
|
||||
// lower:区间下限
|
||||
// upper:区间上限
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandRangeInt32(lower, upper int32) int32 {
|
||||
return lower + this.Int31n(upper-lower)
|
||||
}
|
||||
|
||||
// 获取指定区间的随机数[lower, upper)
|
||||
// lower:区间下限
|
||||
// upper:区间上限
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandRangeInt64(lower, upper int64) int64 {
|
||||
return lower + this.Int63n(upper-lower)
|
||||
}
|
||||
|
||||
// 获取随机数[0, n)
|
||||
// randObj:随机对象
|
||||
// n:范围上限
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandInt(n int) int {
|
||||
return this.Intn(n)
|
||||
}
|
||||
|
||||
// 获取随机数[0, n)
|
||||
// n:范围上限
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandInt32(n int32) int32 {
|
||||
return this.Int31n(n)
|
||||
}
|
||||
|
||||
// 获取随机数[0, n)
|
||||
// randObj:随机对象
|
||||
// n:范围上限
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandInt64(n int64) int64 {
|
||||
return this.Int63n(n)
|
||||
}
|
||||
|
||||
// 获取随机数[0, 1)
|
||||
// randObj:随机对象
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandFloat32() float32 {
|
||||
return this.Float32()
|
||||
}
|
||||
|
||||
// 获取随机数[0, 1)
|
||||
// 返回值:随机数
|
||||
func (this *SafeRand) GetRandFloat64() float64 {
|
||||
return this.Float64()
|
||||
}
|
||||
|
||||
// 获取随机数列表(1~10000,超过10000会抛出异常)
|
||||
// minValue:获取随机数的区间下限值
|
||||
// maxValue:获取随机数的区间上限值
|
||||
// count:随机数量
|
||||
// ifAllowDuplicate:是否允许重复
|
||||
// 返回值
|
||||
// 随机数列表
|
||||
func (this *SafeRand) GetRandNumList(minValue, maxValue, count int, ifAllowDuplicate bool) ([]int, error) {
|
||||
if minValue > maxValue {
|
||||
return nil, errors.New("minValue can't be bigger than maxValue.")
|
||||
}
|
||||
|
||||
if !ifAllowDuplicate && (maxValue-minValue+1) < count {
|
||||
return nil, errors.New("随机的数量超过区间的元素数量")
|
||||
}
|
||||
|
||||
if (maxValue - minValue + 1) > 10000 {
|
||||
return nil, errors.New("随机数的区间不能大于10000")
|
||||
}
|
||||
|
||||
// 定义原始数据
|
||||
sourceCount := maxValue - minValue + 1
|
||||
source := make([]int, sourceCount, sourceCount)
|
||||
for index := 0; index < sourceCount; index++ {
|
||||
source[index] = minValue + index
|
||||
}
|
||||
|
||||
// 定义返回值
|
||||
resultList := make([]int, 0, count)
|
||||
|
||||
// 获取随机的索引列表
|
||||
randIndextList := this.getRandIndexList(len(source), count, ifAllowDuplicate)
|
||||
for _, index := range randIndextList {
|
||||
// 判断是否已经取到足够数量的数据?
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
resultList = append(resultList, source[index])
|
||||
count -= 1
|
||||
}
|
||||
|
||||
return resultList, nil
|
||||
}
|
||||
|
||||
// 获取随机的int列表
|
||||
// source:源列表
|
||||
// count:随机数量
|
||||
// ifAllowDuplicate:是否允许重复
|
||||
// 返回值
|
||||
// 随机数列表
|
||||
func (this *SafeRand) GetRandIntList(source []int, count int, ifAllowDuplicate bool) ([]int, error) {
|
||||
// 在不允许重复的情况下,需要产生的随机数不能超过范围限制
|
||||
if ifAllowDuplicate == false && len(source) < count {
|
||||
return nil, errors.New("随机的数量超过列表的元素数量")
|
||||
}
|
||||
|
||||
// 定义返回值
|
||||
resultList := make([]int, 0, count)
|
||||
|
||||
// 获取随机的索引列表
|
||||
randIndextList := this.getRandIndexList(len(source), count, ifAllowDuplicate)
|
||||
for _, index := range randIndextList {
|
||||
// 判断是否已经取到足够数量的数据?
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
resultList = append(resultList, source[index])
|
||||
count -= 1
|
||||
}
|
||||
|
||||
return resultList, nil
|
||||
}
|
||||
|
||||
// 获取随机的int32列表
|
||||
// source:源列表
|
||||
// count:随机数量
|
||||
// ifAllowDuplicate:是否允许重复
|
||||
// 返回值
|
||||
// 随机数列表
|
||||
func (this *SafeRand) GetRandInt32List(source []int32, count int, ifAllowDuplicate bool) ([]int32, error) {
|
||||
// 在不允许重复的情况下,需要产生的随机数不能超过范围限制
|
||||
if ifAllowDuplicate == false && len(source) < count {
|
||||
return nil, errors.New("随机的数量超过列表的元素数量")
|
||||
}
|
||||
|
||||
// 定义返回值
|
||||
resultList := make([]int32, 0, count)
|
||||
|
||||
// 获取随机的索引列表
|
||||
randIndextList := this.getRandIndexList(len(source), count, ifAllowDuplicate)
|
||||
for _, index := range randIndextList {
|
||||
// 判断是否已经取到足够数量的数据?
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
resultList = append(resultList, source[index])
|
||||
count -= 1
|
||||
}
|
||||
|
||||
return resultList, nil
|
||||
}
|
||||
|
||||
// 获取随机的int64列表
|
||||
// source:源列表
|
||||
// count:随机数量
|
||||
// ifAllowDuplicate:是否允许重复
|
||||
// 返回值
|
||||
// 随机数列表
|
||||
func (this *SafeRand) GetRandInt64List(source []int64, count int, ifAllowDuplicate bool) ([]int64, error) {
|
||||
// 在不允许重复的情况下,需要产生的随机数不能超过范围限制
|
||||
if ifAllowDuplicate == false && len(source) < count {
|
||||
return nil, errors.New("随机的数量超过列表的元素数量")
|
||||
}
|
||||
|
||||
// 定义返回值
|
||||
resultList := make([]int64, 0, count)
|
||||
|
||||
// 获取随机的索引列表
|
||||
randIndextList := this.getRandIndexList(len(source), count, ifAllowDuplicate)
|
||||
for _, index := range randIndextList {
|
||||
// 判断是否已经取到足够数量的数据?
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
resultList = append(resultList, source[index])
|
||||
count -= 1
|
||||
}
|
||||
|
||||
return resultList, nil
|
||||
}
|
||||
|
||||
// 获取随机的interface{}列表
|
||||
// source:源列表
|
||||
// count:随机数量
|
||||
// ifAllowDuplicate:是否允许重复
|
||||
// 返回值
|
||||
// 随机数列表
|
||||
func (this *SafeRand) GetRandInterfaceList(source []interface{}, count int, ifAllowDuplicate bool) ([]interface{}, error) {
|
||||
// 在不允许重复的情况下,需要产生的随机数不能超过范围限制
|
||||
if ifAllowDuplicate == false && len(source) < count {
|
||||
return nil, errors.New("随机的数量超过列表的元素数量")
|
||||
}
|
||||
|
||||
// 定义返回值
|
||||
resultList := make([]interface{}, 0, count)
|
||||
|
||||
// 获取随机的索引列表
|
||||
randIndextList := this.getRandIndexList(len(source), count, ifAllowDuplicate)
|
||||
for _, index := range randIndextList {
|
||||
// 判断是否已经取到足够数量的数据?
|
||||
if count <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
resultList = append(resultList, source[index])
|
||||
count -= 1
|
||||
}
|
||||
|
||||
return resultList, nil
|
||||
}
|
||||
|
||||
// 获取随机的索引列表
|
||||
// count:数量
|
||||
// ifAllowDuplicate:是否允许重复
|
||||
// 返回值
|
||||
// 随机索引值列表
|
||||
func (this *SafeRand) getRandIndexList(maxNum, count int, ifAllowDuplicate bool) []int {
|
||||
// 定义返回值
|
||||
randIndextList := make([]int, 0, count)
|
||||
|
||||
// 使用源列表的数据量来初始化一个仅存放索引值的数组
|
||||
indexList := make([]int, maxNum, maxNum)
|
||||
for index := 0; index < maxNum; index++ {
|
||||
indexList[index] = index
|
||||
}
|
||||
|
||||
// 遍历列表并获取随机对象(通过不断缩小随机的范围来实现)
|
||||
maxIndex := len(indexList) - 1
|
||||
for {
|
||||
if len(randIndextList) < count {
|
||||
// 获取随机索引(由于Next方法不取上限值,所以需要maxIndex+1)
|
||||
randIndex := this.Intn(maxIndex + 1)
|
||||
|
||||
// 将数据添加到列表,并增加findCount
|
||||
randIndextList = append(randIndextList, indexList[randIndex])
|
||||
|
||||
// 如果不允许重复,则需要特殊处理
|
||||
if !ifAllowDuplicate {
|
||||
// 将该位置的数据和最大位置的数据进行交换
|
||||
indexList[randIndex], indexList[maxIndex] = indexList[maxIndex], indexList[randIndex]
|
||||
|
||||
// 将随机的范围缩小
|
||||
maxIndex -= 1
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return randIndextList
|
||||
}
|
||||
|
||||
// 获取带权重的随机数据
|
||||
// source:源数据
|
||||
// 返回值
|
||||
// 数据项
|
||||
// 错误对象
|
||||
func (this *SafeRand) GetRandWeight(source []IWeight) (result IWeight, err error) {
|
||||
if source == nil || len(source) == 0 {
|
||||
err = errors.New("待随机的列表为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 计算出总的数据量,并随机一个[0, total)的值
|
||||
total := 0
|
||||
for _, item := range source {
|
||||
total += item.GetWeight()
|
||||
}
|
||||
|
||||
randNum := this.GetRandInt(total)
|
||||
|
||||
// 根据随机出来的值,判断位于哪个区间
|
||||
total = 0
|
||||
for _, item := range source {
|
||||
total += item.GetWeight()
|
||||
if randNum < total {
|
||||
result = item
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = errors.New("未找到有效的数据")
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
用于生成唯一的、递增的Id。生成的规则如下:
|
||||
1、生成的Id包含一个固定前缀值
|
||||
2、为了生成尽可能多的不重复数字,所以使用int64来表示一个数字,其中:
|
||||
0 000000000000000 0000000000000000000000000000 00000000000000000000
|
||||
第一部分:1位,固定为0
|
||||
第二部分:共IdentifierBit位,表示固定唯一标识(机器号或者服务器Id等)。范围为[0, math.Pow(2, IdentifierBit))
|
||||
第三部分:共ConstructCountBit位,表示对象被构造的次数。范围为[0, math.Pow(2, ConstructCountBit)),以2020-1-1 00:00:00为基准
|
||||
第四部分:共SeedBit位,表示自增种子。范围为[0, math.Pow(2, SeedBit))
|
||||
3、总体而言,此规则支持总共创建math.Pow(2, ConstructCountBit)次对象,并且每次对象构造期间生成math.Pow(2, SeedBit)个不同的数字
|
||||
*/
|
||||
|
||||
/*
|
||||
修改记录:
|
||||
2020-03-04 14:30:00 调整了时间和唯一标识在Id中的位置,以便生成递增的Id
|
||||
2020-04-20 21:10:00 同步了C#版本的逻辑
|
||||
*/
|
||||
|
||||
package idUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type IdentifierConstructCountSeedGenerator struct {
|
||||
identifier int64 // 唯一标识
|
||||
identifierBit int32 // 唯一标识(机器号或者服务器Id等)所占的位数
|
||||
identifierBitOffset int32 // 唯一标识的偏移位数
|
||||
|
||||
constructCount int64 // 对象构造的次数
|
||||
constructCountBit int32 // 对象构造的次数所占的位数
|
||||
constructCountBitOffset int32 // 时间戳对象构造的次数的偏移位数的偏移位数
|
||||
|
||||
seed int64 // 当前种子值
|
||||
seedBit int32 // 自增种子所占的位数
|
||||
seedBitOffset int32 // 自增种子的偏移位数
|
||||
maxSeed int64 // 最大的种子值
|
||||
|
||||
mutex sync.Mutex // 锁对象
|
||||
}
|
||||
|
||||
func (this *IdentifierConstructCountSeedGenerator) getNewSeed() (int64, error) {
|
||||
this.mutex.Lock()
|
||||
defer this.mutex.Unlock()
|
||||
|
||||
if this.seed >= this.maxSeed {
|
||||
return 0, fmt.Errorf("Seed's value is out of scope")
|
||||
}
|
||||
|
||||
this.seed += 1
|
||||
|
||||
return this.seed, nil
|
||||
}
|
||||
|
||||
// 生成新的Id
|
||||
// 返回值:
|
||||
// 新的Id
|
||||
// 错误对象
|
||||
func (this *IdentifierConstructCountSeedGenerator) GenerateNewId() (int64, error) {
|
||||
seed, err := this.getNewSeed()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id := (this.identifier << this.identifierBitOffset) | (this.constructCount << this.constructCountBitOffset) | (seed << this.seedBitOffset)
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// 创建新的Id生成器对象(为了保证Id的唯一,需要保证生成的对象全局唯一)
|
||||
// identifierBit + constructCountBit + seedBit <= 63
|
||||
// identifier:id唯一标识
|
||||
// identifierBit:id唯一标识(机器号或者服务器Id等)的位数
|
||||
// constructCount:对象构造次数
|
||||
// constructCountBit:对象构造次数的位数
|
||||
// seedBit:自增种子的位数
|
||||
// 返回值:
|
||||
// 新的Id生成器对象
|
||||
// 错误对象
|
||||
func NewIdentifierConstructCountSeedGenerator(identifier int64, identifierBit int32, constructCount int64, constructCountBit, seedBit int32) (*IdentifierConstructCountSeedGenerator, error) {
|
||||
// 之所以使用63位而不是64,是为了保证值为正数
|
||||
if identifierBit+constructCountBit+seedBit > 63 {
|
||||
return nil, fmt.Errorf("总位数%d超过63位,请调整所有值的合理范围。", identifierBit+constructCountBit+seedBit)
|
||||
}
|
||||
if identifier < 0 || identifier > int64(math.Pow(2, float64(identifierBit)))-1 {
|
||||
return nil, fmt.Errorf("唯一标识值溢出,有效范围为【0,%d】", int64(math.Pow(2, float64(identifierBit)))-1)
|
||||
}
|
||||
if constructCount < 0 || constructCount > int64(math.Pow(2, float64(constructCountBit)))-1 {
|
||||
return nil, fmt.Errorf("对象构造次数的值溢出,有效范围为【0,%d】", int64(math.Pow(2, float64(constructCountBit)))-1)
|
||||
}
|
||||
|
||||
obj := &IdentifierConstructCountSeedGenerator{
|
||||
identifier: identifier,
|
||||
identifierBit: identifierBit,
|
||||
constructCount: constructCount,
|
||||
constructCountBit: constructCountBit,
|
||||
seed: 0,
|
||||
seedBit: seedBit,
|
||||
maxSeed: int64(math.Pow(2, float64(seedBit)) - 1),
|
||||
}
|
||||
|
||||
obj.seedBitOffset = 0
|
||||
obj.constructCountBitOffset = obj.seedBitOffset + obj.seedBit
|
||||
obj.identifierBitOffset = obj.constructCountBitOffset + obj.constructCountBit
|
||||
|
||||
return obj, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package gameServerMgr
|
||||
|
||||
import (
|
||||
. "Framework/managecenterModel"
|
||||
)
|
||||
|
||||
var (
|
||||
mAreaList = make([]*Area, 0)
|
||||
)
|
||||
|
||||
// 解析大区信息
|
||||
func ParseAreaInfo(areaList []*Area) {
|
||||
mAreaList = areaList
|
||||
}
|
||||
|
||||
// 根据服务器组id获取大区Id
|
||||
func GetAreaIdByGroupId(groupId int32) (areaId int32) {
|
||||
areaId = 0
|
||||
|
||||
//如果没有大区数据,返回0
|
||||
if mAreaList == nil || len(mAreaList) < 1 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, area := range mAreaList {
|
||||
if area.CheckServerIdIsInRange(groupId) {
|
||||
areaId = area.AreaId
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 根据服务器组id获取大区对象数据
|
||||
func GetAreaDBByGroupId(groupId int32) (areaDB *Area, exist bool) {
|
||||
//如果没有大区数据,返回空
|
||||
exist = false
|
||||
if mAreaList == nil || len(mAreaList) < 1 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, area := range mAreaList {
|
||||
if area.CheckServerIdIsInRange(groupId) {
|
||||
areaDB = area
|
||||
exist = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 根据大区ID获取大区信息
|
||||
func GetAreaDBbyAreaID(areaId int32) (areaDB *Area, exist bool) {
|
||||
//如果没有大区数据,返回空
|
||||
exist = false
|
||||
if mAreaList == nil || len(mAreaList) < 1 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, area := range mAreaList {
|
||||
if area.AreaId == areaId {
|
||||
areaDB = area
|
||||
exist = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 获取所有大区信息
|
||||
func GetAllAreaDB() []*Area {
|
||||
tempList := mAreaList
|
||||
return tempList
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package netUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
// 需要排除的IP: 169.254.xx.xx (未分配到IP的段)
|
||||
func excludeIP(ip4 net.IP) bool {
|
||||
return ip4[0] == 169 && ip4[1] == 254
|
||||
}
|
||||
|
||||
// GetLocalIPs
|
||||
//
|
||||
// @Description: 获取本机局域网IP(排除环回地址)
|
||||
// @return ips
|
||||
// @return err
|
||||
func GetLocalIPs() (ips []string, err error) {
|
||||
var addrs []net.Addr
|
||||
if addrs, err = net.InterfaceAddrs(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() /*&& ipnet.IP.IsPrivate()*/ {
|
||||
if ip4 := ipnet.IP.To4(); ip4 != nil {
|
||||
if !excludeIP(ip4) {
|
||||
ips = append(ips, ipnet.IP.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(ips) == 0 {
|
||||
// 未获取到本机局域网IP
|
||||
err = errors.New("not found")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package convertMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
. "Framework/startMgr"
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
var (
|
||||
funcMap = make(map[string]*FuncItem)
|
||||
mutex sync.Mutex
|
||||
operateName = "Convert"
|
||||
)
|
||||
|
||||
// 注册方法(如果名称重复会panic)
|
||||
// name:方法名称(唯一标识)
|
||||
// moduleType:模块类型
|
||||
// definition:方法定义
|
||||
func Register(name string, moduleType ModuleType, definition func() error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if _, exists := funcMap[name]; exists {
|
||||
panic(fmt.Sprintf("%s已经存在,请重新命名", name))
|
||||
}
|
||||
|
||||
funcMap[name] = NewFuncItem(name, moduleType, definition)
|
||||
}
|
||||
|
||||
// 调用所有方法
|
||||
// 返回值:
|
||||
// 错误列表
|
||||
func CallAll() (errList []error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
for _, item := range funcMap {
|
||||
// 调用方法
|
||||
if err := item.Call2(operateName); err != nil {
|
||||
errList = append(errList, err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 按照模块类型进行调用
|
||||
// moduleType:模块类型
|
||||
// 返回值:
|
||||
// errList:错误列表
|
||||
func CallType(moduleType ModuleType) (errList []error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
logUtil.InfoLog("%v %s 执行总时间:%s", moduleType, operateName, time.Since(startTime))
|
||||
}()
|
||||
|
||||
for _, item := range funcMap {
|
||||
if item.ModuleType != moduleType {
|
||||
continue
|
||||
}
|
||||
|
||||
// 调用方法
|
||||
if err := item.Call2(operateName); err != nil {
|
||||
errList = append(errList, err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user