tcp.go 2.5 KB

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