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
|
package quake
import (
"errors"
"net"
"syscall"
"time"
"github.com/osm/quake/common/sequencer"
"github.com/osm/quake/packet"
"github.com/osm/quake/packet/clc"
"github.com/osm/quake/packet/command"
"github.com/osm/quake/packet/command/connect"
"github.com/osm/quake/packet/command/getchallenge"
"github.com/osm/quake/packet/command/ip"
"github.com/osm/quake/packet/svc"
)
func (c *Client) Connect(addrPort string) error {
addr, err := net.ResolveUDPAddr("udp", addrPort)
if err != nil {
return err
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return err
}
c.conn = conn
c.sendChallenge()
buf := make([]byte, 1024*64)
for {
var isReadTimeout bool
var incomingSeq uint32
var incomingAck uint32
var packet packet.Packet
var cmds []command.Command
if err := c.conn.SetReadDeadline(
time.Now().Add(time.Millisecond * c.readDeadline),
); err != nil {
c.logger.Printf("unable to set read deadline, %v\n", err)
}
n, _, err := c.conn.ReadFromUDP(buf)
if err != nil {
if errors.Is(err, syscall.ECONNREFUSED) {
c.logger.Printf("lost connection - reconnecting in 5 seqonds")
time.Sleep(time.Second * 5)
c.sendChallenge()
continue
}
if err, ok := err.(net.Error); ok && err.Timeout() {
isReadTimeout = true
goto process
}
c.logger.Printf("unexpected read error, %v", err)
continue
}
packet, err = svc.Parse(c.ctx, buf[:n])
if err != nil {
c.logger.Printf("error when parsing server data, %v", err)
continue
}
switch p := packet.(type) {
case *svc.Connectionless:
cmds = []command.Command{p.Command}
case *svc.GameData:
cmds = p.Commands
incomingSeq = p.Seq
incomingAck = p.Ack
}
for _, h := range c.handlers {
cmds = append(cmds, h(packet)...)
}
process:
c.processCommands(incomingSeq, incomingAck, cmds, isReadTimeout)
}
}
func (c *Client) processCommands(
incomingSeq, incomingAck uint32,
serverCmds []command.Command,
isReadTmeout bool,
) {
var cmds []command.Command
for _, serverCmd := range serverCmds {
for _, cmd := range c.handleServerCommand(serverCmd) {
switch cmd := cmd.(type) {
case *connect.Command, *ip.Command:
if _, err := c.conn.Write(
(&clc.Connectionless{Command: cmd}).Bytes(),
); err != nil {
c.logger.Printf("unable to send connectionless command, %v\n", err)
}
default:
cmds = append(cmds, cmd)
}
}
}
if c.seq.GetState() <= sequencer.Handshake {
return
}
c.cmdsMu.Lock()
outSeq, outAck, outCmds, err := c.seq.Process(incomingSeq, incomingAck, append(c.cmds, cmds...))
c.cmds = []command.Command{}
c.cmdsMu.Unlock()
if err == sequencer.ErrRateLimit {
return
}
if _, err := c.conn.Write((&clc.GameData{
Seq: outSeq,
Ack: outAck,
QPort: c.qPort,
Commands: append(outCmds, c.getMove(outSeq)),
}).Bytes()); err != nil {
c.logger.Printf("unable to send game data, %v\n", err)
}
}
func (c *Client) sendChallenge() {
c.seq.Reset()
c.seq.SetState(sequencer.Handshake)
c.readDeadline = connectReadDeadline
if _, err := c.conn.Write(
(&clc.Connectionless{Command: &getchallenge.Command{}}).Bytes(),
); err != nil {
c.logger.Printf("unable to send challenge, %v\n", err)
}
}
|