Skip to content
Snippets Groups Projects
common_header.go 783 B
Newer Older
  • Learn to ignore specific revisions
  • package packet
    
    import (
    	"bytes"
    
    	"github.com/bio-routing/bio-rd/util/decoder"
    
    Oliver Herms's avatar
    Oliver Herms committed
    	"github.com/taktv6/tflow2/convert"
    
    Oliver Herms's avatar
    Oliver Herms committed
    	// CommonHeaderLen is the length of a common header
    
    Oliver Herms's avatar
    Oliver Herms committed
    // CommonHeader represents a common header
    
    type CommonHeader struct {
    	Version   uint8
    	MsgLength uint32
    	MsgType   uint8
    }
    
    
    Oliver Herms's avatar
    Oliver Herms committed
    // Serialize serializes a common header
    
    func (c *CommonHeader) Serialize(buf *bytes.Buffer) {
    	buf.WriteByte(c.Version)
    	buf.Write(convert.Uint32Byte(c.MsgLength))
    	buf.WriteByte(c.MsgType)
    }
    
    func decodeCommonHeader(buf *bytes.Buffer) (*CommonHeader, error) {
    	ch := &CommonHeader{}
    	fields := []interface{}{
    		&ch.Version,
    		&ch.MsgLength,
    		&ch.MsgType,
    	}
    
    	err := decoder.Decode(buf, fields)
    	if err != nil {
    		return ch, err
    	}
    
    	return ch, nil
    }