connection.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package internal
  2. import (
  3. "io"
  4. "net"
  5. "sync"
  6. "time"
  7. v2net "v2ray.com/core/common/net"
  8. )
  9. // ConnectionID is the ID of a connection.
  10. type ConnectionID struct {
  11. Local v2net.Address
  12. Remote v2net.Address
  13. RemotePort v2net.Port
  14. }
  15. // NewConnectionID creates a new ConnectionId.
  16. func NewConnectionID(source v2net.Address, dest v2net.Destination) ConnectionID {
  17. return ConnectionID{
  18. Local: source,
  19. Remote: dest.Address,
  20. RemotePort: dest.Port,
  21. }
  22. }
  23. type Reuser struct {
  24. userEnabled bool
  25. appEnable bool
  26. }
  27. func ReuseConnection(reuse bool) *Reuser {
  28. return &Reuser{
  29. userEnabled: reuse,
  30. appEnable: reuse,
  31. }
  32. }
  33. // Connection is an implementation of net.Conn with re-usability.
  34. type Connection struct {
  35. sync.RWMutex
  36. id ConnectionID
  37. conn net.Conn
  38. listener ConnectionRecyler
  39. reuser *Reuser
  40. }
  41. func NewConnection(id ConnectionID, conn net.Conn, manager ConnectionRecyler, reuser *Reuser) *Connection {
  42. return &Connection{
  43. id: id,
  44. conn: conn,
  45. listener: manager,
  46. reuser: reuser,
  47. }
  48. }
  49. func (v *Connection) Read(b []byte) (int, error) {
  50. conn := v.underlyingConn()
  51. if conn == nil {
  52. return 0, io.EOF
  53. }
  54. return conn.Read(b)
  55. }
  56. func (v *Connection) Write(b []byte) (int, error) {
  57. conn := v.underlyingConn()
  58. if conn == nil {
  59. return 0, io.ErrClosedPipe
  60. }
  61. return conn.Write(b)
  62. }
  63. // Close implements net.Conn.Close(). If the connection is reusable, the underlying connection will be recycled.
  64. func (v *Connection) Close() error {
  65. if v == nil {
  66. return io.ErrClosedPipe
  67. }
  68. v.Lock()
  69. defer v.Unlock()
  70. if v.conn == nil {
  71. return io.ErrClosedPipe
  72. }
  73. if v.Reusable() {
  74. v.listener.Put(v.id, v.conn)
  75. return nil
  76. }
  77. err := v.conn.Close()
  78. v.conn = nil
  79. return err
  80. }
  81. func (v *Connection) LocalAddr() net.Addr {
  82. conn := v.underlyingConn()
  83. if conn == nil {
  84. return nil
  85. }
  86. return conn.LocalAddr()
  87. }
  88. func (v *Connection) RemoteAddr() net.Addr {
  89. conn := v.underlyingConn()
  90. if conn == nil {
  91. return nil
  92. }
  93. return conn.RemoteAddr()
  94. }
  95. func (v *Connection) SetDeadline(t time.Time) error {
  96. conn := v.underlyingConn()
  97. if conn == nil {
  98. return nil
  99. }
  100. return conn.SetDeadline(t)
  101. }
  102. func (v *Connection) SetReadDeadline(t time.Time) error {
  103. conn := v.underlyingConn()
  104. if conn == nil {
  105. return nil
  106. }
  107. return conn.SetReadDeadline(t)
  108. }
  109. func (v *Connection) SetWriteDeadline(t time.Time) error {
  110. conn := v.underlyingConn()
  111. if conn == nil {
  112. return nil
  113. }
  114. return conn.SetWriteDeadline(t)
  115. }
  116. func (v *Connection) SetReusable(reusable bool) {
  117. if v == nil {
  118. return
  119. }
  120. v.reuser.appEnable = reusable
  121. }
  122. func (v *Connection) Reusable() bool {
  123. if v == nil {
  124. return false
  125. }
  126. return v.reuser.userEnabled && v.reuser.appEnable
  127. }
  128. func (v *Connection) SysFd() (int, error) {
  129. conn := v.underlyingConn()
  130. if conn == nil {
  131. return 0, io.ErrClosedPipe
  132. }
  133. return GetSysFd(conn)
  134. }
  135. func (v *Connection) underlyingConn() net.Conn {
  136. if v == nil {
  137. return nil
  138. }
  139. v.RLock()
  140. defer v.RUnlock()
  141. return v.conn
  142. }