Example #1
0
func read(reuseBuf *Header, r io.Reader) (int, *Ins, error) {
	header := reuseBuf[:]
	var expectMsgId *uint64
	if r, ok := r.(*utils.Bufio); ok {
		off := uint64(r.Offset(-1))
		expectMsgId = &off
	}

	n, err := io.ReadFull(r, header)
	nRead := n
	if err != nil {
		return n, nil, logex.Trace(err)
	}

	var length uint32
	if err := ReadHeader(header, &length); err != nil {
		return n, nil, logex.Trace(err)
	}

	content := make([]byte, SizeMsgHeader+int(length))
	n, err = io.ReadFull(r, content[SizeMsgHeader:])
	nRead += n
	if err != nil {
		return nRead, nil, logex.Trace(err)
	}

	copy(content, header)
	m, err := Decode(content)
	if err != nil {
		return nRead, nil, logex.Trace(err)
	}
	if expectMsgId != nil && m.MsgId != *expectMsgId {
		return nRead, nil, ErrMsgIdNotMatch.Trace(*expectMsgId, m.MsgId)
	}
	return nRead, m, nil
}