conn.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package enet
  2. import (
  3. "ehang.io/nps/lib/pool"
  4. "errors"
  5. "net"
  6. "sync"
  7. "syscall"
  8. )
  9. type Conn interface {
  10. net.Conn
  11. Reset(int) error
  12. Clear()
  13. Readable() bool
  14. AllBytes() ([]byte, error)
  15. SyscallConn() (syscall.RawConn, error)
  16. }
  17. var _ Conn = (*ReaderConn)(nil)
  18. var bp = pool.NewBufferPool(MaxReadSize)
  19. const MaxReadSize = 32 * 1024
  20. // ReaderConn is an implement of reusable data connection
  21. type ReaderConn struct {
  22. buf []byte
  23. nowIndex int
  24. hasRead int
  25. hasClear bool
  26. net.Conn
  27. sync.RWMutex
  28. }
  29. // NewReaderConn returns a new ReaderConn
  30. func NewReaderConn(conn net.Conn) *ReaderConn {
  31. return &ReaderConn{Conn: conn, buf: bp.Get()}
  32. }
  33. // SyscallConn returns a raw network connection
  34. func (rc *ReaderConn) SyscallConn() (syscall.RawConn, error) {
  35. return rc.Conn.(syscall.Conn).SyscallConn()
  36. }
  37. // Read is an implement of Net.Conn Read function
  38. func (rc *ReaderConn) Read(b []byte) (n int, err error) {
  39. rc.Lock()
  40. defer rc.Unlock()
  41. if rc.hasClear || (rc.nowIndex == rc.hasRead && rc.hasRead == MaxReadSize) {
  42. if !rc.hasClear {
  43. rc.Clear()
  44. }
  45. return rc.Conn.Read(b)
  46. }
  47. if rc.hasRead > rc.nowIndex {
  48. n = copy(b, rc.buf[rc.nowIndex:rc.hasRead])
  49. rc.nowIndex += n
  50. return
  51. }
  52. if rc.hasRead == MaxReadSize {
  53. n = copy(b, rc.buf[rc.nowIndex:rc.hasRead])
  54. rc.nowIndex += n
  55. return
  56. }
  57. err = rc.readOnce()
  58. if err != nil {
  59. return
  60. }
  61. n = copy(b, rc.buf[rc.nowIndex:rc.hasRead])
  62. rc.nowIndex += n
  63. return
  64. }
  65. // readOnce
  66. func (rc *ReaderConn) readOnce() error {
  67. // int(math.Min(float64(MaxReadSize-rc.hasRead), float64(len(b)-(rc.hasRead-rc.nowIndex))))
  68. // read as much as possible to judge whether there is still readable
  69. n, err := rc.Conn.Read(rc.buf[rc.nowIndex : rc.hasRead+MaxReadSize-rc.hasRead])
  70. rc.hasRead += n
  71. return err
  72. }
  73. // Readable return whether there is data in the buffer
  74. func (rc *ReaderConn) Readable() bool {
  75. return (rc.hasRead - rc.nowIndex) > 0
  76. }
  77. // AllBytes return all data in the buffer
  78. func (rc *ReaderConn) AllBytes() ([]byte, error) {
  79. rc.Lock()
  80. defer rc.Unlock()
  81. if rc.hasRead == 0 {
  82. if err := rc.readOnce(); err != nil {
  83. return nil, err
  84. }
  85. }
  86. if !rc.Readable() {
  87. return nil, errors.New("can not read '")
  88. }
  89. b := rc.buf[rc.nowIndex:rc.hasRead]
  90. rc.nowIndex = rc.hasRead
  91. return b, nil
  92. }
  93. // Reset will reset data index
  94. func (rc *ReaderConn) Reset(n int) error {
  95. if !rc.hasClear {
  96. rc.nowIndex = n
  97. return nil
  98. }
  99. return errors.New("the enet can not reset anymore")
  100. }
  101. // Clear will put buf to pool and can not reuse anymore
  102. func (rc *ReaderConn) Clear() {
  103. rc.hasClear = true
  104. bp.Put(rc.buf)
  105. }