aboutsummaryrefslogtreecommitdiffstats
path: root/packet/svc/connectionless.go
blob: 6d42ae2d8a7320b647e04d8c6f69365b3ab82445 (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 svc

import (
	"github.com/osm/quake/common/buffer"
	"github.com/osm/quake/common/context"
	"github.com/osm/quake/packet/command"
	"github.com/osm/quake/packet/command/a2aping"
	"github.com/osm/quake/packet/command/a2cclientcommand"
	"github.com/osm/quake/packet/command/a2cprint"
	"github.com/osm/quake/packet/command/disconnect"
	"github.com/osm/quake/packet/command/passthrough"
	"github.com/osm/quake/packet/command/s2cchallenge"
	"github.com/osm/quake/packet/command/s2cconnection"
	"github.com/osm/quake/protocol"
)

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
	}

	typ, err := buf.ReadByte()
	if err != nil {
		return nil, err
	}

	var cmd command.Command
	switch protocol.CommandType(typ) {
	case protocol.S2CConnection:
		cmd, err = s2cconnection.Parse(ctx, buf)
	case protocol.A2CClientCommand:
		cmd, err = a2cclientcommand.Parse(ctx, buf)
	case protocol.A2CPrint:
		cmd, err = a2cprint.Parse(ctx, buf)
	case protocol.A2APing:
		cmd, err = a2aping.Parse(ctx, buf)
	case protocol.S2CChallenge:
		cmd, err = s2cchallenge.Parse(ctx, buf)
	case protocol.SVCDisconnect:
		cmd, err = disconnect.Parse(ctx, buf)
	default:
		cmd, err = passthrough.Parse(ctx, buf, "")
	}

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

	return &pkg, nil
}