Skip to content

Commit

Permalink
[bugfix] Decouple team/autoRef connection state from 'timeChanged'
Browse files Browse the repository at this point in the history
  • Loading branch information
g3force committed Jun 10, 2019
1 parent a045c6d commit cd911ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/app/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ func NewGameController() (c *GameController) {
c.AutoRefServer = rcon.NewAutoRefServer()
c.AutoRefServer.LoadTrustedKeys(c.Config.Server.AutoRef.TrustedKeysDir)
c.AutoRefServer.ProcessRequest = c.ProcessAutoRefRequests
c.AutoRefServer.ClientsChangedObservers = []func(){c.updateOnlineStates}

c.TeamServer = rcon.NewTeamServer()
c.TeamServer.LoadTrustedKeys(c.Config.Server.Team.TrustedKeysDir)
c.TeamServer.ProcessTeamRequest = c.ProcessTeamRequests
c.TeamServer.ClientsChangedObservers = []func(){c.updateOnlineStates}

c.CiServer = rcon.NewCiServer()

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

go c.AutoRefServer.Listen(c.Config.Server.AutoRef.Address)
go c.AutoRefServer.ListenTls(c.Config.Server.AutoRef.AddressTls)
Expand Down Expand Up @@ -150,7 +153,6 @@ func (c *GameController) publish() {
c.PublishMutex.Lock()
defer c.PublishMutex.Unlock()

c.updateOnlineStates()
c.statePreserver.Save(c.Engine.PersistentState)

c.TeamServer.AllowedTeamNames = []string{
Expand Down Expand Up @@ -184,6 +186,8 @@ func (c *GameController) updateOnlineStates() {
}
sort.Strings(autoRefs)
c.Engine.GcState.AutoRefsConnected = autoRefs

c.publish()
}

// publishToNetwork publishes the current state to the network (multicast) every 25ms
Expand Down
3 changes: 3 additions & 0 deletions internal/app/rcon/autoRefServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func (s *AutoRefServer) handleClientConnection(conn net.Conn) {
s.Clients[client.Id] = client.Client
defer s.CloseConnection(conn, client.Id)
log.Printf("Client %v connected", client.Id)
for _, observer := range s.ClientsChangedObservers {
observer()
}

for {
req := refproto.AutoRefToController{}
Expand Down
10 changes: 7 additions & 3 deletions internal/app/rcon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
)

type Server struct {
Clients map[string]*Client
TrustedKeys map[string]*rsa.PublicKey
ConnectionHandler func(net.Conn)
Clients map[string]*Client
TrustedKeys map[string]*rsa.PublicKey
ConnectionHandler func(net.Conn)
ClientsChangedObservers []func()
}

type Client struct {
Expand Down Expand Up @@ -93,6 +94,9 @@ func (s *Server) ListenTls(address string) {
func (s *Server) CloseConnection(conn net.Conn, id string) {
delete(s.Clients, id)
log.Printf("Connection to %v closed", id)
for _, observer := range s.ClientsChangedObservers {
observer()
}
}

func (c *Client) Ok() (reply refproto.ControllerReply) {
Expand Down
3 changes: 3 additions & 0 deletions internal/app/rcon/teamServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (s *TeamServer) handleClientConnection(conn net.Conn) {
s.Clients[client.Id] = client.Client
defer s.CloseConnection(conn, client.Id)
log.Printf("Client %v connected", client.Id)
for _, observer := range s.ClientsChangedObservers {
observer()
}

for {
req := refproto.TeamToController{}
Expand Down

0 comments on commit cd911ff

Please sign in to comment.