You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.1 KiB

  1. package utils
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "zinx/ziface"
  7. )
  8. /*
  9. 存储一切有关Zinx框架的全局参数供其他模块使用
  10. 一些参数也可以通过 用户根据 zinx.json来配置
  11. */
  12. type GlobalObj struct {
  13. /*
  14. Server
  15. */
  16. TcpServer ziface.IServer //当前Zinx的全局Server对象
  17. Host string //当前服务器主机IP
  18. TcpPort int //当前服务器主机监听端口号
  19. Name string //当前服务器名称
  20. /*
  21. Zinx
  22. */
  23. Version string //当前Zinx版本号
  24. MaxPacketSize uint32 //都需数据包的最大值
  25. MaxConn int //当前服务器主机允许的最大链接个数
  26. WorkerPoolSize uint32 //业务工作Worker池的数量
  27. MaxWorkerTaskLen uint32 //业务工作Worker对应负责的任务队列最大任务存储数量
  28. MaxMsgChanLen uint32 //SendBuffMsg发送消息的缓冲最大长度
  29. /*
  30. config file path
  31. */
  32. ConfFilePath string
  33. }
  34. /*
  35. 定义一个全局的对象
  36. */
  37. var GlobalObject *GlobalObj
  38. //判断一个文件是否存在
  39. func PathExists(path string) (bool, error) {
  40. _, err := os.Stat(path)
  41. if err == nil {
  42. return true, nil
  43. }
  44. if os.IsNotExist(err) {
  45. return false, nil
  46. }
  47. return false, err
  48. }
  49. //读取用户的配置文件
  50. func (g *GlobalObj) Reload() {
  51. if confFileExists, _ := PathExists(g.ConfFilePath); confFileExists != true {
  52. //fmt.Println("Config File ", g.ConfFilePath , " is not exist!!")
  53. return
  54. }
  55. data, err := ioutil.ReadFile(g.ConfFilePath)
  56. if err != nil {
  57. panic(err)
  58. }
  59. //将json数据解析到struct中
  60. //fmt.Printf("json :%s\n", data)
  61. err = json.Unmarshal(data, &GlobalObject)
  62. if err != nil {
  63. panic(err)
  64. }
  65. }
  66. /*
  67. 提供init方法默认加载
  68. */
  69. func init() {
  70. //初始化GlobalObject变量,设置一些默认值
  71. GlobalObject = &GlobalObj{
  72. Name: "ZinxServerApp",
  73. Version: "V0.4",
  74. TcpPort: 7777,
  75. Host: "0.0.0.0",
  76. MaxConn: 12000,
  77. MaxPacketSize: 4096,
  78. ConfFilePath: "conf/zinx.json",
  79. WorkerPoolSize: 10,
  80. MaxWorkerTaskLen: 1024,
  81. MaxMsgChanLen:1024,
  82. }
  83. //从配置文件中加载一些用户配置的参数
  84. GlobalObject.Reload()
  85. }