Skip to content

Commit cd911ff

Browse files
committed
[bugfix] Decouple team/autoRef connection state from 'timeChanged'
1 parent a045c6d commit cd911ff

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

internal/app/controller/controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ func NewGameController() (c *GameController) {
4949
c.AutoRefServer = rcon.NewAutoRefServer()
5050
c.AutoRefServer.LoadTrustedKeys(c.Config.Server.AutoRef.TrustedKeysDir)
5151
c.AutoRefServer.ProcessRequest = c.ProcessAutoRefRequests
52+
c.AutoRefServer.ClientsChangedObservers = []func(){c.updateOnlineStates}
5253

5354
c.TeamServer = rcon.NewTeamServer()
5455
c.TeamServer.LoadTrustedKeys(c.Config.Server.Team.TrustedKeysDir)
5556
c.TeamServer.ProcessTeamRequest = c.ProcessTeamRequests
57+
c.TeamServer.ClientsChangedObservers = []func(){c.updateOnlineStates}
5658

5759
c.CiServer = rcon.NewCiServer()
5860

@@ -84,6 +86,7 @@ func (c *GameController) Run() {
8486
c.ApiServer.PublishUiProtocol(c.Engine.PersistentState.Protocol)
8587
c.TeamServer.AllowedTeamNames = []string{c.Engine.State.TeamState[TeamYellow].Name,
8688
c.Engine.State.TeamState[TeamBlue].Name}
89+
c.updateOnlineStates()
8790

8891
go c.AutoRefServer.Listen(c.Config.Server.AutoRef.Address)
8992
go c.AutoRefServer.ListenTls(c.Config.Server.AutoRef.AddressTls)
@@ -150,7 +153,6 @@ func (c *GameController) publish() {
150153
c.PublishMutex.Lock()
151154
defer c.PublishMutex.Unlock()
152155

153-
c.updateOnlineStates()
154156
c.statePreserver.Save(c.Engine.PersistentState)
155157

156158
c.TeamServer.AllowedTeamNames = []string{
@@ -184,6 +186,8 @@ func (c *GameController) updateOnlineStates() {
184186
}
185187
sort.Strings(autoRefs)
186188
c.Engine.GcState.AutoRefsConnected = autoRefs
189+
190+
c.publish()
187191
}
188192

189193
// publishToNetwork publishes the current state to the network (multicast) every 25ms

internal/app/rcon/autoRefServer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func (s *AutoRefServer) handleClientConnection(conn net.Conn) {
117117
s.Clients[client.Id] = client.Client
118118
defer s.CloseConnection(conn, client.Id)
119119
log.Printf("Client %v connected", client.Id)
120+
for _, observer := range s.ClientsChangedObservers {
121+
observer()
122+
}
120123

121124
for {
122125
req := refproto.AutoRefToController{}

internal/app/rcon/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717
)
1818

1919
type Server struct {
20-
Clients map[string]*Client
21-
TrustedKeys map[string]*rsa.PublicKey
22-
ConnectionHandler func(net.Conn)
20+
Clients map[string]*Client
21+
TrustedKeys map[string]*rsa.PublicKey
22+
ConnectionHandler func(net.Conn)
23+
ClientsChangedObservers []func()
2324
}
2425

2526
type Client struct {
@@ -93,6 +94,9 @@ func (s *Server) ListenTls(address string) {
9394
func (s *Server) CloseConnection(conn net.Conn, id string) {
9495
delete(s.Clients, id)
9596
log.Printf("Connection to %v closed", id)
97+
for _, observer := range s.ClientsChangedObservers {
98+
observer()
99+
}
96100
}
97101

98102
func (c *Client) Ok() (reply refproto.ControllerReply) {

internal/app/rcon/teamServer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ func (s *TeamServer) handleClientConnection(conn net.Conn) {
129129
s.Clients[client.Id] = client.Client
130130
defer s.CloseConnection(conn, client.Id)
131131
log.Printf("Client %v connected", client.Id)
132+
for _, observer := range s.ClientsChangedObservers {
133+
observer()
134+
}
132135

133136
for {
134137
req := refproto.TeamToController{}

0 commit comments

Comments
 (0)