Skip to content

Commit e6461da

Browse files
committed
use average
1 parent c1523c1 commit e6461da

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

internal/gameServer/server.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,16 @@ func (g *GameServer) isConnClosed(err error) bool {
9393
return strings.Contains(err.Error(), "use of closed network connection")
9494
}
9595

96-
func (g *GameServer) lowestBufferHealth(playerNumber int) (byte, error) {
96+
func (g *GameServer) averageBufferHealth(playerNumber int) (float32, error) {
9797
defer g.gameData.bufferHealth[playerNumber].Purge()
9898

9999
if g.gameData.bufferHealth[playerNumber].Len() > 0 {
100-
bufferHealth := byte(255) // Start with the maximum possible value
100+
var total float32
101101
for _, k := range g.gameData.bufferHealth[playerNumber].Keys() {
102102
value, _ := g.gameData.bufferHealth[playerNumber].Peek(k)
103-
if value < bufferHealth {
104-
bufferHealth = value
105-
}
103+
total += float32(value)
106104
}
107-
return bufferHealth, nil
105+
return total / float32(g.gameData.bufferHealth[playerNumber].Len()), nil
108106
} else {
109107
return 0, fmt.Errorf("no buffer health data for player %d", playerNumber)
110108
}
@@ -118,29 +116,29 @@ func (g *GameServer) ManageBuffer() {
118116
}
119117

120118
// Find the largest buffer health
121-
var bufferHealth byte
119+
var bufferHealth float32
122120
var activePlayers bool
123121
var leadPlayer int
124122
g.gameDataMutex.Lock() // BufferHealth can be modified by processUDP in a different thread
125123
for i := range 4 {
126124
var err error
127-
g.gameData.lowestBufferHealth[i], err = g.lowestBufferHealth(i)
125+
g.gameData.averageBufferHealth[i], err = g.averageBufferHealth(i)
128126
if err == nil && g.gameData.countLag[i] == 0 {
129127
activePlayers = true
130128

131-
if g.gameData.lowestBufferHealth[i] > bufferHealth {
132-
bufferHealth = g.gameData.lowestBufferHealth[i]
129+
if g.gameData.averageBufferHealth[i] > bufferHealth {
130+
bufferHealth = g.gameData.averageBufferHealth[i]
133131
leadPlayer = i + 1
134132
}
135133
}
136134
}
137135
g.gameDataMutex.Unlock()
138136

139137
if activePlayers {
140-
if bufferHealth > g.BufferTarget && g.gameData.bufferSize > 0 {
138+
if bufferHealth > float32(g.BufferTarget)+0.75 && g.gameData.bufferSize > 0 {
141139
g.gameData.bufferSize--
142140
g.Logger.Info("reduced buffer size", "bufferHealth", bufferHealth, "bufferSize", g.gameData.bufferSize, "leadPlayer", leadPlayer)
143-
} else if bufferHealth < g.BufferTarget {
141+
} else if bufferHealth < float32(g.BufferTarget)-0.75 {
144142
g.gameData.bufferSize++
145143
g.Logger.Info("increased buffer size", "bufferHealth", bufferHealth, "bufferSize", g.gameData.bufferSize, "leadPlayer", leadPlayer)
146144
}
@@ -161,7 +159,7 @@ func (g *GameServer) ManagePlayers() {
161159
_, ok := g.registrations[i]
162160
if ok {
163161
if g.gameData.playerAlive[i] {
164-
g.Logger.Info("player status", "player", i, "regID", g.registrations[i].regID, "bufferHealth", g.gameData.lowestBufferHealth[i], "bufferSize", g.gameData.bufferSize, "countLag", g.gameData.countLag[i], "address", g.gameData.playerAddresses[i])
162+
g.Logger.Info("player status", "player", i, "regID", g.registrations[i].regID, "bufferHealth", g.gameData.averageBufferHealth[i], "bufferSize", g.gameData.bufferSize, "countLag", g.gameData.countLag[i], "address", g.gameData.playerAddresses[i])
165163
playersActive = true
166164
} else {
167165
g.Logger.Info("player disconnected UDP", "player", i, "regID", g.registrations[i].regID, "address", g.gameData.playerAddresses[i])

internal/gameServer/udp.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ type InputData struct {
1919
}
2020

2121
type GameData struct {
22-
syncValues *lru.Cache[uint32, []byte]
23-
playerAddresses [4]*net.UDPAddr
24-
bufferSize uint32
25-
lowestBufferHealth [4]byte
26-
bufferHealth [4]*lru.Cache[uint32, byte]
27-
inputs [4]*lru.Cache[uint32, InputData]
28-
pendingInput [4]InputData
29-
countLag [4]uint32
30-
sendBuffer []byte
31-
recvBuffer []byte
32-
playerAlive [4]bool
33-
leadCount uint32
34-
status byte
22+
syncValues *lru.Cache[uint32, []byte]
23+
playerAddresses [4]*net.UDPAddr
24+
bufferSize uint32
25+
averageBufferHealth [4]float32
26+
bufferHealth [4]*lru.Cache[uint32, byte]
27+
inputs [4]*lru.Cache[uint32, InputData]
28+
pendingInput [4]InputData
29+
countLag [4]uint32
30+
sendBuffer []byte
31+
recvBuffer []byte
32+
playerAlive [4]bool
33+
leadCount uint32
34+
status byte
3535
}
3636

3737
const (

0 commit comments

Comments
 (0)