104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
package models
|
|
|
|
import (
|
|
"bash_go_service/shared/pkg/constants"
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// Rule 映射 C 结构体
|
|
type Rule struct {
|
|
Cmd [constants.MaxRuleCmdLength]byte // 命令
|
|
Type [constants.MaxRuleTypeLength]byte // 类型
|
|
Msg [constants.MaxRuleMsgLength]byte // 消息
|
|
Args [constants.MaxArgs][constants.MaxArgLength]byte // 参数
|
|
ArgCount int32 // 参数数量
|
|
}
|
|
|
|
// ConfigData 映射 C 结构体
|
|
type ConfigData struct {
|
|
Enabled bool // 是否启用
|
|
Rules [constants.MaxRules]Rule // 规则列表
|
|
RuleCount int32 // 规则数量
|
|
}
|
|
|
|
// JSONRule 表示 JSON 配置中的一条规则
|
|
type JSONRule struct {
|
|
Cmd string `json:"cmd"` // 命令
|
|
Type string `json:"type"` // 类型
|
|
Msg string `json:"msg"` // 消息
|
|
Args []string `json:"args"` // 参数列表
|
|
}
|
|
|
|
// JSONConfig 表示整个 JSON 配置
|
|
type JSONConfig struct {
|
|
Enabled bool `json:"enabled"` // 是否启用
|
|
Rules []JSONRule `json:"rules"` // 规则列表
|
|
}
|
|
|
|
func (c *ConfigData) ToJSON() (string, error) {
|
|
|
|
jsonConfig := JSONConfig{
|
|
Enabled: c.Enabled,
|
|
Rules: make([]JSONRule, c.RuleCount),
|
|
}
|
|
for i := 0; i < int(c.RuleCount); i++ {
|
|
jsonConfig.Rules[i] = c.Rules[i].ToJSONRule()
|
|
}
|
|
jsonData, err := json.Marshal(jsonConfig)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(jsonData), nil
|
|
}
|
|
|
|
func (r *Rule) ToJSONRule() JSONRule {
|
|
// 处理C字符串到Go字符串的转换,去除末尾的空字符
|
|
cmd := strings.TrimRight(string(r.Cmd[:]), "\x00")
|
|
typ := strings.TrimRight(string(r.Type[:]), "\x00")
|
|
msg := strings.TrimRight(string(r.Msg[:]), "\x00")
|
|
|
|
// 处理参数数组
|
|
args := make([]string, r.ArgCount)
|
|
for i := 0; i < int(r.ArgCount); i++ {
|
|
args[i] = strings.TrimRight(string(r.Args[i][:]), "\x00")
|
|
}
|
|
|
|
return JSONRule{
|
|
Cmd: cmd,
|
|
Type: typ,
|
|
Msg: msg,
|
|
Args: args,
|
|
}
|
|
}
|
|
|
|
// func init() {
|
|
// // 计算一个Rule结构体的大小
|
|
// ruleSize := int(unsafe.Sizeof(Rule{}))
|
|
|
|
// // 计算ConfigData结构体的总大小
|
|
// configSize := int(unsafe.Sizeof(ConfigData{}))
|
|
|
|
// // 转换为MB的函数
|
|
// toMB := func(bytes int) float64 {
|
|
// return float64(bytes) / 1024 / 1024
|
|
// }
|
|
|
|
// // 验证共享内存大小是否足够
|
|
// if configSize > constants.ShmSize {
|
|
// panic(fmt.Sprintf("Shared memory size is too small to hold ConfigData structure. "+
|
|
// "Required size: %d bytes (%.2f MB), "+
|
|
// "Current size: %d bytes (%.2f MB)",
|
|
// configSize, toMB(configSize),
|
|
// constants.ShmSize, toMB(constants.ShmSize)))
|
|
// }
|
|
|
|
// // 打印详细信息便于调试
|
|
// log.Printf("Memory allocation details:")
|
|
// log.Printf("- Single Rule size: %d bytes (%.2f MB)", ruleSize, toMB(ruleSize))
|
|
// log.Printf("- Total ConfigData size: %d bytes (%.2f MB)", configSize, toMB(configSize))
|
|
// log.Printf("- Shared memory size: %d bytes (%.2f MB)", constants.ShmSize, toMB(constants.ShmSize))
|
|
// log.Printf("- Available space: %d bytes (%.2f MB)",
|
|
// constants.ShmSize-configSize, toMB(constants.ShmSize-configSize))
|
|
// }
|