@@ -93,18 +93,16 @@ func (g *GameServer) isConnClosed(err error) bool {
93
93
return strings .Contains (err .Error (), "use of closed network connection" )
94
94
}
95
95
96
- func (g * GameServer ) lowestBufferHealth (playerNumber int ) (byte , error ) {
96
+ func (g * GameServer ) averageBufferHealth (playerNumber int ) (float32 , error ) {
97
97
defer g .gameData .bufferHealth [playerNumber ].Purge ()
98
98
99
99
if g .gameData .bufferHealth [playerNumber ].Len () > 0 {
100
- bufferHealth := byte ( 255 ) // Start with the maximum possible value
100
+ var total float32
101
101
for _ , k := range g .gameData .bufferHealth [playerNumber ].Keys () {
102
102
value , _ := g .gameData .bufferHealth [playerNumber ].Peek (k )
103
- if value < bufferHealth {
104
- bufferHealth = value
105
- }
103
+ total += float32 (value )
106
104
}
107
- return bufferHealth , nil
105
+ return total / float32 ( g . gameData . bufferHealth [ playerNumber ]. Len ()) , nil
108
106
} else {
109
107
return 0 , fmt .Errorf ("no buffer health data for player %d" , playerNumber )
110
108
}
@@ -118,29 +116,29 @@ func (g *GameServer) ManageBuffer() {
118
116
}
119
117
120
118
// Find the largest buffer health
121
- var bufferHealth byte
119
+ var bufferHealth float32
122
120
var activePlayers bool
123
121
var leadPlayer int
124
122
g .gameDataMutex .Lock () // BufferHealth can be modified by processUDP in a different thread
125
123
for i := range 4 {
126
124
var err error
127
- g .gameData .lowestBufferHealth [i ], err = g .lowestBufferHealth (i )
125
+ g .gameData .averageBufferHealth [i ], err = g .averageBufferHealth (i )
128
126
if err == nil && g .gameData .countLag [i ] == 0 {
129
127
activePlayers = true
130
128
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 ]
133
131
leadPlayer = i + 1
134
132
}
135
133
}
136
134
}
137
135
g .gameDataMutex .Unlock ()
138
136
139
137
if activePlayers {
140
- if bufferHealth > g .BufferTarget && g .gameData .bufferSize > 0 {
138
+ if bufferHealth > float32 ( g .BufferTarget ) + 0.75 && g .gameData .bufferSize > 0 {
141
139
g .gameData .bufferSize --
142
140
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 {
144
142
g .gameData .bufferSize ++
145
143
g .Logger .Info ("increased buffer size" , "bufferHealth" , bufferHealth , "bufferSize" , g .gameData .bufferSize , "leadPlayer" , leadPlayer )
146
144
}
@@ -161,7 +159,7 @@ func (g *GameServer) ManagePlayers() {
161
159
_ , ok := g .registrations [i ]
162
160
if ok {
163
161
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 ])
165
163
playersActive = true
166
164
} else {
167
165
g .Logger .Info ("player disconnected UDP" , "player" , i , "regID" , g .registrations [i ].regID , "address" , g .gameData .playerAddresses [i ])
0 commit comments