mirror of
https://github.com/renorris/openfsd
synced 2026-04-21 02:15:31 +08:00
store client coords as a single atomic unit
This commit is contained in:
@@ -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})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,12 +99,14 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return callback(foundClient)
|
return callback(foundClient)
|
||||||
})
|
})
|
||||||
p.treeLock.RUnlock()
|
p.treeLock.RUnlock()
|
||||||
|
|||||||
Reference in New Issue
Block a user