sessionmanager.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package socket
  2. import (
  3. "rocommon"
  4. "rocommon/util"
  5. "sync"
  6. "sync/atomic"
  7. "time"
  8. )
  9. //SessionMagExport interface
  10. type (
  11. SessionManager interface {
  12. rocommon.SessionMagExport
  13. Add(rocommon.Session)
  14. Remove(rocommon.Session)
  15. }
  16. NetSessionManager struct {
  17. sessionIdGen int64
  18. count int64
  19. sessionMap sync.Map
  20. genKey int
  21. timeKey uint32
  22. lastTimeStamp uint64
  23. sequence uint64
  24. }
  25. )
  26. func NewNetSessionManager() *NetSessionManager {
  27. mag := &NetSessionManager{
  28. lastTimeStamp: util.GetTimeMilliseconds(),
  29. }
  30. return mag
  31. }
  32. //func (this *NetSessionManager) Add(s rocommon.Session) {
  33. // id := atomic.AddInt64(&this.sessionIdGen, 1)
  34. // atomic.AddInt64(&this.count, 1)
  35. // if id >= math.MaxUint32 {
  36. // id = 1
  37. // atomic.StoreInt64(&this.sessionIdGen, 1)
  38. // }
  39. // uuid := this.uuidCreate(id)
  40. // s.(interface{ SetID(uint642 uint64) }).SetID(uuid)
  41. // this.sessionMap.Store(uuid, s)
  42. //}
  43. func (this *NetSessionManager) Add(s rocommon.Session) {
  44. uuid := this.genSessionId()
  45. s.(interface{ SetID(uint642 uint64) }).SetID(uuid)
  46. //util.InfoF("NetSessionManager add uid=%v", uuid)
  47. if _, ok := this.sessionMap.Load(uuid); ok {
  48. util.ErrorF("NetSessionManager add multiple uid=%v", uuid)
  49. panic(nil)
  50. }
  51. this.sessionMap.Store(uuid, s)
  52. }
  53. func (this *NetSessionManager) Remove(s rocommon.Session) {
  54. this.sessionMap.Delete(s.ID())
  55. s.(rocommon.ContextSet).SetContextData("user", nil, "MagRemove")
  56. //if data,ok := s.(rocommon.ContextSet).SetContextData("user");ok {
  57. // if data != nil {
  58. // data.
  59. // }
  60. //}
  61. atomic.AddInt64(&this.count, -1)
  62. }
  63. func (this *NetSessionManager) GetSession(id uint64) rocommon.Session {
  64. if s, ok := this.sessionMap.Load(id); ok {
  65. return s.(rocommon.Session)
  66. }
  67. return nil
  68. }
  69. func (this *NetSessionManager) SessionNum() int {
  70. return int(atomic.LoadInt64(&this.count))
  71. }
  72. func (this *NetSessionManager) CloseAllSession() {
  73. this.sessionMap.Range(func(key, value interface{}) bool {
  74. value.(rocommon.Session).Close()
  75. return true
  76. })
  77. }
  78. func (this *NetSessionManager) SetUuidCreateKey(genKey int) {
  79. this.genKey = genKey
  80. this.timeKey = uint32(time.Now().UnixNano())
  81. }
  82. func (this *NetSessionManager) uuidCreate(id int64) uint64 {
  83. var uuid uint64 = 0
  84. uuid |= uint64(uint64(this.genKey) << 32) //6位
  85. uuid |= uint64(id) << 20
  86. uuid |= uint64(this.timeKey) & 0xfffff
  87. return uuid
  88. }
  89. func (this *NetSessionManager) genSessionId() uint64 {
  90. currentTimeStamp := uint64(time.Now().Unix())
  91. if this.lastTimeStamp == 0 {
  92. this.lastTimeStamp = currentTimeStamp
  93. }
  94. if this.genKey > 0xff {
  95. return 0
  96. }
  97. if this.lastTimeStamp == currentTimeStamp {
  98. this.sequence++
  99. if this.sequence > 0xffff {
  100. //一秒内尝试的数量超过上限
  101. return 0
  102. }
  103. } else {
  104. this.lastTimeStamp = currentTimeStamp
  105. this.sequence = 1
  106. }
  107. var uid uint64 = 0
  108. //前32位时间戳(秒)
  109. uid |= this.lastTimeStamp << 24
  110. //16位序列号
  111. uid |= uint64(this.sequence&0xffff) << 8
  112. //左后是逻辑ID(服务器id)
  113. uid |= uint64(this.genKey & 0xff)
  114. return uid
  115. }