예제 #1
0
파일: decoder.go 프로젝트: jyzhe/beehive
// Decode decodes the next raft message. If there is no data to read it returns
// io.EOF.
func (d *Decoder) Decode(m *raftpb.Message) error {
	var buf [4]byte
	b := buf[:4]
	n, err := d.r.Read(b)
	if err != nil {
		return err
	}
	if n != 4 {
		return ErrNoEnoughData
	}
	s := int(binary.BigEndian.Uint32(b))
	b = make([]byte, s)
	n, err = io.ReadFull(d.r, b)
	if err != nil {
		return err
	}
	if n != s {
		return ErrNoEnoughData
	}
	m.Unmarshal(b)
	return nil
}