本文整理汇总了Golang中encoding/binary.ReadUvarint函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadUvarint函数的具体用法?Golang ReadUvarint怎么用?Golang ReadUvarint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadUvarint函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ParseKey
func (sr *StoredRow) ParseKey(key []byte) error {
buf := bytes.NewBuffer(key)
_, err := buf.ReadByte() // type
if err != nil {
return err
}
sr.docID, err = buf.ReadBytes(ByteSeparator)
if len(sr.docID) < 2 { // 1 for min doc id length, 1 for separator
err = fmt.Errorf("invalid doc length 0")
return err
}
sr.docID = sr.docID[:len(sr.docID)-1] // trim off separator byte
sr.docNum, err = binary.ReadUvarint(buf)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &sr.field)
if err != nil {
return err
}
sr.arrayPositions = make([]uint64, 0)
nextArrayPos, err := binary.ReadUvarint(buf)
for err == nil {
sr.arrayPositions = append(sr.arrayPositions, nextArrayPos)
nextArrayPos, err = binary.ReadUvarint(buf)
}
return nil
}
示例2: NewStoredRowK
func NewStoredRowK(key []byte) (*StoredRow, error) {
rv := StoredRow{}
buf := bytes.NewBuffer(key)
_, err := buf.ReadByte() // type
if err != nil {
return nil, err
}
rv.doc, err = buf.ReadBytes(ByteSeparator)
if len(rv.doc) < 2 { // 1 for min doc id length, 1 for separator
err = fmt.Errorf("invalid doc length 0")
return nil, err
}
rv.doc = rv.doc[:len(rv.doc)-1] // trim off separator byte
err = binary.Read(buf, binary.LittleEndian, &rv.field)
if err != nil {
return nil, err
}
rv.arrayPositions = make([]uint64, 0)
nextArrayPos, err := binary.ReadUvarint(buf)
for err == nil {
rv.arrayPositions = append(rv.arrayPositions, nextArrayPos)
nextArrayPos, err = binary.ReadUvarint(buf)
}
return &rv, nil
}
示例3: recv
func (p *Player) recv() {
for {
length, err := ReadVarint(p.conn)
if err != nil {
p.Game.RemovePlayer(p.Name)
fmt.Println("Player disconnected")
return
}
buf := make([]byte, length)
io.ReadFull(p.conn, buf)
r := bytes.NewReader(buf)
binary.ReadUvarint(r)
id, _ := binary.ReadUvarint(r)
switch id {
case 0x01:
message := ReadString(r)
if strings.HasPrefix(message, "/") {
// Commands
} else {
WriteChatMessage(p.Game, fmt.Sprintf("<%s> %s", p.Name, message))
fmt.Printf("<%s> %s\n", p.Name, message)
}
}
}
}
示例4: readEntry
func readEntry(reader *dirIndexReader, offset uint64) (*indexEntry, error) {
seekErr := seekIndex(reader, offset)
if seekErr != nil {
return nil, seekErr
}
n, err := binary.ReadUvarint(reader.indexReader)
if err != nil {
return nil, err
}
entries := []indexEntryElement{}
for i := uint64(0); i < n; i++ {
docId, derr := binary.ReadUvarint(reader.indexReader)
if derr != nil {
return nil, derr
}
var freq float64
ferr := binary.Read(reader.indexReader, binary.LittleEndian, &freq)
if ferr != nil {
return nil, ferr
}
entries = append(entries, indexEntryElement{docId, freq})
}
tail, terr := binary.ReadUvarint(reader.indexReader)
if terr != nil {
return nil, terr
}
return &indexEntry{entries, tail}, nil
}
示例5: UnserializeSimpleTask
// Format of a simple task:
// 2 bytes : magic number
// 8 bytes : id length : l
// 8 bytes : data length : n
// 32 bytes : target
// 32 bytes : action
// l bytes : id
// n bytes : data
func UnserializeSimpleTask(data []byte) *SimpleTask {
// Magic number 01
if data[0] != 0 || data[1] != 1 {
return nil
}
// ID Length
idLength, err := binary.ReadUvarint(bytes.NewBuffer(data[2:10]))
if err != nil {
return nil
}
// Data Length
length, err := binary.ReadUvarint(bytes.NewBuffer(data[10:18]))
if err != nil {
return nil
}
// Target
target := string(data[18:50])
// Action
action := string(data[50:82])
// ID
readId := string(data[82 : 82+idLength])
// Data
readData := data[82+idLength : 82+idLength+length]
return NewSimpleTask(readId, target, action, readData)
}
示例6: readUvarint
func (bin *mBinaryProto) readUvarint(reader io.Reader) (uint64, error) {
if br, ok := reader.(io.ByteReader); ok {
return binary.ReadUvarint(br)
}
br := newByteReader(reader, bin.readBuffer)
return binary.ReadUvarint(br)
}
示例7: ReadHandshake
func ReadHandshake(r *bytes.Reader) Handshake {
handshake := Handshake{}
handshake.Version, _ = binary.ReadUvarint(r)
handshake.Address = ReadString(r)
binary.Read(r, binary.BigEndian, &handshake.Port)
handshake.State, _ = binary.ReadUvarint(r)
return handshake
}
示例8: readMsgDial
func readMsgDial(r io.ByteReader) (*msgDial, error) {
id, err := binary.ReadUvarint(r)
if err != nil {
return nil, err
}
seqno, err := binary.ReadUvarint(r)
if err != nil {
return nil, err
}
return &msgDial{ID: chainID(id), SeqNo: SeqNo(seqno)}, nil
}
示例9: ReadDHCPOptions
func ReadDHCPOptions(payload []byte) ([]DHCPOption, error) {
magicCookieStart := 236
magicCookieEnd := 240
if len(payload) < magicCookieEnd {
return []DHCPOption{}, PayloadError
}
magicCookie := []byte{byte(99), byte(130), byte(83), byte(99)}
if !bytes.Equal(payload[magicCookieStart:magicCookieEnd], magicCookie) {
return []DHCPOption{}, errors.New("Cannot find magic cookie")
}
opts := []DHCPOption{}
pR := bytes.NewReader(payload[magicCookieEnd:])
// End Loop when only 255 end DHCP option is left
for {
buf := make([]byte, 1)
if _, err := pR.Read(buf); err != nil {
return []DHCPOption{}, err
}
// break if the end is reached
if bytes.Equal(buf, []byte{byte(255)}) {
break
}
code, err := binary.ReadUvarint(bytes.NewReader(buf))
if err != nil {
return []DHCPOption{}, err
}
buf = make([]byte, 1)
if _, err := pR.Read(buf); err != nil {
return []DHCPOption{}, err
}
size, err := binary.ReadUvarint(bytes.NewReader(buf))
if err != nil {
return []DHCPOption{}, err
}
buf = make([]byte, size)
if _, err := pR.Read(buf); err != nil {
return []DHCPOption{}, err
}
opts = append(opts, DHCPOption{code, buf, size})
}
return opts, nil
}
示例10: next
func (r *restartRange) next() (err error) {
if r.cached && len(r.cache) > r.pos {
r.kv = r.cache[r.pos]
r.pos++
return
}
if r.buf.Len() == 0 {
return io.EOF
}
var nkey []byte
// Read header
var shared, nonShared, valueLen uint64
shared, err = binary.ReadUvarint(r.buf)
if err != nil || shared > uint64(len(r.kv.key)) {
goto corrupt
}
nonShared, err = binary.ReadUvarint(r.buf)
if err != nil {
goto corrupt
}
valueLen, err = binary.ReadUvarint(r.buf)
if err != nil {
goto corrupt
}
if nonShared+valueLen > uint64(r.buf.Len()) {
goto corrupt
}
if r.cached && r.pos > 0 {
r.cache = append(r.cache, r.kv)
}
// Read content
nkey = r.buf.Next(int(nonShared))
if shared == 0 {
r.kv.key = nkey
} else {
pkey := r.kv.key[:shared]
key := make([]byte, shared+nonShared)
copy(key, pkey)
copy(key[shared:], nkey)
r.kv.key = key
}
r.kv.value = r.buf.Next(int(valueLen))
r.pos++
return
corrupt:
return errors.ErrCorrupt("bad entry in block")
}
示例11: readHeader
func (conn *conn) readHeader() (uint64, uint64, error) {
seq, err := binary.ReadUvarint(conn.buf)
if err != nil {
return 0, 0, err
}
size, err := binary.ReadUvarint(conn.buf)
if err != nil {
return 0, 0, err
}
return seq, size, nil
}
示例12: readUint64
func readUint64(r Reader) uint64 {
x, err := binary.ReadUvarint(r)
if err != nil {
log.Fatal(err)
}
return x
}
示例13: decodeUint
func decodeUint(buf *bytes.Reader) (uint32, error) {
v, err := binary.ReadUvarint(buf)
if v > 0xffffffff {
return 0, errors.New("Something wrong, this number looks too big")
}
return uint32(v), err
}
示例14: NewStreamReader
// NewStreamReader returns a reader that will decode the supplied data stream.
//
// This is compatible content from the NewStreamWriter function.
//
// When you are done with the Reader, use Close to release resources.
func NewStreamReader(in io.Reader) (Reader, error) {
f := &streamReader{
ready: make(chan *rblock, 8), // Read up to 8 blocks ahead
closeReader: make(chan struct{}, 0),
readerClosed: make(chan struct{}, 0),
curBlock: 0,
}
br := bufio.NewReader(in)
format, err := binary.ReadUvarint(br)
if err != nil {
return nil, err
}
switch format {
case 2:
err = f.readFormat2(br)
if err != nil {
return nil, err
}
default:
return nil, ErrUnknownFormat
}
go f.streamReader(br)
return f, nil
}
示例15: NewSeekReader
// NewSeekRead returns a reader that will decode the supplied index and data stream.
//
// This is compatible content from the NewWriter function.
//
// No blocks will be kept in memory, but the block data input must be seekable.
// The function will decode the index before returning.
//
// When you are done with the Reader, use Close to release resources.
func NewSeekReader(index io.Reader, blocks io.ReadSeeker) (IndexedReader, error) {
f := &reader{streamReader: streamReader{
ready: make(chan *rblock, 8), // Read up to 8 blocks ahead
closeReader: make(chan struct{}, 0),
readerClosed: make(chan struct{}, 0),
curBlock: 0,
maxLength: 8, // We have 8 blocks readahead.
}}
idx := bufio.NewReader(index)
format, err := binary.ReadUvarint(idx)
if err != nil {
return nil, err
}
switch format {
case 1:
err = f.readFormat1(idx)
default:
err = ErrUnknownFormat
}
go f.seekReader(blocks)
return f, err
}