Fix for incorrect shutting down

master
Evgeny Kovalev 1 year ago
parent b2a1571f8d
commit 4f965ddefb
  1. 20
      internal/gameServer/gameServer.go
  2. 18
      main.go

@ -3,6 +3,7 @@ package gameServer
import ( import (
"fmt" "fmt"
"log" "log"
"sync"
"daydev.org/shipsgs/internal/config" "daydev.org/shipsgs/internal/config"
"daydev.org/shipsgs/internal/player" "daydev.org/shipsgs/internal/player"
@ -27,6 +28,9 @@ type GameServer struct {
//Quit the loops correctly and shut down the Game Server //Quit the loops correctly and shut down the Game Server
Shutdown chan bool Shutdown chan bool
//sync threads
WG sync.WaitGroup
} }
func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) { 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.maxLobbies = m.Config.MaxLobbies
m.maxPlayers = m.Config.MaxPlayers m.maxPlayers = m.Config.MaxPlayers
m.Shutdown = make(chan bool)
m.currentLobbies = 0 m.currentLobbies = 0
m.currentPlayers = 0 m.currentPlayers = 0
m.WG.Add(1)
} }
func (m *GameServer) Update() { func (m *GameServer) Update() {
for { for {
// select - go through the messages
select { select {
case c := <-m.connection: case <-m.Shutdown:
fmt.Println("a new connection", c)
}
//default shutdown
if m.Shutdown == nil {
m.ShutdownServer() m.ShutdownServer()
break m.WG.Done()
} }
} }
} }
func (m *GameServer) Scheduled1S() { func (m *GameServer) Scheduled1S() {

@ -29,32 +29,36 @@ func main() {
closer.Bind(cleanup) closer.Bind(cleanup)
//Can be optimised to NEW instead of MAKE
sighup := make(chan bool) sighup := make(chan bool)
ch_sighup = &sighup ch_sighup = &sighup
//If debug - fill Stdout too - LOG to use? //If debug - fill Stdout too - LOG to use?
//Logger = utils.New(os.Stdout, utils.LevelInfo) //Logger = utils.New(os.Stdout, utils.LevelInfo)
//GameServer
var gs gameServer.GameServer
GS = &gs
go gs.Update()
config.ReadConfig() config.ReadConfig()
GS = new(gameServer.GameServer)
GS.Init(Logger, &config.Config)
go GS.Update()
log.Println("Starting Healthy") log.Println("Starting Healthy")
//closer.Hold() //closer.Hold()
server.SetupAndRun(":8080", ch_sighup) server.SetupAndRun(":8080", ch_sighup)
GS.WG.Wait()
} }
func cleanup() { func cleanup() {
fmt.Println("Shutting down ") fmt.Println("Shutting down ")
*ch_sighup <- true
//Investigate why no messages on correct closing //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{ Logger.PrintInfo("main", map[string]string{
"Info": "Closing Application Normally", "Info": "Closing Application Normally",

Loading…
Cancel
Save