aboutsummaryrefslogtreecommitdiffstats
path: root/packet/clc/connectionless.go
blob: 96db11246124ce9a700e7d970c5d822a57024ec9 (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
package clc

import (
	"errors"

	"github.com/osm/quake/common/args"
	"github.com/osm/quake/common/buffer"
	"github.com/osm/quake/common/context"
	"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/passthrough"
)

type Connectionless struct {
	Command command.Command
}

func (cmd *Connectionless) Bytes() []byte {
	buf := buffer.New()

	buf.PutInt32(-1)
	buf.PutBytes(cmd.Command.Bytes())

	return buf.Bytes()
}

func parseConnectionless(ctx *context.Context, buf *buffer.Buffer) (*Connectionless, error) {
	var err error
	var pkg Connectionless

	if err := buf.Skip(4); err != nil {
		return nil, err
	}

	var str string
	if str, err = buf.GetString(); err != nil {
		return nil, err
	}

	args := args.Parse(str)
	if len(args) != 1 {
		return nil, errors.New("unexpected length of parsed arguments")
	}

	arg := args[0]

	var cmd command.Command
	switch arg.Cmd {
	case "connect":
		cmd, err = connect.Parse(ctx, buf, arg)
	case "getchallenge":
		cmd, err = getchallenge.Parse(ctx, buf)
	default:
		cmd, err = passthrough.Parse(ctx, buf, str)
	}

	if err != nil {
		return nil, err
	}
	pkg.Command = cmd

	return &pkg, nil
}