tcp.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package hub
  2. import (
  3. "errors"
  4. "net"
  5. "time"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. )
  9. var (
  10. ErrorClosedConnection = errors.New("Connection already closed.")
  11. )
  12. type TCPConn struct {
  13. conn *net.TCPConn
  14. listener *TCPHub
  15. dirty bool
  16. }
  17. func (this *TCPConn) Read(b []byte) (int, error) {
  18. if this == nil || this.conn == nil {
  19. return 0, ErrorClosedConnection
  20. }
  21. return this.conn.Read(b)
  22. }
  23. func (this *TCPConn) Write(b []byte) (int, error) {
  24. if this == nil || this.conn == nil {
  25. return 0, ErrorClosedConnection
  26. }
  27. return this.conn.Write(b)
  28. }
  29. func (this *TCPConn) Close() error {
  30. if this == nil || this.conn == nil {
  31. return ErrorClosedConnection
  32. }
  33. err := this.conn.Close()
  34. this.conn = nil
  35. this.listener = nil
  36. return err
  37. }
  38. func (this *TCPConn) Release() {
  39. if this == nil || this.listener == nil {
  40. return
  41. }
  42. if this.dirty {
  43. this.Close()
  44. return
  45. }
  46. this.listener.recycle(this.conn)
  47. }
  48. func (this *TCPConn) LocalAddr() net.Addr {
  49. return this.conn.LocalAddr()
  50. }
  51. func (this *TCPConn) RemoteAddr() net.Addr {
  52. return this.conn.RemoteAddr()
  53. }
  54. func (this *TCPConn) SetDeadline(t time.Time) error {
  55. return this.conn.SetDeadline(t)
  56. }
  57. func (this *TCPConn) SetReadDeadline(t time.Time) error {
  58. return this.conn.SetReadDeadline(t)
  59. }
  60. func (this *TCPConn) SetWriteDeadline(t time.Time) error {
  61. return this.conn.SetWriteDeadline(t)
  62. }
  63. func (this *TCPConn) CloseRead() error {
  64. if this == nil || this.conn == nil {
  65. return nil
  66. }
  67. return this.conn.CloseRead()
  68. }
  69. func (this *TCPConn) CloseWrite() error {
  70. if this == nil || this.conn == nil {
  71. return nil
  72. }
  73. return this.conn.CloseWrite()
  74. }
  75. type TCPHub struct {
  76. listener *net.TCPListener
  77. connCallback func(*TCPConn)
  78. accepting bool
  79. }
  80. func ListenTCP(port v2net.Port, callback func(*TCPConn)) (*TCPHub, error) {
  81. listener, err := net.ListenTCP("tcp", &net.TCPAddr{
  82. IP: []byte{0, 0, 0, 0},
  83. Port: int(port),
  84. Zone: "",
  85. })
  86. if err != nil {
  87. return nil, err
  88. }
  89. tcpListener := &TCPHub{
  90. listener: listener,
  91. connCallback: callback,
  92. }
  93. go tcpListener.start()
  94. return tcpListener, nil
  95. }
  96. func (this *TCPHub) Close() {
  97. this.accepting = false
  98. this.listener.Close()
  99. this.listener = nil
  100. }
  101. func (this *TCPHub) start() {
  102. this.accepting = true
  103. for this.accepting {
  104. conn, err := this.listener.AcceptTCP()
  105. if err != nil {
  106. if this.accepting {
  107. log.Warning("Listener: Failed to accept new TCP connection: ", err)
  108. }
  109. continue
  110. }
  111. go this.connCallback(&TCPConn{
  112. conn: conn,
  113. listener: this,
  114. })
  115. }
  116. }
  117. func (this *TCPHub) recycle(conn *net.TCPConn) {
  118. }