From 4f965ddefb7c6f0d4ed921cdf137ebdc7418a7a5 Mon Sep 17 00:00:00 2001 From: Evgeny Kovalev Date: Fri, 2 Jun 2023 15:14:44 +0300 Subject: [PATCH] Fix for incorrect shutting down --- internal/gameServer/gameServer.go | 20 ++++++++++---------- main.go | 18 +++++++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/internal/gameServer/gameServer.go b/internal/gameServer/gameServer.go index 9e1ffdb..7ac0a54 100644 --- a/internal/gameServer/gameServer.go +++ b/internal/gameServer/gameServer.go @@ -3,6 +3,7 @@ package gameServer import ( "fmt" "log" + "sync" "daydev.org/shipsgs/internal/config" "daydev.org/shipsgs/internal/player" @@ -27,6 +28,9 @@ type GameServer struct { //Quit the loops correctly and shut down the Game Server Shutdown chan bool + + //sync threads + WG sync.WaitGroup } func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) { @@ -36,27 +40,23 @@ func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) { m.maxLobbies = m.Config.MaxLobbies m.maxPlayers = m.Config.MaxPlayers + m.Shutdown = make(chan bool) + m.currentLobbies = 0 m.currentPlayers = 0 + m.WG.Add(1) + } func (m *GameServer) Update() { for { - // select - go through the messages select { - case c := <-m.connection: - fmt.Println("a new connection", c) - } - - //default shutdown - if m.Shutdown == nil { + case <-m.Shutdown: m.ShutdownServer() - break + m.WG.Done() } - } - } func (m *GameServer) Scheduled1S() { diff --git a/main.go b/main.go index 5d9a551..523656d 100644 --- a/main.go +++ b/main.go @@ -29,32 +29,36 @@ func main() { closer.Bind(cleanup) + //Can be optimised to NEW instead of MAKE sighup := make(chan bool) ch_sighup = &sighup //If debug - fill Stdout too - LOG to use? //Logger = utils.New(os.Stdout, utils.LevelInfo) - //GameServer - var gs gameServer.GameServer - GS = &gs - go gs.Update() - config.ReadConfig() + GS = new(gameServer.GameServer) + GS.Init(Logger, &config.Config) + go GS.Update() + log.Println("Starting Healthy") //closer.Hold() server.SetupAndRun(":8080", ch_sighup) + GS.WG.Wait() + } func cleanup() { fmt.Println("Shutting down ") - *ch_sighup <- true //Investigate why no messages on correct closing - GS.Shutdown = nil + GS.Shutdown <- true + + // Main function has to be closed the last + *ch_sighup <- true Logger.PrintInfo("main", map[string]string{ "Info": "Closing Application Normally",