session.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package mux
  2. import (
  3. "sync"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/buf"
  6. "v2ray.com/core/common/protocol"
  7. )
  8. type SessionManager struct {
  9. sync.RWMutex
  10. sessions map[uint16]*Session
  11. count uint16
  12. closed bool
  13. }
  14. func NewSessionManager() *SessionManager {
  15. return &SessionManager{
  16. count: 0,
  17. sessions: make(map[uint16]*Session, 16),
  18. }
  19. }
  20. func (m *SessionManager) Size() int {
  21. m.RLock()
  22. defer m.RUnlock()
  23. return len(m.sessions)
  24. }
  25. func (m *SessionManager) Count() int {
  26. m.RLock()
  27. defer m.RUnlock()
  28. return int(m.count)
  29. }
  30. func (m *SessionManager) Allocate() *Session {
  31. m.Lock()
  32. defer m.Unlock()
  33. if m.closed {
  34. return nil
  35. }
  36. m.count++
  37. s := &Session{
  38. ID: m.count,
  39. parent: m,
  40. }
  41. m.sessions[s.ID] = s
  42. return s
  43. }
  44. func (m *SessionManager) Add(s *Session) {
  45. m.Lock()
  46. defer m.Unlock()
  47. if m.closed {
  48. return
  49. }
  50. m.count++
  51. m.sessions[s.ID] = s
  52. }
  53. func (m *SessionManager) Remove(id uint16) {
  54. m.Lock()
  55. defer m.Unlock()
  56. if m.closed {
  57. return
  58. }
  59. delete(m.sessions, id)
  60. if len(m.sessions) == 0 {
  61. m.sessions = make(map[uint16]*Session, 16)
  62. }
  63. }
  64. func (m *SessionManager) Get(id uint16) (*Session, bool) {
  65. m.RLock()
  66. defer m.RUnlock()
  67. if m.closed {
  68. return nil, false
  69. }
  70. s, found := m.sessions[id]
  71. return s, found
  72. }
  73. func (m *SessionManager) CloseIfNoSession() bool {
  74. m.Lock()
  75. defer m.Unlock()
  76. if m.closed {
  77. return true
  78. }
  79. if len(m.sessions) != 0 {
  80. return false
  81. }
  82. m.closed = true
  83. return true
  84. }
  85. func (m *SessionManager) Close() error {
  86. m.Lock()
  87. defer m.Unlock()
  88. if m.closed {
  89. return nil
  90. }
  91. m.closed = true
  92. for _, s := range m.sessions {
  93. common.Close(s.input) // nolint: errcheck
  94. common.Close(s.output) // nolint: errcheck
  95. }
  96. m.sessions = nil
  97. return nil
  98. }
  99. // Session represents a client connection in a Mux connection.
  100. type Session struct {
  101. input buf.Reader
  102. output buf.Writer
  103. parent *SessionManager
  104. ID uint16
  105. transferType protocol.TransferType
  106. }
  107. // Close closes all resources associated with this session.
  108. func (s *Session) Close() error {
  109. common.Close(s.output) // nolint: errcheck
  110. common.Close(s.input) // nolint: errcheck
  111. s.parent.Remove(s.ID)
  112. return nil
  113. }
  114. // NewReader creates a buf.Reader based on the transfer type of this Session.
  115. func (s *Session) NewReader(reader *buf.BufferedReader) buf.Reader {
  116. if s.transferType == protocol.TransferTypeStream {
  117. return NewStreamReader(reader)
  118. }
  119. return NewPacketReader(reader)
  120. }