12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package xugu
- import (
- "errors"
- "fmt"
- "net"
- "time"
- )
- var (
- ErrBusyBuffer = errors.New("busy buffer")
- )
- type buffer struct {
- buf []byte
- conn net.Conn
- idx int
- length int
- timeout time.Duration
- }
- func newBuffer(nc net.Conn) buffer {
- return buffer{
- buf: make([]byte, 2048),
- conn: nc,
- }
- }
- func (b *buffer) store(buf []byte) error {
- if len(buf) > cap(b.buf) {
- fmt.Println("大于缓冲区: len(buf) > cap(b.buf)")
- return ErrBusyBuffer
- }
- b.buf = buf[:cap(buf)]
- return nil
- }
- func (b *buffer) peekChar() byte {
- ret := b.buf[b.idx]
- return ret
- }
- func (b *buffer) readNext(need int, reverse bool) []byte {
-
- if len(b.buf[b.idx:]) < need {
-
- b.conn.Read(b.buf[b.idx:])
- offset := b.idx
- b.idx += need
- return b.buf[offset:b.idx]
- }
- offset := b.idx
- b.idx += need
- fmt.Println("readNext: 大端值", b.buf[offset:b.idx], " - ", string(b.buf[offset:b.idx]))
-
- if reverse {
- tmp2 := b.buf[offset:b.idx]
- fmt.Println("readNext: 转换小端前", tmp2, " - ", string(tmp2))
- tmp := reverseBytes(b.buf[offset:b.idx])
- fmt.Println("readNext: 转换小端后", tmp, " - ", string(tmp))
- return tmp
- } else {
- return b.buf[offset:b.idx]
- }
- }
|