forked from Cistern/sflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
54 lines (43 loc) · 908 Bytes
/
sample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package sflow
import (
"encoding/binary"
"errors"
"io"
)
const (
TypeFlowSample = 1
TypeCounterSample = 2
TypeExpandedFlowSample = 3
TypeExpandedCounterSample = 4
)
var (
ErrUnknownSampleType = errors.New("sflow: Unknown sample type")
)
type Sample interface {
SampleType() int
GetRecords() []Record
encode(w io.Writer) error
}
func decodeSample(r io.ReadSeeker) (Sample, error) {
format, length, err := uint32(0), uint32(0), error(nil)
err = binary.Read(r, binary.BigEndian, &format)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &length)
if err != nil {
return nil, err
}
switch format {
case TypeCounterSample:
return decodeCounterSample(r)
case TypeFlowSample:
return decodeFlowSample(r)
default:
_, err = r.Seek(int64(length), 1)
if err != nil {
return nil, err
}
return nil, ErrUnknownSampleType
}
}