store client coords as a single atomic unit

This commit is contained in:
Reese Norris
2025-05-25 11:57:06 -07:00
parent 2f423ed824
commit c0155e78d4
3 changed files with 17 additions and 8 deletions

View File

@@ -16,7 +16,8 @@ type Client struct {
cancelCtx func() cancelCtx func()
sendChan chan string sendChan chan string
lat, lon, visRange atomic.Float64 coords atomic.Value
visRange atomic.Float64
closestVelocityClientDistance float64 // The closest Velocity-compatible client in meters closestVelocityClientDistance float64 // The closest Velocity-compatible client in meters
flightPlan atomic.String flightPlan atomic.String
@@ -37,6 +38,7 @@ type Client struct {
} }
type LatLon struct { type LatLon struct {
lat, lon float64
} }
func newClient(ctx context.Context, conn net.Conn, scanner *bufio.Scanner, loginData loginData) (client *Client) { 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 { 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})
} }

View File

@@ -110,13 +110,14 @@ func (s *Server) handleGetOnlineUsers(c *gin.Context) {
} }
for _, client := range clientMap { for _, client := range clientMap {
latLon := client.latLon()
genData := OnlineUserGeneralData{ genData := OnlineUserGeneralData{
Callsign: client.callsign, Callsign: client.callsign,
CID: client.cid, CID: client.cid,
Name: client.realName, Name: client.realName,
NetworkRating: int(client.networkRating), NetworkRating: int(client.networkRating),
Latitude: client.lat.Load(), Latitude: latLon[0],
Longitude: client.lon.Load(), Longitude: latLon[1],
LogonTime: client.loginTime, LogonTime: client.loginTime,
LastUpdated: client.lastUpdated.Load(), LastUpdated: client.lastUpdated.Load(),
} }

View File

@@ -68,8 +68,7 @@ func (p *postOffice) updatePosition(client *Client, newCenter [2]float64, newVis
oldMin, oldMax := calculateBoundingBox(client.latLon(), client.visRange.Load()) oldMin, oldMax := calculateBoundingBox(client.latLon(), client.visRange.Load())
newMin, newMax := calculateBoundingBox(newCenter, newVisRange) newMin, newMax := calculateBoundingBox(newCenter, newVisRange)
client.lat.Store(newCenter[0]) client.setLatLon(newCenter[0], newCenter[1])
client.lon.Store(newCenter[1])
client.visRange.Store(newVisRange) client.visRange.Store(newVisRange)
// Avoid redundant updates // Avoid redundant updates
@@ -100,7 +99,9 @@ func (p *postOffice) search(client *Client, callback func(recipient *Client) boo
} }
if foundClient.protoRevision == 101 { 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 { if dist < client.closestVelocityClientDistance {
client.closestVelocityClientDistance = dist client.closestVelocityClientDistance = dist
} }