session.go 2.5 KB

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