{Rcon}.login no longer exported.

split the cmd from its args when checking timeout map.

remove final TrimPrefix, it's already handled in {Rcon}.listen()
This commit is contained in:
onyx-and-iris 2024-11-24 13:13:07 +00:00
parent 65ab17b9c9
commit 3e1088d625
2 changed files with 11 additions and 6 deletions

2
.gitignore vendored
View File

@ -25,4 +25,4 @@ go.work.sum
# env file # env file
.env .env
cmd/aeiou cmd/codrcon

View File

@ -7,6 +7,8 @@ import (
"strings" "strings"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/q3rcon/internal/conn" "github.com/onyx-and-iris/q3rcon/internal/conn"
"github.com/onyx-and-iris/q3rcon/internal/packet" "github.com/onyx-and-iris/q3rcon/internal/packet"
) )
@ -47,14 +49,14 @@ func New(host string, port int, password string, options ...Option) (*Rcon, erro
o(r) o(r)
} }
if err = r.Login(); err != nil { if err = r.login(); err != nil {
return nil, err return nil, err
} }
return r, nil return r, nil
} }
func (r Rcon) Login() error { func (r Rcon) login() error {
timeout := time.After(r.loginTimeout) timeout := time.After(r.loginTimeout)
for { for {
select { select {
@ -78,10 +80,13 @@ func (r Rcon) Login() error {
} }
} }
func (r Rcon) Send(cmd string) (string, error) { func (r Rcon) Send(cmdWithArgs string) (string, error) {
cmd, _, _ := strings.Cut(string(cmdWithArgs), " ")
timeout, ok := r.timeouts[cmd] timeout, ok := r.timeouts[cmd]
if !ok { if !ok {
timeout = r.defaultTimeout timeout = r.defaultTimeout
} else {
log.Debugf("%s in timeouts map, using timeout %v", cmd, timeout)
} }
respChan := make(chan string) respChan := make(chan string)
@ -89,7 +94,7 @@ func (r Rcon) Send(cmd string) (string, error) {
go r.listen(timeout, respChan, errChan) go r.listen(timeout, respChan, errChan)
_, err := r.conn.Write(r.request.Encode(cmd)) _, err := r.conn.Write(r.request.Encode(cmdWithArgs))
if err != nil { if err != nil {
return "", err return "", err
} }
@ -98,7 +103,7 @@ func (r Rcon) Send(cmd string) (string, error) {
case err := <-errChan: case err := <-errChan:
return "", err return "", err
case resp := <-respChan: case resp := <-respChan:
return strings.TrimPrefix(resp, string(r.response.Header())), nil return resp, nil
} }
} }