1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-12 04:43:22 +08:00

speed up recv

This commit is contained in:
源文雨
2022-04-20 19:48:08 +08:00
parent 6586b5dd10
commit 4ccd99b63f
4 changed files with 64 additions and 13 deletions

View File

@@ -6,6 +6,10 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"io"
"unsafe"
"github.com/FloatTech/zbputils/math"
)
// Writer 写入
@@ -118,6 +122,50 @@ func (w *Writer) Grow(n int) {
(*bytes.Buffer)(w).Grow(n)
}
func (w *Writer) Skip(n int) (int, error) {
b := (*buffer)(unsafe.Pointer(w))
b.lastRead = opInvalid
if len(b.buf) <= b.off {
// Buffer is empty, reset to recover space.
w.Reset()
if n == 0 {
return 0, nil
}
return 0, io.EOF
}
n = math.Min(n, len(b.buf[b.off:]))
b.off += n
if n > 0 {
b.lastRead = opRead
}
return n, nil
}
func (w *Writer) put() {
PutWriter(w)
}
// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.
type buffer struct {
buf []byte // contents are the bytes buf[off : len(buf)]
off int // read at &buf[off], write at &buf[len(buf)]
lastRead readOp // last read operation, so that Unread* can work correctly.
}
// The readOp constants describe the last action performed on
// the buffer, so that UnreadRune and UnreadByte can check for
// invalid usage. opReadRuneX constants are chosen such that
// converted to int they correspond to the rune size that was read.
type readOp int8
// Don't use iota for these, as the values need to correspond with the
// names and comments, which is easier to see when being explicit.
const (
opRead readOp = -1 // Any other read operation.
opInvalid readOp = 0 // Non-read operation.
opReadRune1 readOp = 1 // Read rune of size 1.
opReadRune2 readOp = 2 // Read rune of size 2.
opReadRune3 readOp = 3 // Read rune of size 3.
opReadRune4 readOp = 4 // Read rune of size 4.
)