connection.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package hub
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "time"
  6. "github.com/v2ray/v2ray-core/common"
  7. )
  8. type ConnectionHandler func(Connection)
  9. type Connection interface {
  10. common.Releasable
  11. Read([]byte) (int, error)
  12. Write([]byte) (int, error)
  13. Close() error
  14. LocalAddr() net.Addr
  15. RemoteAddr() net.Addr
  16. SetDeadline(t time.Time) error
  17. SetReadDeadline(t time.Time) error
  18. SetWriteDeadline(t time.Time) error
  19. CloseRead() error
  20. CloseWrite() error
  21. }
  22. type TCPConnection struct {
  23. conn *net.TCPConn
  24. listener *TCPHub
  25. }
  26. func (this *TCPConnection) Read(b []byte) (int, error) {
  27. if this == nil || this.conn == nil {
  28. return 0, ErrorClosedConnection
  29. }
  30. return this.conn.Read(b)
  31. }
  32. func (this *TCPConnection) Write(b []byte) (int, error) {
  33. if this == nil || this.conn == nil {
  34. return 0, ErrorClosedConnection
  35. }
  36. return this.conn.Write(b)
  37. }
  38. func (this *TCPConnection) Close() error {
  39. if this == nil || this.conn == nil {
  40. return ErrorClosedConnection
  41. }
  42. err := this.conn.Close()
  43. return err
  44. }
  45. func (this *TCPConnection) Release() {
  46. if this == nil || this.listener == nil {
  47. return
  48. }
  49. this.Close()
  50. this.conn = nil
  51. this.listener = nil
  52. }
  53. func (this *TCPConnection) LocalAddr() net.Addr {
  54. return this.conn.LocalAddr()
  55. }
  56. func (this *TCPConnection) RemoteAddr() net.Addr {
  57. return this.conn.RemoteAddr()
  58. }
  59. func (this *TCPConnection) SetDeadline(t time.Time) error {
  60. return this.conn.SetDeadline(t)
  61. }
  62. func (this *TCPConnection) SetReadDeadline(t time.Time) error {
  63. return this.conn.SetReadDeadline(t)
  64. }
  65. func (this *TCPConnection) SetWriteDeadline(t time.Time) error {
  66. return this.conn.SetWriteDeadline(t)
  67. }
  68. func (this *TCPConnection) CloseRead() error {
  69. if this == nil || this.conn == nil {
  70. return nil
  71. }
  72. return this.conn.CloseRead()
  73. }
  74. func (this *TCPConnection) CloseWrite() error {
  75. if this == nil || this.conn == nil {
  76. return nil
  77. }
  78. return this.conn.CloseWrite()
  79. }
  80. type TLSConnection struct {
  81. conn *tls.Conn
  82. listener *TCPHub
  83. }
  84. func (this *TLSConnection) Read(b []byte) (int, error) {
  85. if this == nil || this.conn == nil {
  86. return 0, ErrorClosedConnection
  87. }
  88. return this.conn.Read(b)
  89. }
  90. func (this *TLSConnection) Write(b []byte) (int, error) {
  91. if this == nil || this.conn == nil {
  92. return this.conn.Write(b)
  93. }
  94. return this.conn.Write(b)
  95. }
  96. func (this *TLSConnection) Close() error {
  97. if this == nil || this.conn == nil {
  98. return ErrorClosedConnection
  99. }
  100. err := this.conn.Close()
  101. return err
  102. }
  103. func (this *TLSConnection) Release() {
  104. if this == nil || this.listener == nil {
  105. return
  106. }
  107. this.Close()
  108. this.conn = nil
  109. this.listener = nil
  110. }
  111. func (this *TLSConnection) LocalAddr() net.Addr {
  112. return this.conn.LocalAddr()
  113. }
  114. func (this *TLSConnection) RemoteAddr() net.Addr {
  115. return this.conn.RemoteAddr()
  116. }
  117. func (this *TLSConnection) SetDeadline(t time.Time) error {
  118. return this.conn.SetDeadline(t)
  119. }
  120. func (this *TLSConnection) SetReadDeadline(t time.Time) error {
  121. return this.conn.SetReadDeadline(t)
  122. }
  123. func (this *TLSConnection) SetWriteDeadline(t time.Time) error {
  124. return this.conn.SetWriteDeadline(t)
  125. }
  126. func (this *TLSConnection) CloseRead() error {
  127. return nil
  128. }
  129. func (this *TLSConnection) CloseWrite() error {
  130. return nil
  131. }