forked from WorldUnitedNFS/freeroam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
360 lines (332 loc) · 8.79 KB
/
client.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package freeroam
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/WorldUnitedNFS/freeroam/math"
"net"
"runtime/debug"
"sort"
"sync"
"time"
)
type clientPosSortInfo struct {
Client *Client
Length int
}
type clientPosSort []clientPosSortInfo
func (self clientPosSort) Len() int {
return len(self)
}
func (self clientPosSort) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self clientPosSort) Less(i, j int) bool {
return self[i].Length < self[j].Length
}
type ClientConfig struct {
CliTime []byte
Addr *net.UDPAddr
Conn *net.UDPConn
Buffers *sync.Pool
Clients map[string]*Client
AllowedPersonas []int
}
func newClient(opts ClientConfig) *Client {
c := &Client{
Addr: opts.Addr,
conn: opts.Conn,
startTime: time.Now(),
cliTime: binary.BigEndian.Uint16(opts.CliTime),
seq: 0,
slots: make([]*slotInfo, 14),
LastPacket: time.Now(),
clients: opts.Clients,
allowedPersonas: opts.AllowedPersonas,
buffers: opts.Buffers,
updateID: 1,
}
return c
}
type Client struct {
Addr *net.UDPAddr
conn *net.UDPConn
startTime time.Time
cliTime uint16
seq uint16
carPos CarPosPacket
chanInfo []byte
playerInfo []byte
slots []*slotInfo
LastPacket time.Time
LastPacketSeq uint16
Ping int
PersonaName string
allowedPersonas []int
ackMissedCount int
updateID uint8
buffers *sync.Pool
clients map[string]*Client
timeDiffDiff int
hasCalcDD bool
posRecvTD uint16
}
func (c *Client) registerUpdate() {
c.updateID++
if c.updateID == 0 {
c.updateID = 1
}
}
func (c Client) getTimeDiff() uint16 {
return uint16(time.Now().Sub(c.startTime).Seconds() * 1000)
}
func (c *Client) getSeq() uint16 {
out := c.seq
c.seq++
return out
}
func (c *Client) replyHandshake() {
buf := c.buffers.Get().(*bytes.Buffer)
buf.Reset()
binary.Write(buf, binary.BigEndian, c.getSeq())
buf.WriteByte(0x01)
binary.Write(buf, binary.BigEndian, c.getTimeDiff())
binary.Write(buf, binary.BigEndian, c.cliTime)
buf.Write([]byte{0x49, 0x26, 0x03, 0x01})
c.SendRawPacket(buf.Bytes())
c.buffers.Put(buf)
}
// Active returns true if the client has communicated with the server lately.
func (c Client) Active() bool {
return time.Now().Sub(c.LastPacket).Seconds() < 5
}
func (c *Client) processPacket(packet []byte) {
defer func() {
r := recover()
if r != nil {
fmt.Printf("Error occured while processing packet (%v):\n", len(packet))
fmt.Println(r)
debug.PrintStack()
}
}()
c.Ping = int(time.Now().Sub(c.LastPacket).Milliseconds())
c.LastPacket = time.Now()
pktSeq := binary.BigEndian.Uint16(packet[0:2])
if pktSeq == 65535 {
c.LastPacketSeq = 0
}
srvCounter := binary.BigEndian.Uint16(packet[8:10])
for _, slot := range c.slots {
if slot != nil && !slot.UpdateACKed {
if srvCounter == slot.PacketSentSeq {
slot.UpdateACKed = true
} else {
slot.ACKMissedCount++
}
}
}
var updated bool
data := packet[16 : len(packet)-5]
reader := bytes.NewReader(data)
for {
ptype, err := reader.ReadByte()
if err != nil {
break
}
plen, _ := reader.ReadByte()
innerData := make([]byte, plen)
reader.Read(innerData)
switch ptype {
case 0x00:
c.chanInfo = innerData
updated = true
case 0x01:
if c.allowedPersonas != nil {
personaID := binary.LittleEndian.Uint32(innerData[41:45])
var allowed bool
for _, id := range c.allowedPersonas {
if id == int(personaID) {
allowed = true
break
}
}
if !allowed {
fmt.Printf("Kicking %v; %v != %v\n", c.Addr.String(), personaID, c.allowedPersonas)
delete(c.clients, c.Addr.String())
return
}
}
c.playerInfo = innerData
nameField := innerData[1:33]
c.PersonaName = string(nameField[:cStrLen(nameField)])
updated = true
case 0x12:
if pktSeq >= c.LastPacketSeq {
if c.IsReady() && !bytes.Equal(innerData[2:], c.carPos.packet[2:]) {
updated = true
}
c.carPos.Update(innerData)
c.posRecvTD = c.getTimeDiff()
}
}
}
if c.IsReady() {
if updated {
c.registerUpdate()
}
c.sendPlayerSlots()
}
c.LastPacketSeq = pktSeq
}
func (self *Client) getClosestPlayers(clients []*Client) []*Client {
closePlayers := make([]clientPosSortInfo, 0)
for _, client := range clients {
if !client.IsReady() || client.Addr == self.Addr {
continue
}
distance := math.Distance(self.GetPos(), client.GetPos())
closePlayers = append(closePlayers, clientPosSortInfo{
Length: int(distance),
Client: client,
})
}
sort.Sort(clientPosSort(closePlayers))
out := make([]*Client, min(14, len(closePlayers)))
for i := range out {
out[i] = closePlayers[i].Client
}
return out
}
func (self *Client) removeSlot(client *Client) {
index := func() int {
for i, c := range self.slots {
if c != nil && c.Client == client {
return i
}
}
return -1
}()
self.slots[index] = nil
}
func (self *Client) addSlot(client *Client) {
index := -1
for i, c := range self.slots {
if c == nil {
index = i
break
}
}
if index == -1 {
panic("addSlot: tried to add client with all slots full")
}
self.slots[index] = &slotInfo{
Client: client,
}
}
func (self *Client) recalculateSlots(clients []*Client) {
players := self.getClosestPlayers(clients)
oldPlayers := make([]*Client, 0)
for _, v := range self.slots {
if v != nil {
oldPlayers = append(oldPlayers, v.Client)
}
}
diff := ArrayDiff(oldPlayers, players)
// Adding and removing slots at the same time causes some weird things to happen.
// As a temporary workaround, only one section of the diff is handled at a time.
// This allows the game to remove old players BEFORE swapping in new ones.
if len(diff.Removed) > 0 {
for _, c := range diff.Removed {
self.removeSlot(c)
}
} else if len(diff.Added) > 0 {
for _, c := range diff.Added {
self.addSlot(c)
}
}
}
func (c *Client) sendPlayerSlots() {
clients := make([]*Client, len(c.clients))
{
i := 0
for _, cl := range c.clients {
clients[i] = cl
i++
}
}
c.recalculateSlots(clients)
buf := c.buffers.Get().(*bytes.Buffer)
buf.Reset()
seq := c.getSeq()
binary.Write(buf, binary.BigEndian, seq)
buf.WriteByte(0x02)
binary.Write(buf, binary.BigEndian, c.getTimeDiff())
binary.Write(buf, binary.BigEndian, c.cliTime)
binary.Write(buf, binary.BigEndian, seq)
buf.Write([]byte{0xff, 0xff, 0x00})
fullsSent := 0
for _, slot := range c.slots {
if slot == nil {
buf.Write([]byte{0xff, 0xff})
} else {
pktTime := uint16(int(c.getTimeDiff()) - slot.Client.Ping)
if slot.HasSentFull && slot.Client.posRecvTD == slot.LastCPTime {
buf.Write([]byte{0x00, 0xff})
} else if fullsSent >= 3 {
slot.Client.writeFullPosPacket(buf, pktTime)
slot.LastCPTime = slot.Client.posRecvTD
} else if !slot.HasSentFull {
slot.Client.writeFullSlotPacket(buf, pktTime)
slot.HasSentFull = true
slot.PacketSentSeq = seq
slot.LastCPTime = slot.Client.posRecvTD
fullsSent++
} else if slot.UpdateACKed || slot.ACKMissedCount < 5 {
slot.Client.writeFullPosPacket(buf, pktTime)
slot.LastCPTime = slot.Client.posRecvTD
} else {
slot.Client.writeFullSlotPacket(buf, pktTime)
slot.ACKMissedCount = 0
slot.PacketSentSeq = seq
slot.LastCPTime = slot.Client.posRecvTD
fullsSent++
}
}
}
buf.Write([]byte{0x01, 0x01, 0x01, 0x01})
c.SendRawPacket(buf.Bytes())
c.buffers.Put(buf)
}
// GetPos returns the current position of the client.
func (c Client) GetPos() math.Vector2D {
return c.carPos.Pos()
}
// GetRotation returns the current rotation of the client.
func (c Client) GetRotation() float64 {
return c.carPos.Rotation()
}
// SendRawPacket sends a raw UDP packet to the client.
func (c *Client) SendRawPacket(b []byte) error {
_, err := c.conn.WriteToUDP(b, c.Addr)
return err
}
// IsReady returns true if the client is ready to be broadcasted to other clients.
// This means that the server has valid channel info, player info and position data of the client.
func (c Client) IsReady() bool {
return c.chanInfo != nil && c.playerInfo != nil && c.carPos.Valid()
}
func (c Client) writeFullPosPacket(buf *bytes.Buffer, time uint16) {
buf.WriteByte(0x00) // Slot start
WriteSubpacket(buf, 0x12, c.carPos.Packet(time))
buf.WriteByte(0xff) // Slot end
}
func (c Client) writeFullSlotPacket(buf *bytes.Buffer, time uint16) {
buf.WriteByte(0x00) // Slot start
WriteSubpacket(buf, 0x00, c.chanInfo)
WriteSubpacket(buf, 0x01, c.playerInfo)
WriteSubpacket(buf, 0x12, c.carPos.Packet(time))
buf.WriteByte(0xff) // Slot end
}