hpds_net_framework/examples/tcp/server.go

61 lines
1.6 KiB
Go
Raw Normal View History

2022-08-03 16:55:40 +08:00
package main
import (
"git.hpds.cc/Component/hpds_net_framework"
"git.hpds.cc/Component/hpds_net_framework/examples/comm/msg/code"
"git.hpds.cc/Component/hpds_net_framework/examples/comm/protobuf"
"git.hpds.cc/Component/hpds_net_framework/security"
"git.hpds.cc/Component/logging"
"go.uber.org/zap"
)
var (
logger *logging.Logger
processor *hpds_net_framework.PbProcessor
)
func init() {
logger = LoadLoggerConfig()
passwd := security.RandLittlePassword()
cipher := security.NewAESCipher(passwd)
processor = hpds_net_framework.NewPbProcessor(logger)
// add encrypt cipher for processor
processor.SetEncryptor(cipher)
// 注册消息,以及回调处理
processor.RegisterHandler(code.Hello, &protobuf.Hello{}, func(args ...interface{}) {
msg := args[hpds_net_framework.Msg].(*protobuf.Hello)
logger.Info("Message => from client", zap.String("content", msg.Hello))
conn := args[hpds_net_framework.Conn].(hpds_net_framework.IConnection)
conn.WriteMsg(msg)
})
}
func main() {
// run server
if s, err := hpds_net_framework.NewTcpServer("localhost:2021", processor, logger); err != nil {
panic(err)
} else {
e := s.Run()
if e != nil {
logger.Fatal("Fatal", zap.Error(e))
}
}
}
// LoadLoggerConfig 加载日志配置
func LoadLoggerConfig() *logging.Logger {
return logging.NewLogger(
logging.SetPath("./log/"),
logging.SetPrefix(""),
logging.SetDevelopment(true),
logging.SetDebugFileSuffix(""),
logging.SetWarnFileSuffix(""),
logging.SetErrorFileSuffix(""),
logging.SetInfoFileSuffix(""),
logging.SetMaxAge(30),
logging.SetMaxBackups(100),
logging.SetMaxSize(100),
logging.SetLevel(logging.LogLevel["debug"]),
)
}