zinx icon indicating copy to clipboard operation
zinx copied to clipboard

服务器配置应该拎出来,改为注入

Open wahello opened this issue 4 years ago • 2 comments

//NewServer 创建一个服务器句柄
func NewServer() ziface.IServer {
	printLogo()

	s := &Server{
		Name:       utils.GlobalObject.Name,
		IPVersion:  "tcp4",
		IP:         utils.GlobalObject.Host,
		Port:       utils.GlobalObject.TCPPort,
		msgHandler: NewMsgHandle(),
		ConnMgr:    NewConnManager(),
	}
	return s
}

可以改成类似如下的方式吗?


type ZConfig struct {
	/*
		Server
	*/
	Name string
	TcpVersion string
	Ip: string
	Port int

	/*
		Zinx
	*/
	MaxPacketSize    uint32 //都需数据包的最大值
	MaxConn          int    //当前服务器主机允许的最大链接个数
	WorkerPoolSize   uint32 //业务工作Worker池的数量
	MaxWorkerTaskLen uint32 //业务工作Worker对应负责的任务队列最大任务存储数量
	MaxMsgChanLen    uint32 //SendBuffMsg发送消息的缓冲最大长度

}

//iServer 接口实现,定义一个Server服务类
type Server struct {
	//服务器配置
	Config ZConfig
        
       ......
}
/*
  创建一个服务器句柄
*/
func NewServer(conf *ZConfig) ziface.IServer {
	printLogo()

	s := &Server{
		Config: conf,
		Name:       conf.Name,
		IPVersion:  conf.TcpVersion,
		IP:         conf.Ip,
		Port:       conf.Port,
		msgHandler: NewMsgHandle(),
		ConnMgr:    NewConnManager(),
	}
	return s
}

更加方便外部配置.

wahello avatar Mar 05 '21 11:03 wahello

方案比较赞同,对于改动建议,欢迎提交PR,我们Review OK就会Merge的

aceld avatar Jan 26 '22 02:01 aceld

目前Server提供了 NewUserConfServer 方法:

func NewUserConfServer(config *utils.Config, opts ...Option) ziface.IServer {
	//打印logo
	printLogo()

	s := &Server{
		Name:           config.Name,
		IPVersion:      config.TcpVersion,
		IP:             config.Host,
		Port:           config.TcpPort,
		msgHandler:     NewMsgHandle(),
		ConnMgr:        NewConnManager(),
		exitChan:       nil,
		packet:         zpack.Factory().NewPack(ziface.ZinxDataPack),
		defaultDecoder: zpack.NewTLVDecoder(),
	}
	//更替打包方式
	for _, opt := range opts {
		opt(s)
	}
	//刷新用户配置到全局配置变量
	utils.UserConfToGlobal(config)

	//提示当前配置信息
	utils.GlobalObject.Show()

	return s
}

aceld avatar Mar 22 '23 02:03 aceld