session.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.sessions[s.ID] = s
  51. }
  52. func (m *SessionManager) Remove(id uint16) {
  53. m.Lock()
  54. defer m.Unlock()
  55. if m.closed {
  56. return
  57. }
  58. delete(m.sessions, id)
  59. if len(m.sessions) == 0 {
  60. m.sessions = make(map[uint16]*Session, 16)
  61. }
  62. }
  63. func (m *SessionManager) Get(id uint16) (*Session, bool) {
  64. m.RLock()
  65. defer m.RUnlock()
  66. if m.closed {
  67. return nil, false
  68. }
  69. s, found := m.sessions[id]
  70. return s, found
  71. }
  72. func (m *SessionManager) CloseIfNoSession() bool {
  73. m.Lock()
  74. defer m.Unlock()
  75. if m.closed {
  76. return true
  77. }
  78. if len(m.sessions) != 0 {
  79. return false
  80. }
  81. m.closed = true
  82. return true
  83. }
  84. func (m *SessionManager) Close() error {
  85. m.Lock()
  86. defer m.Unlock()
  87. if m.closed {
  88. return nil
  89. }
  90. m.closed = true
  91. for _, s := range m.sessions {
  92. common.Close(s.input) // nolint: errcheck
  93. common.Close(s.output) // nolint: errcheck
  94. }
  95. m.sessions = nil
  96. return nil
  97. }
  98. // Session represents a client connection in a Mux connection.
  99. type Session struct {
  100. input buf.Reader
  101. output buf.Writer
  102. parent *SessionManager
  103. ID uint16
  104. transferType protocol.TransferType
  105. }
  106. // Close closes all resources associated with this session.
  107. func (s *Session) Close() error {
  108. common.Close(s.output) // nolint: errcheck
  109. common.Close(s.input) // nolint: errcheck
  110. s.parent.Remove(s.ID)
  111. return nil
  112. }
  113. // NewReader creates a buf.Reader based on the transfer type of this Session.
  114. func (s *Session) NewReader(reader *buf.BufferedReader) buf.Reader {
  115. if s.transferType == protocol.TransferTypeStream {
  116. return NewStreamReader(reader)
  117. }
  118. return NewPacketReader(reader)
  119. }