Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions counter_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *CounterSample) GetRecords() []Record {
return s.Records
}

func decodeCounterSample(r io.ReadSeeker) (Sample, error) {
func decodeCounterSample(r io.ReadSeeker, format uint32) (Sample, error) {
s := &CounterSample{}

var err error
Expand All @@ -58,24 +58,43 @@ func decodeCounterSample(r io.ReadSeeker) (Sample, error) {
return nil, err
}

err = binary.Read(r, binary.BigEndian, &s.SourceIdType)
if err != nil {
return nil, err
}
switch format {
case TypeCounterSample:
err = binary.Read(r, binary.BigEndian, &s.SourceIdType)
if err != nil {
return nil, err
}

var srcIdIndexVal [3]byte
n, err := r.Read(srcIdIndexVal[:])
if err != nil {
return nil, err
}
var srcIdIndexVal [3]byte
n, err := r.Read(srcIdIndexVal[:])
if err != nil {
return nil, err
}

if n != 3 {
return nil, errors.New("sflow: counter sample decoding error")
}
if n != 3 {
return nil, errors.New("sflow: counter sample decoding error")
}

s.SourceIdIndexVal = uint32(srcIdIndexVal[2]) |
uint32(srcIdIndexVal[1])<<8 |
uint32(srcIdIndexVal[0])<<16

case TypeExpandedCounterSample:
var sourceIdType uint32
err = binary.Read(r, binary.BigEndian, &sourceIdType)
if err != nil {
return nil, err
}
s.SourceIdType = byte(sourceIdType)

err = binary.Read(r, binary.BigEndian, &s.SourceIdIndexVal)
if err != nil {
return nil, err
}

s.SourceIdIndexVal = uint32(srcIdIndexVal[2]) |
uint32(srcIdIndexVal[1])<<8 |
uint32(srcIdIndexVal[0])<<16
default:
return nil, ErrUnknownSampleType
}

err = binary.Read(r, binary.BigEndian, &s.numRecords)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion counter_sample_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestDecodeEncodeAndDecodeCounterSample(t *testing.T) {
buf.Read(skip[:])

// bytes.Buffer is not an io.ReadSeeker. bytes.Reader is.
decodedSample, err := decodeCounterSample(bytes.NewReader(buf.Bytes()))
decodedSample, err := decodeCounterSample(bytes.NewReader(buf.Bytes()), TypeCounterSample)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func decodeSample(r io.ReadSeeker) (Sample, error) {

switch format {
case TypeCounterSample:
return decodeCounterSample(r)
return decodeCounterSample(r, format)

case TypeExpandedCounterSample:
return decodeCounterSample(r, format)

case TypeFlowSample:
return decodeFlowSample(r)
Expand Down
Loading