blob: 35527f1a335c66a4cc5a3045e2321fcf9a9e3a36 (
plain) (
blame)
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
|
package connect
import (
"errors"
"fmt"
"strconv"
"github.com/osm/quake/common/args"
"github.com/osm/quake/common/buffer"
"github.com/osm/quake/common/context"
"github.com/osm/quake/common/infostring"
"github.com/osm/quake/protocol"
)
type Command struct {
Command string
Version string
QPort uint16
ChallengeID string
UserInfo *infostring.InfoString
Extensions []*protocol.Extension
}
func (cmd *Command) Bytes() []byte {
buf := buffer.New()
buf.PutBytes([]byte(cmd.Command + " "))
buf.PutBytes([]byte(cmd.Version + " "))
buf.PutBytes([]byte(fmt.Sprintf("%v ", cmd.QPort)))
buf.PutBytes([]byte(cmd.ChallengeID + " "))
if cmd.UserInfo != nil {
buf.PutBytes(cmd.UserInfo.Bytes())
}
buf.PutByte(0x0a)
for _, ext := range cmd.Extensions {
if ext != nil {
buf.PutBytes(ext.Bytes())
}
}
return buf.Bytes()
}
func Parse(ctx *context.Context, buf *buffer.Buffer, arg args.Arg) (*Command, error) {
var cmd Command
if len(arg.Args) < 4 {
return nil, errors.New("unexpected length of args")
}
cmd.Command = arg.Cmd
cmd.Version = arg.Args[0]
qPort, err := strconv.ParseUint(arg.Args[1], 10, 16)
if err != nil {
return nil, err
}
cmd.QPort = uint16(qPort)
cmd.ChallengeID = arg.Args[2]
cmd.UserInfo = infostring.Parse(arg.Args[3])
return &cmd, nil
}
|