diff options
Diffstat (limited to 'packet/clc/connectionless.go')
| -rw-r--r-- | packet/clc/connectionless.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/packet/clc/connectionless.go b/packet/clc/connectionless.go new file mode 100644 index 0000000..96db112 --- /dev/null +++ b/packet/clc/connectionless.go @@ -0,0 +1,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 +} |
