buffer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rlpx
  17. import (
  18. "io"
  19. )
  20. // readBuffer implements buffering for network reads. This type is similar to bufio.Reader,
  21. // with two crucial differences: the buffer slice is exposed, and the buffer keeps all
  22. // read data available until reset.
  23. //
  24. // How to use this type:
  25. //
  26. // Keep a readBuffer b alongside the underlying network connection. When reading a packet
  27. // from the connection, first call b.reset(). This empties b.data. Now perform reads
  28. // through b.read() until the end of the packet is reached. The complete packet data is
  29. // now available in b.data.
  30. type readBuffer struct {
  31. data []byte
  32. end int
  33. }
  34. // reset removes all processed data which was read since the last call to reset.
  35. // After reset, len(b.data) is zero.
  36. func (b *readBuffer) reset() {
  37. unprocessed := b.end - len(b.data)
  38. copy(b.data[:unprocessed], b.data[len(b.data):b.end])
  39. b.end = unprocessed
  40. b.data = b.data[:0]
  41. }
  42. // read reads at least n bytes from r, returning the bytes.
  43. // The returned slice is valid until the next call to reset.
  44. func (b *readBuffer) read(r io.Reader, n int) ([]byte, error) {
  45. offset := len(b.data)
  46. have := b.end - len(b.data)
  47. // If n bytes are available in the buffer, there is no need to read from r at all.
  48. if have >= n {
  49. b.data = b.data[:offset+n]
  50. return b.data[offset : offset+n], nil
  51. }
  52. // Make buffer space available.
  53. need := n - have
  54. b.grow(need)
  55. // Read.
  56. rn, err := io.ReadAtLeast(r, b.data[b.end:cap(b.data)], need)
  57. if err != nil {
  58. return nil, err
  59. }
  60. b.end += rn
  61. b.data = b.data[:offset+n]
  62. return b.data[offset : offset+n], nil
  63. }
  64. // grow ensures the buffer has at least n bytes of unused space.
  65. func (b *readBuffer) grow(n int) {
  66. if cap(b.data)-b.end >= n {
  67. return
  68. }
  69. need := n - (cap(b.data) - b.end)
  70. offset := len(b.data)
  71. b.data = append(b.data[:cap(b.data)], make([]byte, need)...)
  72. b.data = b.data[:offset]
  73. }
  74. // writeBuffer implements buffering for network writes. This is essentially
  75. // a convenience wrapper around a byte slice.
  76. type writeBuffer struct {
  77. data []byte
  78. }
  79. func (b *writeBuffer) reset() {
  80. b.data = b.data[:0]
  81. }
  82. func (b *writeBuffer) appendZero(n int) []byte {
  83. offset := len(b.data)
  84. b.data = append(b.data, make([]byte, n)...)
  85. return b.data[offset : offset+n]
  86. }
  87. func (b *writeBuffer) Write(data []byte) (int, error) {
  88. b.data = append(b.data, data...)
  89. return len(data), nil
  90. }
  91. const maxUint24 = int(^uint32(0) >> 8)
  92. func readUint24(b []byte) uint32 {
  93. return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
  94. }
  95. func putUint24(v uint32, b []byte) {
  96. b[0] = byte(v >> 16)
  97. b[1] = byte(v >> 8)
  98. b[2] = byte(v)
  99. }
  100. // growslice ensures b has the wanted length by either expanding it to its capacity
  101. // or allocating a new slice if b has insufficient capacity.
  102. func growslice(b []byte, wantLength int) []byte {
  103. if len(b) >= wantLength {
  104. return b
  105. }
  106. if cap(b) >= wantLength {
  107. return b[:cap(b)]
  108. }
  109. return make([]byte, wantLength)
  110. }