nodeproperty.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package socket
  2. import (
  3. "reflect"
  4. "rocommon"
  5. "sync"
  6. )
  7. /////////////////////////////////////////////NetServerNodeProperty
  8. type NetServerNodeProperty struct {
  9. name string //服务器名称 game,game,auth
  10. addr string //包含了ip和port
  11. queue rocommon.NetEventQueue
  12. serverType int //服务器类型(例如gate,game,auth)
  13. zone int //前服务器区号(理解成服务组)
  14. index int //服务器区内的编号
  15. }
  16. func (this *NetServerNodeProperty) GetName() string {
  17. return this.name
  18. }
  19. func (this *NetServerNodeProperty) SetName(s string) {
  20. this.name = s
  21. }
  22. func (this *NetServerNodeProperty) GetAddr() string {
  23. return this.addr
  24. }
  25. func (this *NetServerNodeProperty) SetAddr(s string) {
  26. this.addr = s
  27. }
  28. func (this *NetServerNodeProperty) SetQueue(v rocommon.NetEventQueue) {
  29. this.queue = v
  30. }
  31. func (this *NetServerNodeProperty) Queue() rocommon.NetEventQueue {
  32. return this.queue
  33. }
  34. func (this *NetServerNodeProperty) SetServerType(t int) {
  35. this.serverType = t
  36. }
  37. func (this *NetServerNodeProperty) ServerType() int {
  38. return this.serverType
  39. }
  40. func (this *NetServerNodeProperty) SetZone(t int) {
  41. this.zone = t
  42. }
  43. func (this *NetServerNodeProperty) GetZone() int {
  44. return this.zone
  45. }
  46. func (this *NetServerNodeProperty) SetIndex(t int) {
  47. this.index = t
  48. }
  49. func (this *NetServerNodeProperty) GetIndex() int {
  50. return this.index
  51. }
  52. /////////////////////////////////////////////NetContextSet
  53. //用来记录session数据
  54. type NetContextSet struct {
  55. guard sync.RWMutex //读写锁
  56. dataMap map[interface{}]keyValueData
  57. //user 玩家
  58. //sd 服务器相关数据
  59. }
  60. type keyValueData struct {
  61. key interface{}
  62. value interface{}
  63. }
  64. func (this *NetContextSet) SetContextData(key, value interface{}, from string) {
  65. this.guard.Lock()
  66. defer this.guard.Unlock()
  67. if this.dataMap == nil {
  68. this.dataMap = map[interface{}]keyValueData{}
  69. }
  70. if _, ok := this.dataMap[key]; ok {
  71. if value == nil {
  72. //util.InfoF("ContextData clean key:%v oldValue:%v newValue:%v [%v]", key, data, value, from)
  73. } else {
  74. //util.FatalF("ContextData exist key:%v oldValue:%v newValue:%v [%v]", key, data, value, from)
  75. }
  76. this.dataMap[key] = keyValueData{key, value}
  77. } else {
  78. this.dataMap[key] = keyValueData{key, value}
  79. }
  80. }
  81. func (this *NetContextSet) GetContextData(key interface{}) (interface{}, bool) {
  82. this.guard.RLock()
  83. defer this.guard.RUnlock()
  84. if this.dataMap == nil {
  85. this.dataMap = map[interface{}]keyValueData{}
  86. }
  87. if data, ok := this.dataMap[key]; ok {
  88. return data.value, true
  89. }
  90. return nil, false
  91. }
  92. //根据给定类型获取数据
  93. func (this *NetContextSet) RawContextData(key interface{}, valuePtr interface{}) bool {
  94. value, ok := this.GetContextData(key)
  95. if !ok {
  96. return false
  97. }
  98. switch outValue := valuePtr.(type) {
  99. case *string:
  100. *outValue = value.(string)
  101. default:
  102. v := reflect.Indirect(reflect.ValueOf(valuePtr))
  103. if value != nil {
  104. v.Set(reflect.ValueOf(value))
  105. }
  106. }
  107. return true
  108. }
  109. /////////////////////////////////////////////NetRedisParam
  110. type NetRedisParam struct {
  111. Pwd string
  112. DBIndex int
  113. }
  114. func (this *NetRedisParam) SetPwd(pwd string) {
  115. this.Pwd = pwd
  116. }
  117. func (this *NetRedisParam) SetDBIndex(db int) {
  118. this.DBIndex = db
  119. }