-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_send.go
105 lines (81 loc) · 1.72 KB
/
net_send.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
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"net"
"time"
log "github.com/sirupsen/logrus"
)
var lastStateSent []byte
func (n *Net) announce(iface net.Interface) {
daddr, err := net.ResolveUDPAddr("udp6", "[ff12::7179%"+iface.Name+"]:7179")
if err != nil {
log.Fatal(err)
}
conn, err := net.DialUDP("udp6", nil, daddr)
if err != nil {
return
}
defer conn.Close()
bs, err := encode(&Announce{n.id, n.announcePort})
if err != nil {
log.Fatal(err)
}
_, err = conn.Write(bs)
if err != nil {
log.Fatal(err)
}
log.Debugf("announce %v on %v (port %v)\n", n.id, iface.Name, n.announcePort)
}
func (n *Net) Announce() {
for {
ifaces, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
for _, iface := range ifaces {
n.announce(iface)
}
time.Sleep(time.Second / time.Duration(n.config.AnnounceFrequency))
}
}
func (n *Net) sendState(game *Game, seq int) {
if n.peer == nil {
return
}
// Copy Addr, use announce port as remote target port
var addr net.UDPAddr = *n.peer
addr.Port = n.announcePort
conn, err := net.DialUDP("udp6", nil, &addr)
if err != nil {
log.Fatal(err)
}
bs, err := encode(&State{n.id, game})
if err != nil {
log.Fatal(err)
}
dl := -1
if lastStateSent != nil && seq > 0 {
d := diff(lastStateSent, bs)
bs, err = encode(d)
if err != nil {
log.Fatal(err)
}
dl = len(*d)
}
c, err := conn.Write(append([]byte{byte(seq)}, bs...))
if err != nil {
log.Fatal(err)
}
log.Debugf("send state %v (size %v, diff %v)", n.peer, c, dl)
if seq == 0 {
lastStateSent = bs
}
}
func (n *Net) SendState(game *Game) {
s := 0
for {
n.sendState(game, s)
time.Sleep(time.Second / time.Duration(n.config.StateSyncFrequency))
s += 1
s %= 60 / n.config.StateFullSyncFrequency
}
}