88 "net"
99 "time"
1010
11+ lru "github.com/hashicorp/golang-lru/v2"
1112 "golang.org/x/net/ipv4"
1213 "golang.org/x/net/ipv6"
1314)
@@ -17,8 +18,8 @@ type GameData struct {
1718 PlayerAddresses []* net.UDPAddr
1819 BufferSize []uint32
1920 BufferHealth []int32
20- Inputs []map [uint32 ] uint32
21- Plugin []map [uint32 ] byte
21+ Inputs []* lru. Cache [uint32 , uint32 ]
22+ Plugin []* lru. Cache [uint32 , byte ]
2223 PendingInput []uint32
2324 CountLag []uint32
2425 PendingPlugin []byte
@@ -36,11 +37,11 @@ const (
3637)
3738
3839const (
39- StatusDesync = 1
40- DisconnectTimeoutS = 30
41- NoRegID = 255
42- InputDataMax uint32 = 5000
43- CS4 = 32
40+ StatusDesync = 1
41+ DisconnectTimeoutS = 30
42+ NoRegID = 255
43+ InputDataMax int = 5000
44+ CS4 = 32
4445)
4546
4647// returns true if v is bigger than w (accounting for uint32 wrap around).
@@ -62,12 +63,10 @@ func (g *GameServer) getPlayerNumberByID(regID uint32) (byte, error) {
6263}
6364
6465func (g * GameServer ) fillInput (playerNumber byte , count uint32 ) {
65- _ , inputExists := g .GameData .Inputs [playerNumber ][count ]
66- delete (g .GameData .Inputs [playerNumber ], count - InputDataMax ) // no need to keep old input data
67- delete (g .GameData .Plugin [playerNumber ], count - InputDataMax ) // no need to keep old input data
66+ _ , inputExists := g .GameData .Inputs [playerNumber ].Get (count )
6867 if ! inputExists {
69- g .GameData .Inputs [playerNumber ][ count ] = g .GameData .PendingInput [playerNumber ]
70- g .GameData .Plugin [playerNumber ][ count ] = g .GameData .PendingPlugin [playerNumber ]
68+ g .GameData .Inputs [playerNumber ]. Add ( count , g .GameData .PendingInput [playerNumber ])
69+ g .GameData .Plugin [playerNumber ]. Add ( count , g .GameData .PendingPlugin [playerNumber ])
7170 }
7271}
7372
@@ -93,17 +92,17 @@ func (g *GameServer) sendUDPInput(count uint32, addr *net.UDPAddr, playerNumber
9392 currentByte := 5
9493 start := count
9594 end := start + g .GameData .BufferSize [sendingPlayerNumber ]
96- _ , ok := g .GameData .Inputs [playerNumber ][ count ] // check if input exists for this count
95+ inputData , ok := g .GameData .Inputs [playerNumber ]. Get ( count ) // check if input exists for this count
9796 for (currentByte < len (buffer )- 9 ) && ((! spectator && countLag == 0 && uintLarger (end , count )) || ok ) {
9897 binary .BigEndian .PutUint32 (buffer [currentByte :], count )
9998 currentByte += 4
10099 g .fillInput (playerNumber , count )
101- binary .BigEndian .PutUint32 (buffer [currentByte :], g . GameData . Inputs [ playerNumber ][ count ] )
100+ binary .BigEndian .PutUint32 (buffer [currentByte :], inputData )
102101 currentByte += 4
103- buffer [currentByte ] = g .GameData .Plugin [playerNumber ][ count ]
102+ buffer [currentByte ], _ = g .GameData .Plugin [playerNumber ]. Get ( count )
104103 currentByte ++
105104 count ++
106- _ , ok = g .GameData .Inputs [playerNumber ][ count ] // check if input exists for this count
105+ inputData , ok = g .GameData .Inputs [playerNumber ]. Get ( count ) // check if input exists for this count
107106 }
108107
109108 if count > start {
@@ -210,13 +209,13 @@ func (g *GameServer) createUDPServer() error {
210209 g .GameData .PlayerAddresses = make ([]* net.UDPAddr , 4 ) //nolint:gomnd,mnd
211210 g .GameData .BufferSize = []uint32 {3 , 3 , 3 , 3 }
212211 g .GameData .BufferHealth = []int32 {- 1 , - 1 , - 1 , - 1 }
213- g .GameData .Inputs = make ([]map [uint32 ] uint32 , 4 ) //nolint:gomnd,mnd
212+ g .GameData .Inputs = make ([]* lru. Cache [uint32 , uint32 ] , 4 ) //nolint:gomnd,mnd
214213 for i := range 4 {
215- g .GameData .Inputs [i ] = make ( map [uint32 ] uint32 )
214+ g .GameData .Inputs [i ], _ = lru. New [uint32 , uint32 ]( InputDataMax )
216215 }
217- g .GameData .Plugin = make ([]map [uint32 ] byte , 4 ) //nolint:gomnd,mnd
216+ g .GameData .Plugin = make ([]* lru. Cache [uint32 , byte ] , 4 ) //nolint:gomnd,mnd
218217 for i := range 4 {
219- g .GameData .Plugin [i ] = make ( map [uint32 ] byte )
218+ g .GameData .Plugin [i ], _ = lru. New [uint32 , byte ]( InputDataMax )
220219 }
221220 g .GameData .PendingInput = make ([]uint32 , 4 ) //nolint:gomnd,mnd
222221 g .GameData .PendingPlugin = make ([]byte , 4 ) //nolint:gomnd,mnd
0 commit comments