From c0155e78d4e8947d83d62a98731b6be7c62bef40 Mon Sep 17 00:00:00 2001 From: Reese Norris Date: Sun, 25 May 2025 11:57:06 -0700 Subject: [PATCH] store client coords as a single atomic unit --- fsd/client.go | 11 +++++++++-- fsd/http_service.go | 5 +++-- fsd/postoffice.go | 9 +++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/fsd/client.go b/fsd/client.go index 4fd8270..1fa91e1 100644 --- a/fsd/client.go +++ b/fsd/client.go @@ -16,7 +16,8 @@ type Client struct { cancelCtx func() sendChan chan string - lat, lon, visRange atomic.Float64 + coords atomic.Value + visRange atomic.Float64 closestVelocityClientDistance float64 // The closest Velocity-compatible client in meters flightPlan atomic.String @@ -37,6 +38,7 @@ type Client struct { } type LatLon struct { + lat, lon float64 } func newClient(ctx context.Context, conn net.Conn, scanner *bufio.Scanner, loginData loginData) (client *Client) { @@ -125,5 +127,10 @@ func (s *Server) eventLoop(client *Client) { } func (c *Client) latLon() [2]float64 { - return [2]float64{c.lat.Load(), c.lon.Load()} + latLon := c.coords.Load().(LatLon) + return [2]float64{latLon.lat, latLon.lon} +} + +func (c *Client) setLatLon(lat, lon float64) { + c.coords.Store(LatLon{lat: lat, lon: lon}) } diff --git a/fsd/http_service.go b/fsd/http_service.go index ff94976..bdef9fa 100644 --- a/fsd/http_service.go +++ b/fsd/http_service.go @@ -110,13 +110,14 @@ func (s *Server) handleGetOnlineUsers(c *gin.Context) { } for _, client := range clientMap { + latLon := client.latLon() genData := OnlineUserGeneralData{ Callsign: client.callsign, CID: client.cid, Name: client.realName, NetworkRating: int(client.networkRating), - Latitude: client.lat.Load(), - Longitude: client.lon.Load(), + Latitude: latLon[0], + Longitude: latLon[1], LogonTime: client.loginTime, LastUpdated: client.lastUpdated.Load(), } diff --git a/fsd/postoffice.go b/fsd/postoffice.go index 0438297..5b4d6c7 100644 --- a/fsd/postoffice.go +++ b/fsd/postoffice.go @@ -68,8 +68,7 @@ func (p *postOffice) updatePosition(client *Client, newCenter [2]float64, newVis oldMin, oldMax := calculateBoundingBox(client.latLon(), client.visRange.Load()) newMin, newMax := calculateBoundingBox(newCenter, newVisRange) - client.lat.Store(newCenter[0]) - client.lon.Store(newCenter[1]) + client.setLatLon(newCenter[0], newCenter[1]) client.visRange.Store(newVisRange) // Avoid redundant updates @@ -100,12 +99,14 @@ func (p *postOffice) search(client *Client, callback func(recipient *Client) boo } if foundClient.protoRevision == 101 { - dist := distance(client.lat.Load(), client.lon.Load(), foundClient.lat.Load(), foundClient.lon.Load()) + clientLatLon := client.latLon() + foundClientLatLon := foundClient.latLon() + dist := distance(clientLatLon[0], clientLatLon[1], foundClientLatLon[0], foundClientLatLon[1]) if dist < client.closestVelocityClientDistance { client.closestVelocityClientDistance = dist } } - + return callback(foundClient) }) p.treeLock.RUnlock()