@@ -19,38 +19,38 @@ type Client struct {
19
19
}
20
20
21
21
type Registration struct {
22
- RegID uint32
23
- Plugin byte
24
- Raw byte
22
+ regID uint32
23
+ plugin byte
24
+ raw byte
25
25
}
26
26
27
27
type GameServer struct {
28
28
StartTime time.Time
29
29
Players map [string ]Client
30
30
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
38
38
Logger logr.Logger
39
39
GameName string
40
40
Password string
41
41
ClientSha string
42
42
MD5 string
43
43
Emulator string
44
- TCPSettings []byte
45
- GameData GameData
46
- GameDataMutex sync.Mutex
44
+ tcpSettings []byte
45
+ gameData GameData
46
+ gameDataMutex sync.Mutex
47
47
Port int
48
- HasSettings bool
48
+ hasSettings bool
49
49
Running bool
50
50
Features map [string ]string
51
51
NeedsUpdatePlayers bool
52
52
NumberOfPlayers int
53
- BufferTarget uint32
53
+ BufferTarget byte
54
54
QuitChannel * chan bool
55
55
}
56
56
@@ -62,7 +62,7 @@ func (g *GameServer) CreateNetworkServers(basePort int, maxGames int, roomName s
62
62
}
63
63
if err := g .createUDPServer (); err != nil {
64
64
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 ) {
66
66
g .Logger .Error (err , "error closing TcpListener" )
67
67
}
68
68
return 0
@@ -71,12 +71,12 @@ func (g *GameServer) CreateNetworkServers(basePort int, maxGames int, roomName s
71
71
}
72
72
73
73
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 ) {
75
75
g .Logger .Error (err , "error closing UdpListener" )
76
76
} else if err == nil {
77
77
g .Logger .Info ("UDP server closed" )
78
78
}
79
- if err := g .TCPListener .Close (); err != nil && ! g .isConnClosed (err ) {
79
+ if err := g .tcpListener .Close (); err != nil && ! g .isConnClosed (err ) {
80
80
g .Logger .Error (err , "error closing TcpListener" )
81
81
} else if err == nil {
82
82
g .Logger .Info ("TCP server closed" )
@@ -93,16 +93,18 @@ 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 ) 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 ()
98
98
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
+ }
104
106
}
105
- return bufferHealth / float32 ( g . GameData . BufferHealth [ playerNumber ]. Len ()) , nil
107
+ return bufferHealth , nil
106
108
} else {
107
109
return 0 , fmt .Errorf ("no buffer health data for player %d" , playerNumber )
108
110
}
@@ -116,31 +118,31 @@ func (g *GameServer) ManageBuffer() {
116
118
}
117
119
118
120
// Find the largest buffer health
119
- var bufferHealth float32
121
+ var bufferHealth byte
120
122
var activePlayers bool
121
123
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
123
125
for i := range 4 {
124
126
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 {
127
129
activePlayers = true
128
130
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 ]
131
133
leadPlayer = i + 1
132
134
}
133
135
}
134
136
}
135
- g .GameDataMutex .Unlock ()
137
+ g .gameDataMutex .Unlock ()
136
138
137
139
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 )
144
146
}
145
147
}
146
148
@@ -154,20 +156,20 @@ func (g *GameServer) ManagePlayers() {
154
156
playersActive := false // used to check if anyone is still around
155
157
var i byte
156
158
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
158
160
for i = range 4 {
159
- _ , ok := g .Registrations [i ]
161
+ _ , ok := g .registrations [i ]
160
162
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 ])
163
165
playersActive = true
164
166
} 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 ))
167
169
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 ()
171
173
172
174
for k , v := range g .Players {
173
175
if v .Number == int (i ) {
@@ -177,12 +179,12 @@ func (g *GameServer) ManagePlayers() {
177
179
g .PlayersMutex .Unlock ()
178
180
}
179
181
}
180
- g .GameData . BufferHealth [i ].Purge ()
182
+ g .gameData . bufferHealth [i ].Purge ()
181
183
}
182
184
}
183
- g .GameData . PlayerAlive [i ] = false
185
+ g .gameData . playerAlive [i ] = false
184
186
}
185
- g .GameDataMutex .Unlock ()
187
+ g .gameDataMutex .Unlock ()
186
188
187
189
if ! playersActive {
188
190
g .Logger .Info ("no more players, closing room" , "numPlayers" , g .NumberOfPlayers , "clientSHA" , g .ClientSha , "playTime" , time .Since (g .StartTime ).String ())
0 commit comments