new server
This commit is contained in:
50
models/reader.go
Normal file
50
models/reader.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Reader struct {
|
||||
buf []byte
|
||||
off int
|
||||
}
|
||||
|
||||
func NewReader(b []byte) *Reader {
|
||||
return &Reader{buf: b}
|
||||
}
|
||||
|
||||
func (r *Reader) ReadU8() uint8 {
|
||||
v := r.buf[r.off]
|
||||
r.off++
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadU16() uint16 {
|
||||
v := binary.LittleEndian.Uint16(r.buf[r.off:])
|
||||
r.off += 2
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadU32() uint32 {
|
||||
v := binary.LittleEndian.Uint32(r.buf[r.off:])
|
||||
r.off += 4
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadF32() float32 {
|
||||
v := math.Float32frombits(binary.LittleEndian.Uint32(r.buf[r.off:]))
|
||||
r.off += 4
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadBool() bool {
|
||||
return r.ReadU8() == 1
|
||||
}
|
||||
|
||||
func (r *Reader) ReadString() string {
|
||||
l := r.ReadU16()
|
||||
s := string(r.buf[r.off : r.off+int(l)])
|
||||
r.off += int(l)
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user