Skip to content

Commit 7621179

Browse files
authored
Use single buffer size (#113)
* Use single buffer size * more * more * more * more
1 parent c1fe08d commit 7621179

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

internal/gameServer/server.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,28 @@ func (g *GameServer) ManageBuffer() {
9494
g.Logger.Info("done managing buffers")
9595
return
9696
}
97-
// Adjust the buffer size for the lead player(s)
97+
98+
// Find the largest buffer health
99+
var bufferHealth int32 = -1
98100
for i := range 4 {
99101
if g.GameData.BufferHealth[i] != -1 && g.GameData.CountLag[i] == 0 {
100-
if g.GameData.BufferHealth[i] > g.BufferTarget && g.GameData.BufferSize[i] > 0 {
101-
g.GameData.BufferSize[i]--
102-
// g.Logger.Info("reducing buffer size", "player", i, "bufferSize", g.GameData.BufferSize[i])
103-
} else if g.GameData.BufferHealth[i] < g.BufferTarget {
104-
g.GameData.BufferSize[i]++
105-
// g.Logger.Info("increasing buffer size", "player", i, "bufferSize", g.GameData.BufferSize[i])
102+
if g.GameData.BufferHealth[i] > bufferHealth {
103+
bufferHealth = g.GameData.BufferHealth[i]
106104
}
107105
}
108106
}
107+
108+
// Adjust the buffer size
109+
if bufferHealth != -1 {
110+
if bufferHealth > g.BufferTarget && g.GameData.BufferSize > 0 {
111+
g.GameData.BufferSize--
112+
g.Logger.Info("reduced buffer size", "bufferSize", g.GameData.BufferSize)
113+
} else if bufferHealth < g.BufferTarget {
114+
g.GameData.BufferSize++
115+
g.Logger.Info("increased buffer size", "bufferSize", g.GameData.BufferSize)
116+
}
117+
}
118+
109119
time.Sleep(time.Second * 5)
110120
}
111121
}
@@ -121,7 +131,7 @@ func (g *GameServer) ManagePlayers() {
121131
_, ok := g.Registrations[i]
122132
if ok {
123133
if g.GameData.PlayerAlive[i] {
124-
g.Logger.Info("player status", "player", i, "regID", g.Registrations[i].RegID, "bufferSize", g.GameData.BufferSize[i], "bufferHealth", g.GameData.BufferHealth[i], "countLag", g.GameData.CountLag[i], "address", g.GameData.PlayerAddresses[i])
134+
g.Logger.Info("player status", "player", i, "regID", g.Registrations[i].RegID, "bufferSize", g.GameData.BufferSize, "bufferHealth", g.GameData.BufferHealth[i], "countLag", g.GameData.CountLag[i], "address", g.GameData.PlayerAddresses[i])
125135
playersActive = true
126136
} else {
127137
g.Logger.Info("play disconnected UDP", "player", i, "regID", g.Registrations[i].RegID, "address", g.GameData.PlayerAddresses[i])
@@ -139,6 +149,7 @@ func (g *GameServer) ManagePlayers() {
139149
g.PlayersMutex.Unlock()
140150
}
141151
}
152+
g.GameData.BufferHealth[i] = -1
142153
}
143154
}
144155
g.GameData.PlayerAlive[i] = false

internal/gameServer/tcp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ func (g *GameServer) processTCP(conn *net.TCPConn) {
340340
g.GameDataMutex.Lock() // any player can modify this, which would be in a different thread
341341
g.GameData.PlayerAlive[i] = false
342342
g.GameData.Status |= (0x1 << (i + 1))
343-
g.GameDataMutex.Unlock()
344343

345344
g.RegistrationsMutex.Lock() // any player can modify this, which would be in a different thread
346345
delete(g.Registrations, i)
@@ -354,6 +353,8 @@ func (g *GameServer) processTCP(conn *net.TCPConn) {
354353
g.PlayersMutex.Unlock()
355354
}
356355
}
356+
g.GameData.BufferHealth[i] = -1
357+
g.GameDataMutex.Unlock()
357358
}
358359
}
359360
}

internal/gameServer/udp.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type InputData struct {
2121
type GameData struct {
2222
SyncValues map[uint32][]byte
2323
PlayerAddresses []*net.UDPAddr
24-
BufferSize []uint32
24+
BufferSize uint32
2525
BufferHealth []int32
2626
Inputs []*lru.Cache[uint32, InputData]
2727
PendingInput []uint32
@@ -86,7 +86,6 @@ func (g *GameServer) sendUDPInput(count uint32, addr *net.UDPAddr, playerNumber
8686
countLag = g.GameData.LeadCount - count
8787
}
8888
if sendingPlayerNumber == NoRegID { // if the incoming packet was KeyInfoClient, the regID isn't included in the packet
89-
sendingPlayerNumber = playerNumber
9089
buffer[0] = KeyInfoServerGratuitous // client will ignore countLag value in this case
9190
} else {
9291
buffer[0] = KeyInfoServer
@@ -96,7 +95,7 @@ func (g *GameServer) sendUDPInput(count uint32, addr *net.UDPAddr, playerNumber
9695
buffer[3] = uint8(countLag)
9796
currentByte := 5
9897
start := count
99-
end := start + g.GameData.BufferSize[sendingPlayerNumber]
98+
end := start + g.GameData.BufferSize
10099
_, ok := g.GameData.Inputs[playerNumber].Get(count) // check if input exists for this count
101100
for (currentByte < len(buffer)-9) && ((!spectator && countLag == 0 && uintLarger(end, count)) || ok) {
102101
binary.BigEndian.PutUint32(buffer[currentByte:], count)
@@ -213,7 +212,7 @@ func (g *GameServer) createUDPServer() error {
213212
g.Logger.Info("Created UDP server", "port", g.Port)
214213

215214
g.GameData.PlayerAddresses = make([]*net.UDPAddr, 4)
216-
g.GameData.BufferSize = []uint32{3, 3, 3, 3}
215+
g.GameData.BufferSize = 3
217216
g.GameData.BufferHealth = []int32{-1, -1, -1, -1}
218217
g.GameData.Inputs = make([]*lru.Cache[uint32, InputData], 4)
219218
for i := range 4 {

0 commit comments

Comments
 (0)