connection.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package tcp
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "reflect"
  7. "time"
  8. )
  9. var (
  10. ErrInvalidConn = errors.New("Invalid Connection.")
  11. )
  12. type ConnectionManager interface {
  13. Recycle(string, net.Conn)
  14. }
  15. type RawConnection struct {
  16. net.TCPConn
  17. }
  18. func (this *RawConnection) Reusable() bool {
  19. return false
  20. }
  21. func (this *RawConnection) SetReusable(b bool) {}
  22. func (this *RawConnection) SysFd() (int, error) {
  23. return getSysFd(&this.TCPConn)
  24. }
  25. type Connection struct {
  26. dest string
  27. conn net.Conn
  28. listener ConnectionManager
  29. reusable bool
  30. }
  31. func NewConnection(dest string, conn net.Conn, manager ConnectionManager) *Connection {
  32. return &Connection{
  33. dest: dest,
  34. conn: conn,
  35. listener: manager,
  36. reusable: effectiveConfig.ConnectionReuse,
  37. }
  38. }
  39. func (this *Connection) Read(b []byte) (int, error) {
  40. if this == nil || this.conn == nil {
  41. return 0, io.EOF
  42. }
  43. return this.conn.Read(b)
  44. }
  45. func (this *Connection) Write(b []byte) (int, error) {
  46. if this == nil || this.conn == nil {
  47. return 0, io.ErrClosedPipe
  48. }
  49. return this.conn.Write(b)
  50. }
  51. func (this *Connection) Close() error {
  52. if this == nil || this.conn == nil {
  53. return io.ErrClosedPipe
  54. }
  55. if this.Reusable() {
  56. this.listener.Recycle(this.dest, this.conn)
  57. return nil
  58. }
  59. err := this.conn.Close()
  60. this.conn = nil
  61. return err
  62. }
  63. func (this *Connection) LocalAddr() net.Addr {
  64. return this.conn.LocalAddr()
  65. }
  66. func (this *Connection) RemoteAddr() net.Addr {
  67. return this.conn.RemoteAddr()
  68. }
  69. func (this *Connection) SetDeadline(t time.Time) error {
  70. return this.conn.SetDeadline(t)
  71. }
  72. func (this *Connection) SetReadDeadline(t time.Time) error {
  73. return this.conn.SetReadDeadline(t)
  74. }
  75. func (this *Connection) SetWriteDeadline(t time.Time) error {
  76. return this.conn.SetWriteDeadline(t)
  77. }
  78. func (this *Connection) SetReusable(reusable bool) {
  79. if !effectiveConfig.ConnectionReuse {
  80. return
  81. }
  82. this.reusable = reusable
  83. }
  84. func (this *Connection) Reusable() bool {
  85. return this.reusable
  86. }
  87. func (this *Connection) SysFd() (int, error) {
  88. return getSysFd(this.conn)
  89. }
  90. func getSysFd(conn net.Conn) (int, error) {
  91. cv := reflect.ValueOf(conn)
  92. switch ce := cv.Elem(); ce.Kind() {
  93. case reflect.Struct:
  94. netfd := ce.FieldByName("conn").FieldByName("fd")
  95. switch fe := netfd.Elem(); fe.Kind() {
  96. case reflect.Struct:
  97. fd := fe.FieldByName("sysfd")
  98. return int(fd.Int()), nil
  99. }
  100. }
  101. return 0, ErrInvalidConn
  102. }