@@ -12,58 +12,69 @@ const maxDatagramSize = 8192
12
12
// Publisher can publish state and commands to the teams
13
13
type Publisher struct {
14
14
address string
15
- conn * net.UDPConn
15
+ conns [] * net.UDPConn
16
16
}
17
17
18
18
// NewPublisher creates a new publisher that publishes referee messages via UDP to the teams
19
19
func NewPublisher (address string ) (p * Publisher ) {
20
20
p = new (Publisher )
21
21
p .address = address
22
-
22
+ p . conns = [] * net. UDPConn {}
23
23
return
24
24
}
25
25
26
26
func (p * Publisher ) connect () bool {
27
- p .conn = nil
27
+ p .disconnect ()
28
28
29
29
addr , err := net .ResolveUDPAddr ("udp" , p .address )
30
30
if err != nil {
31
31
log .Printf ("Could not resolve address '%v': %v" , p .address , err )
32
32
return false
33
33
}
34
34
35
- conn , err := net .DialUDP ("udp" , nil , addr )
36
- if err != nil {
37
- log .Printf ("Could not connect to '%v': %v" , addr , err )
38
- return false
39
- }
35
+ iaddrs , err := net .InterfaceAddrs ()
36
+ for _ , iaddr := range iaddrs {
37
+ ip := iaddr .(* net.IPNet ).IP
38
+ if ip .To4 () == nil {
39
+ continue
40
+ }
41
+ laddr := & net.UDPAddr {
42
+ IP : ip ,
43
+ }
44
+ conn , err := net .DialUDP ("udp" , laddr , addr )
45
+ if err != nil {
46
+ log .Printf ("Could not connect to '%v': %v" , addr , err )
47
+ continue
48
+ }
40
49
41
- if err := conn .SetWriteBuffer (maxDatagramSize ); err != nil {
42
- log .Printf ("Could not set write buffer to %v." , maxDatagramSize )
43
- }
44
- log .Println ("Publishing referee messages to" , p . address )
50
+ if err := conn .SetWriteBuffer (maxDatagramSize ); err != nil {
51
+ log .Printf ("Could not set write buffer to %v." , maxDatagramSize )
52
+ }
53
+ log .Printf ("Publishing referee messages to %s at %s " , conn . RemoteAddr (), conn . LocalAddr () )
45
54
46
- p .conn = conn
55
+ p .conns = append (p .conns , conn )
56
+ }
47
57
return true
48
58
}
49
59
50
60
func (p * Publisher ) disconnect () {
51
- p .conn = nil
61
+ p .conns = [] * net. UDPConn {}
52
62
}
53
63
54
64
func (p * Publisher ) SendMessage (refereeMsg * state.Referee ) {
55
- if p . conn == nil && ! p .connect () {
65
+ if len ( p . conns ) == 0 && ! p .connect () {
56
66
return
57
67
}
58
-
59
- bytes , err := proto .Marshal (refereeMsg )
60
- if err != nil {
61
- log .Printf ("Could not marshal referee message: %v\n Error: %v" , refereeMsg , err )
62
- return
63
- }
64
- _ , err = p .conn .Write (bytes )
65
- if err != nil {
66
- log .Printf ("Could not write message: %v" , err )
67
- p .disconnect ()
68
+ for _ , conn := range p .conns {
69
+ bytes , err := proto .Marshal (refereeMsg )
70
+ if err != nil {
71
+ log .Printf ("Could not marshal referee message: %v\n Error: %v" , refereeMsg , err )
72
+ return
73
+ }
74
+ _ , err = conn .Write (bytes )
75
+ if err != nil {
76
+ log .Printf ("Could not write message: %v" , err )
77
+ p .disconnect ()
78
+ }
68
79
}
69
80
}
0 commit comments