-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
59 lines (46 loc) · 1.1 KB
/
publisher.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
package visualization
import (
"github.com/golang/protobuf/proto"
"log"
"net"
)
const maxDatagramSize = 8192
type Publisher struct {
address string
conn *net.UDPConn
}
func NewPublisher(address string) (publisher Publisher) {
publisher.address = address
publisher.connect()
return
}
func (p *Publisher) connect() {
p.conn = nil
addr, err := net.ResolveUDPAddr("udp", p.address)
if err != nil {
log.Printf("Could not resolve address '%v': %v", p.address, err)
return
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
log.Printf("Could not connect to '%v': %v", addr, err)
return
}
if err := conn.SetWriteBuffer(maxDatagramSize); err != nil {
log.Printf("Could not set write buffer to %v.", maxDatagramSize)
}
log.Println("Publishing to", p.address)
p.conn = conn
return
}
func (p *Publisher) Send(frame *VisualizationFrame) {
bytes, err := proto.Marshal(frame)
if err != nil {
log.Printf("Could not marshal referee message: %v\nError: %v", frame, err)
return
}
_, err = p.conn.Write(bytes)
if err != nil {
log.Printf("Could not write message: %v", err)
}
}