@@ -10,31 +10,31 @@ import (
10
10
11
11
const maxDatagramSize = 8192
12
12
13
- type RefBoxPublisher struct {
14
- Conn * net.UDPConn
15
- Message sslproto.SSL_Referee
13
+ type Publisher struct {
14
+ conn * net.UDPConn
15
+ message sslproto.SSL_Referee
16
16
}
17
17
18
- // NewBroadcaster creates a new UDP multicast connection on which to broadcast
19
- func NewRefBoxPublisher (address string ) RefBoxPublisher {
18
+ // Create a new publisher that publishes referee messages via UDP to the teams
19
+ func NewPublisher (address string ) ( publisher Publisher , err error ) {
20
20
addr , err := net .ResolveUDPAddr ("udp" , address )
21
21
if err != nil {
22
- log . Fatalln ( err )
22
+ return
23
23
}
24
24
25
25
conn , err := net .DialUDP ("udp" , nil , addr )
26
26
if err != nil {
27
- log . Fatalln ( err )
27
+ return
28
28
}
29
+
29
30
conn .SetReadBuffer (maxDatagramSize )
30
31
log .Println ("Connected to" , address )
31
32
32
- publisher := RefBoxPublisher {}
33
- publisher .Conn = conn
33
+ publisher .conn = conn
34
34
35
- initRefereeMessage (& publisher .Message )
35
+ initRefereeMessage (& publisher .message )
36
36
37
- return publisher
37
+ return
38
38
}
39
39
40
40
func initRefereeMessage (m * sslproto.SSL_Referee ) {
@@ -61,15 +61,20 @@ func initTeamInfo(t *sslproto.SSL_Referee_TeamInfo) {
61
61
t .Goalie = new (uint32 )
62
62
}
63
63
64
- func (p * RefBoxPublisher ) Publish (state * RefBoxState , command * RefBoxEventCommand ) {
64
+ // Publish the state and command
65
+ func (p * Publisher ) Publish (state * RefBoxState , command * RefBoxEventCommand ) {
65
66
66
- updateMessage (& p .Message , state , command )
67
- bytes , err := proto .Marshal (& p .Message )
67
+ if p .conn == nil {
68
+ return
69
+ }
70
+
71
+ updateMessage (& p .message , state , command )
72
+ bytes , err := proto .Marshal (& p .message )
68
73
if err != nil {
69
74
log .Printf ("Could not marshal referee message: %v\n Error: %v" , state , err )
70
75
return
71
76
}
72
- _ , err = p .Conn .Write (bytes )
77
+ _ , err = p .conn .Write (bytes )
73
78
if err != nil {
74
79
log .Printf ("Could not write message: %v" , err )
75
80
}
@@ -90,6 +95,7 @@ func updateMessage(r *sslproto.SSL_Referee, state *RefBoxState, command *RefBoxE
90
95
* r .CommandTimestamp = uint64 (time .Now ().UnixNano () / 1000 )
91
96
}
92
97
}
98
+
93
99
func mapCommand (c * RefBoxEventCommand ) sslproto.SSL_Referee_Command {
94
100
switch c .Type {
95
101
case CommandHalt :
0 commit comments