-
Notifications
You must be signed in to change notification settings - Fork 16
/
challenge.go
50 lines (38 loc) · 896 Bytes
/
challenge.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
package a2s
import (
"errors"
)
const (
A2S_PLAYER_CHALLENGE_REPLY_HEADER = 0x41
)
var (
ErrBadChallengeResponse = errors.New("Bad challenge response")
)
func (c *Client) getChallenge(header []byte, fullResult byte) ([]byte, bool, error) {
if err := c.send(header); err != nil {
return nil, false, err
}
data, err := c.receive()
if err != nil {
return nil, false, err
}
reader := NewPacketReader(data)
switch int32(reader.ReadUint32()) {
case -2:
// We received an unexpected full reply
return data, true, nil
case -1:
// Continue
default:
return nil, false, ErrBadPacketHeader
}
switch reader.ReadUint8() {
case A2S_PLAYER_CHALLENGE_REPLY_HEADER:
// Received a challenge number
return data[reader.Pos() : reader.Pos()+4], false, nil
case fullResult:
// Received full result
return data, true, nil
}
return nil, false, ErrBadChallengeResponse
}