-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiveroptions.go
93 lines (79 loc) · 2.45 KB
/
receiveroptions.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package lcm
import (
"net"
"golang.org/x/net/bpf"
"google.golang.org/protobuf/proto"
)
// receiverOptions are the configuration options for an LCM receiver.
type receiverOptions struct {
interfaceName string
port int
ips []net.IP
bufferSizeBytes int
batchSize int
bpfProgram []bpf.Instruction
protos []proto.Message
}
// DefaultMulticastIP returns the default LCM multicast IP.
func DefaultMulticastIP() net.IP {
return net.IPv4(239, 255, 76, 67)
}
// DefaultPort is the default LCM port.
const DefaultPort = 7667
// defaultReceiverOptions returns receiver options with sensible default values.
func defaultReceiverOptions() *receiverOptions {
return &receiverOptions{
batchSize: 5,
port: DefaultPort,
bufferSizeBytes: 2097152, // 2MB (from the LCM documentation)
bpfProgram: shortMessageFilter(), // TODO: add support for fragmented messages
}
}
// ReceiverOption configures an LCM receiver.
type ReceiverOption func(*receiverOptions)
// WithReceivePort configures the port to listen on.
func WithReceivePort(port int) ReceiverOption {
return func(o *receiverOptions) {
o.port = port
}
}
// WithReceiveInterface configures the interface to receive on.
func WithReceiveInterface(interfaceName string) ReceiverOption {
return func(o *receiverOptions) {
o.interfaceName = interfaceName
}
}
// WithReceiveAddress a multicast group address to receive from.
//
// Provide this option multiple times to join multiple multicast groups.
func WithReceiveAddress(ip net.IP) ReceiverOption {
return func(o *receiverOptions) {
o.ips = append(o.ips, ip)
}
}
// WithReceiveBPF configures the Berkely Packet Filter to set on the receiver socket.
//
// Ineffectual in non-Linux environments.
func WithReceiveBPF(program []bpf.Instruction) ReceiverOption {
return func(o *receiverOptions) {
o.bpfProgram = program
}
}
func WithReceiveProtos(msgs ...proto.Message) ReceiverOption {
return func(o *receiverOptions) {
o.bpfProgram = shortProtoMessageFilter(msgs...)
o.protos = msgs
}
}
// WithReceiveBufferSize configures the kernel read buffer size (in bytes).
func WithReceiveBufferSize(n int) ReceiverOption {
return func(o *receiverOptions) {
o.bufferSizeBytes = n
}
}
// WithReceiveBatchSize configures the max number of messages to receive from the kernel in a single batch.
func WithReceiveBatchSize(n int) ReceiverOption {
return func(o *receiverOptions) {
o.batchSize = n
}
}