Skip to content

Commit c1523c1

Browse files
committed
use lowest buffer health
1 parent 415ba40 commit c1523c1

File tree

4 files changed

+268
-266
lines changed

4 files changed

+268
-266
lines changed

internal/gameServer/server.go

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ type Client struct {
1919
}
2020

2121
type Registration struct {
22-
RegID uint32
23-
Plugin byte
24-
Raw byte
22+
regID uint32
23+
plugin byte
24+
raw byte
2525
}
2626

2727
type GameServer struct {
2828
StartTime time.Time
2929
Players map[string]Client
3030
PlayersMutex sync.Mutex
31-
TCPListener *net.TCPListener
32-
UDPListener *net.UDPConn
33-
Registrations map[byte]*Registration
34-
RegistrationsMutex sync.Mutex
35-
TCPMutex sync.Mutex
36-
TCPFiles map[string][]byte
37-
CustomData map[byte][]byte
31+
tcpListener *net.TCPListener
32+
udpListener *net.UDPConn
33+
registrations map[byte]*Registration
34+
registrationsMutex sync.Mutex
35+
tcpMutex sync.Mutex
36+
tcpFiles map[string][]byte
37+
customData map[byte][]byte
3838
Logger logr.Logger
3939
GameName string
4040
Password string
4141
ClientSha string
4242
MD5 string
4343
Emulator string
44-
TCPSettings []byte
45-
GameData GameData
46-
GameDataMutex sync.Mutex
44+
tcpSettings []byte
45+
gameData GameData
46+
gameDataMutex sync.Mutex
4747
Port int
48-
HasSettings bool
48+
hasSettings bool
4949
Running bool
5050
Features map[string]string
5151
NeedsUpdatePlayers bool
5252
NumberOfPlayers int
53-
BufferTarget uint32
53+
BufferTarget byte
5454
QuitChannel *chan bool
5555
}
5656

@@ -62,7 +62,7 @@ func (g *GameServer) CreateNetworkServers(basePort int, maxGames int, roomName s
6262
}
6363
if err := g.createUDPServer(); err != nil {
6464
g.Logger.Error(err, "error creating UDP server")
65-
if err := g.TCPListener.Close(); err != nil && !g.isConnClosed(err) {
65+
if err := g.tcpListener.Close(); err != nil && !g.isConnClosed(err) {
6666
g.Logger.Error(err, "error closing TcpListener")
6767
}
6868
return 0
@@ -71,12 +71,12 @@ func (g *GameServer) CreateNetworkServers(basePort int, maxGames int, roomName s
7171
}
7272

7373
func (g *GameServer) CloseServers() {
74-
if err := g.UDPListener.Close(); err != nil && !g.isConnClosed(err) {
74+
if err := g.udpListener.Close(); err != nil && !g.isConnClosed(err) {
7575
g.Logger.Error(err, "error closing UdpListener")
7676
} else if err == nil {
7777
g.Logger.Info("UDP server closed")
7878
}
79-
if err := g.TCPListener.Close(); err != nil && !g.isConnClosed(err) {
79+
if err := g.tcpListener.Close(); err != nil && !g.isConnClosed(err) {
8080
g.Logger.Error(err, "error closing TcpListener")
8181
} else if err == nil {
8282
g.Logger.Info("TCP server closed")
@@ -93,16 +93,18 @@ func (g *GameServer) isConnClosed(err error) bool {
9393
return strings.Contains(err.Error(), "use of closed network connection")
9494
}
9595

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

99-
if g.GameData.BufferHealth[playerNumber].Len() > 0 {
100-
var bufferHealth float32
101-
for _, k := range g.GameData.BufferHealth[playerNumber].Keys() {
102-
value, _ := g.GameData.BufferHealth[playerNumber].Peek(k)
103-
bufferHealth += float32(value)
99+
if g.gameData.bufferHealth[playerNumber].Len() > 0 {
100+
bufferHealth := byte(255) // Start with the maximum possible value
101+
for _, k := range g.gameData.bufferHealth[playerNumber].Keys() {
102+
value, _ := g.gameData.bufferHealth[playerNumber].Peek(k)
103+
if value < bufferHealth {
104+
bufferHealth = value
105+
}
104106
}
105-
return bufferHealth / float32(g.GameData.BufferHealth[playerNumber].Len()), nil
107+
return bufferHealth, nil
106108
} else {
107109
return 0, fmt.Errorf("no buffer health data for player %d", playerNumber)
108110
}
@@ -116,31 +118,31 @@ func (g *GameServer) ManageBuffer() {
116118
}
117119

118120
// Find the largest buffer health
119-
var bufferHealth float32
121+
var bufferHealth byte
120122
var activePlayers bool
121123
var leadPlayer int
122-
g.GameDataMutex.Lock() // BufferHealth can be modified by processUDP in a different thread
124+
g.gameDataMutex.Lock() // BufferHealth can be modified by processUDP in a different thread
123125
for i := range 4 {
124126
var err error
125-
g.GameData.BufferHealthAverage[i], err = g.bufferHealthAverage(i)
126-
if err == nil && g.GameData.CountLag[i] == 0 {
127+
g.gameData.lowestBufferHealth[i], err = g.lowestBufferHealth(i)
128+
if err == nil && g.gameData.countLag[i] == 0 {
127129
activePlayers = true
128130

129-
if g.GameData.BufferHealthAverage[i] > bufferHealth {
130-
bufferHealth = g.GameData.BufferHealthAverage[i]
131+
if g.gameData.lowestBufferHealth[i] > bufferHealth {
132+
bufferHealth = g.gameData.lowestBufferHealth[i]
131133
leadPlayer = i + 1
132134
}
133135
}
134136
}
135-
g.GameDataMutex.Unlock()
137+
g.gameDataMutex.Unlock()
136138

137139
if activePlayers {
138-
if bufferHealth > float32(g.BufferTarget)+0.5 && g.GameData.BufferSize > 0 {
139-
g.GameData.BufferSize--
140-
g.Logger.Info("reduced buffer size", "bufferHealth", bufferHealth, "bufferSize", g.GameData.BufferSize, "leadPlayer", leadPlayer)
141-
} else if bufferHealth < float32(g.BufferTarget)-0.5 {
142-
g.GameData.BufferSize++
143-
g.Logger.Info("increased buffer size", "bufferHealth", bufferHealth, "bufferSize", g.GameData.BufferSize, "leadPlayer", leadPlayer)
140+
if bufferHealth > g.BufferTarget && g.gameData.bufferSize > 0 {
141+
g.gameData.bufferSize--
142+
g.Logger.Info("reduced buffer size", "bufferHealth", bufferHealth, "bufferSize", g.gameData.bufferSize, "leadPlayer", leadPlayer)
143+
} else if bufferHealth < g.BufferTarget {
144+
g.gameData.bufferSize++
145+
g.Logger.Info("increased buffer size", "bufferHealth", bufferHealth, "bufferSize", g.gameData.bufferSize, "leadPlayer", leadPlayer)
144146
}
145147
}
146148

@@ -154,20 +156,20 @@ func (g *GameServer) ManagePlayers() {
154156
playersActive := false // used to check if anyone is still around
155157
var i byte
156158

157-
g.GameDataMutex.Lock() // PlayerAlive and Status can be modified by processUDP in a different thread
159+
g.gameDataMutex.Lock() // PlayerAlive and Status can be modified by processUDP in a different thread
158160
for i = range 4 {
159-
_, ok := g.Registrations[i]
161+
_, ok := g.registrations[i]
160162
if ok {
161-
if g.GameData.PlayerAlive[i] {
162-
g.Logger.Info("player status", "player", i, "regID", g.Registrations[i].RegID, "bufferHealthAverage", g.GameData.BufferHealthAverage[i], "bufferSize", g.GameData.BufferSize, "countLag", g.GameData.CountLag[i], "address", g.GameData.PlayerAddresses[i])
163+
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])
163165
playersActive = true
164166
} else {
165-
g.Logger.Info("player disconnected UDP", "player", i, "regID", g.Registrations[i].RegID, "address", g.GameData.PlayerAddresses[i])
166-
g.GameData.Status |= (0x1 << (i + 1))
167+
g.Logger.Info("player disconnected UDP", "player", i, "regID", g.registrations[i].regID, "address", g.gameData.playerAddresses[i])
168+
g.gameData.status |= (0x1 << (i + 1))
167169

168-
g.RegistrationsMutex.Lock() // Registrations can be modified by processTCP
169-
delete(g.Registrations, i)
170-
g.RegistrationsMutex.Unlock()
170+
g.registrationsMutex.Lock() // Registrations can be modified by processTCP
171+
delete(g.registrations, i)
172+
g.registrationsMutex.Unlock()
171173

172174
for k, v := range g.Players {
173175
if v.Number == int(i) {
@@ -177,12 +179,12 @@ func (g *GameServer) ManagePlayers() {
177179
g.PlayersMutex.Unlock()
178180
}
179181
}
180-
g.GameData.BufferHealth[i].Purge()
182+
g.gameData.bufferHealth[i].Purge()
181183
}
182184
}
183-
g.GameData.PlayerAlive[i] = false
185+
g.gameData.playerAlive[i] = false
184186
}
185-
g.GameDataMutex.Unlock()
187+
g.gameDataMutex.Unlock()
186188

187189
if !playersActive {
188190
g.Logger.Info("no more players, closing room", "numPlayers", g.NumberOfPlayers, "clientSHA", g.ClientSha, "playTime", time.Since(g.StartTime).String())

0 commit comments

Comments
 (0)