diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2023-03-08 19:48:50 -0500 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2023-04-17 14:49:47 -0400 |
commit | f6d85eaf1c969bfc10d70d57c43328588d69fde1 (patch) | |
tree | 7acf6a17e317e4ff49c88fbe8e87950466d530ea /pkg/api/api0/client.go | |
parent | 118fbcdd6510a1a449b1aacf451db60aa72dd804 (diff) | |
download | Atlas-f6d85eaf1c969bfc10d70d57c43328588d69fde1.tar.gz Atlas-f6d85eaf1c969bfc10d70d57c43328588d69fde1.zip |
pkg/api/api0, pkg/atlas: Use new nspkt package
Diffstat (limited to 'pkg/api/api0/client.go')
-rw-r--r-- | pkg/api/api0/client.go | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/pkg/api/api0/client.go b/pkg/api/api0/client.go index e7caaf6..6d90bc2 100644 --- a/pkg/api/api0/client.go +++ b/pkg/api/api0/client.go @@ -11,7 +11,6 @@ import ( "strings" "time" - "github.com/r2northstar/atlas/pkg/a2s" "github.com/r2northstar/atlas/pkg/api/api0/api0gameserver" "github.com/r2northstar/atlas/pkg/eax" "github.com/r2northstar/atlas/pkg/origin" @@ -617,61 +616,65 @@ func (h *Handler) handleClientAuthWithServer(w http.ResponseWriter, r *http.Requ h.m().client_authwithserver_gameserverauth_duration_seconds.UpdateDuration(authStart) } else { - obj := map[string]any{ - "type": "connect", - "token": authToken, - "uid": acct.UID, - "username": acct.Username, - "ip": raddr.Addr().String(), - "time": time.Now().Unix(), - } + var attempts int + if rej, err := func() (string, error) { + obj := map[string]any{ + "type": "connect", + "token": authToken, + "uid": acct.UID, + "username": acct.Username, + "ip": raddr.Addr().String(), + "time": time.Now().Unix(), + } - key := connectStateKey{ - ServerID: srv.ID, - Token: authToken, - } + key := connectStateKey{ + ServerID: srv.ID, + Token: authToken, + } - ch := make(chan string, 1) - h.connect.Store(key, &connectState{ - res: ch, - pdata: pbuf, - }) - defer h.connect.Delete(key) + ch := make(chan string, 1) + h.connect.Store(key, &connectState{ + res: ch, + pdata: pbuf, + }) + defer h.connect.Delete(key) - var attempts int - if rej, err := func() (string, error) { - t := time.NewTicker(time.Millisecond * 250) - defer t.Stop() + ctx, cancel := context.WithCancel(ctx) + defer cancel() x := make(chan error, 1) - for { - attempts++ - go func() { - if err := a2s.AtlasSigreq1(srv.AuthAddr(), time.Second, srv.ServerAuthToken, obj); err != nil { - if !errors.Is(err, a2s.ErrTimeout) { - select { - case x <- err: - default: - } - } - } - }() - select { - case <-ctx.Done(): - err := ctx.Err() - if errors.Is(err, context.DeadlineExceeded) { - // if we timed out, it might be because of an error while sending the packet + go func() { + t := time.NewTicker(time.Millisecond * 250) + defer t.Stop() + + for { + attempts++ + if err := h.NSPkt.SendAtlasSigreq1(srv.AuthAddr(), srv.ServerAuthToken, obj); err != nil { select { - case err = <-x: + case x <- err: default: } } - return "", err - case x := <-ch: - return x, nil - case <-t.C: + select { + case <-ctx.Done(): + return + case <-t.C: + } + } + }() + + select { + case <-ctx.Done(): + err := ctx.Err() + select { + case err = <-x: + // error could be due to an issue sending the packet + default: } + return "", err + case x := <-ch: + return x, nil } }(); err != nil { h.m().client_authwithserver_gameserverauthudp_duration_seconds.UpdateDuration(authStart) |