Skip to content

Commit 80e37aa

Browse files
authored
use slice for bufferHealth (#146)
1 parent 26788c5 commit 80e37aa

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

internal/gameServer/server.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,14 @@ func (g *GameServer) isConnClosed(err error) bool {
9494
}
9595

9696
func (g *GameServer) averageBufferHealth(playerNumber int) (float32, error) {
97-
defer g.gameData.bufferHealth[playerNumber].Purge()
97+
defer func() { g.gameData.bufferHealth[playerNumber] = g.gameData.bufferHealth[playerNumber][:0] }()
9898

99-
if g.gameData.bufferHealth[playerNumber].Len() > 0 {
99+
if len(g.gameData.bufferHealth[playerNumber]) > 0 {
100100
var total float32
101-
for _, k := range g.gameData.bufferHealth[playerNumber].Keys() {
102-
value, _ := g.gameData.bufferHealth[playerNumber].Peek(k)
101+
for _, value := range g.gameData.bufferHealth[playerNumber] {
103102
total += float32(value)
104103
}
105-
return total / float32(g.gameData.bufferHealth[playerNumber].Len()), nil
104+
return total / float32(len(g.gameData.bufferHealth[playerNumber])), nil
106105
} else {
107106
return 0, fmt.Errorf("no buffer health data for player %d", playerNumber)
108107
}
@@ -174,7 +173,7 @@ func (g *GameServer) ManagePlayers() {
174173
g.PlayersMutex.Unlock()
175174
}
176175
}
177-
g.gameData.bufferHealth[i].Purge()
176+
g.gameData.bufferHealth[i] = g.gameData.bufferHealth[i][:0]
178177
}
179178
}
180179
g.gameData.playerAlive[i] = false

internal/gameServer/tcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (g *GameServer) processTCP(conn *net.TCPConn) {
353353
g.PlayersMutex.Unlock()
354354
}
355355
}
356-
g.gameData.bufferHealth[i].Purge()
356+
g.gameData.bufferHealth[i] = g.gameData.bufferHealth[i][:0]
357357
g.gameDataMutex.Unlock()
358358
}
359359
}

internal/gameServer/udp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type GameData struct {
2323
playerAddresses [4]*net.UDPAddr
2424
bufferSize uint32
2525
averageBufferHealth [4]float32
26-
bufferHealth [4]*lru.Cache[uint32, byte]
26+
bufferHealth [4][]byte
2727
inputs [4]*lru.Cache[uint32, InputData]
2828
pendingInput [4]InputData
2929
countLag [4]uint32
@@ -151,7 +151,7 @@ func (g *GameServer) processUDP(addr *net.UDPAddr) {
151151
}
152152
g.gameData.countLag[sendingPlayerNumber] = g.sendUDPInput(count, addr, playerNumber, spectator != 0, sendingPlayerNumber)
153153
g.gameDataMutex.Lock() // PlayerAlive and BufferHealth can be modified in different threads
154-
g.gameData.bufferHealth[sendingPlayerNumber].Add(count, g.gameData.recvBuffer[11])
154+
g.gameData.bufferHealth[sendingPlayerNumber] = append(g.gameData.bufferHealth[sendingPlayerNumber], g.gameData.recvBuffer[11])
155155
g.gameData.playerAlive[sendingPlayerNumber] = true
156156
g.gameDataMutex.Unlock()
157157
case CP0Info:
@@ -213,7 +213,7 @@ func (g *GameServer) createUDPServer() error {
213213
g.gameData.bufferSize = 3
214214
for i := range 4 {
215215
g.gameData.inputs[i], _ = lru.New[uint32, InputData](InputDataMax)
216-
g.gameData.bufferHealth[i], _ = lru.New[uint32, byte](InputDataMax)
216+
g.gameData.bufferHealth[i] = make([]byte, 0, InputDataMax)
217217
}
218218
g.gameData.syncValues, _ = lru.New[uint32, []byte](100) // Store up to 100 sync values
219219
g.gameData.sendBuffer = make([]byte, 508)

0 commit comments

Comments
 (0)