From 22a3e1d98b87bd9c4d7997bb15e4831bd1bdfa70 Mon Sep 17 00:00:00 2001 From: mkwcat Date: Sun, 5 May 2024 20:56:27 -0400 Subject: [PATCH] Wait for backend to start before accepting connections --- gamestats/main.go | 26 ++++++++++++++------------ gpsp/main.go | 39 ++++++++++++++------------------------- main.go | 30 +++++++++++++++++------------- nas/main.go | 4 +++- natneg/main.go | 24 +++++++++++++----------- qr2/main.go | 24 +++++++++++++----------- serverbrowser/main.go | 28 +++++++++++++++------------- 7 files changed, 89 insertions(+), 86 deletions(-) diff --git a/gamestats/main.go b/gamestats/main.go index ddb8653..4149de5 100644 --- a/gamestats/main.go +++ b/gamestats/main.go @@ -70,20 +70,22 @@ func StartServer() { panic(err) } - // Close the listener when the application closes. - defer l.Close() - logging.Notice("GSTATS", "Listening on", address) + go func() { + // Close the listener when the application closes. + defer l.Close() + logging.Notice("GSTATS", "Listening on", address) - for { - // Listen for an incoming connection. - conn, err := l.Accept() - if err != nil { - panic(err) + for { + // Listen for an incoming connection. + conn, err := l.Accept() + if err != nil { + panic(err) + } + + // Handle connections in a new goroutine. + go handleRequest(conn) } - - // Handle connections in a new goroutine. - go handleRequest(conn) - } + }() } // Handles incoming requests. diff --git a/gpsp/main.go b/gpsp/main.go index 2c18410..1d74233 100644 --- a/gpsp/main.go +++ b/gpsp/main.go @@ -2,19 +2,10 @@ package gpsp import ( "bufio" - "context" "net" "wwfc/common" "wwfc/gpcm" "wwfc/logging" - - "github.com/jackc/pgx/v4/pgxpool" -) - -var ( - ctx = context.Background() - pool *pgxpool.Pool - userId int64 ) func StartServer() { @@ -27,20 +18,22 @@ func StartServer() { panic(err) } - // Close the listener when the application closes. - defer l.Close() - logging.Notice("GPSP", "Listening on", address) + go func() { + // Close the listener when the application closes. + defer l.Close() + logging.Notice("GPSP", "Listening on", address) - for { - // Listen for an incoming connection. - conn, err := l.Accept() - if err != nil { - panic(err) + for { + // Listen for an incoming connection. + conn, err := l.Accept() + if err != nil { + panic(err) + } + + // Handle connections in a new goroutine. + go handleRequest(conn) } - - // Handle connections in a new goroutine. - go handleRequest(conn) - } + }() } // Handles incoming requests. @@ -77,19 +70,15 @@ func handleRequest(conn net.Conn) { logging.Error(moduleName, "Unknown command:", command.Command) logging.Error(moduleName, "Raw data:", string(buffer)) replyError(moduleName, conn, gpcm.ErrParse) - break case "ka": conn.Write([]byte(`\ka\\final\`)) - break case "otherslist": conn.Write([]byte(handleOthersList(command))) - break case "search": conn.Write([]byte(handleSearch(command))) - break } } } diff --git a/main.go b/main.go index d9d2fb5..16716b7 100644 --- a/main.go +++ b/main.go @@ -63,7 +63,20 @@ func backendMain() { os.Exit(1) } - logging.Notice("BACKEND", "Listening on", aurora.BrightCyan(address)) + common.ConnectFrontend() + + wg := &sync.WaitGroup{} + actions := []func(){nas.StartServer, gpcm.StartServer, qr2.StartServer, gpsp.StartServer, serverbrowser.StartServer, sake.StartServer, natneg.StartServer, api.StartServer, gamestats.StartServer} + wg.Add(len(actions)) + for _, action := range actions { + go func(ac func()) { + defer wg.Done() + ac() + }(action) + } + + // Wait for all servers to start + wg.Wait() go func() { for { @@ -77,19 +90,10 @@ func backendMain() { } }() - // TODO: Wait until the servers are started before allowing in connections + logging.Notice("BACKEND", "Listening on", aurora.BrightCyan(address)) - wg := &sync.WaitGroup{} - actions := []func(){nas.StartServer, gpcm.StartServer, qr2.StartServer, gpsp.StartServer, serverbrowser.StartServer, sake.StartServer, natneg.StartServer, api.StartServer, gamestats.StartServer} - wg.Add(len(actions)) - for _, action := range actions { - go func(ac func()) { - defer wg.Done() - ac() - }(action) - } - - wg.Wait() + // Prevent application from exiting + select {} } // RPCPacket.NewConnection is called by the frontend to notify the backend of a new connection diff --git a/nas/main.go b/nas/main.go index 30d7228..eb4bdf8 100644 --- a/nas/main.go +++ b/nas/main.go @@ -55,7 +55,9 @@ func StartServer() { } logging.Notice("NAS", "Starting HTTP server on", address) - panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest))) + go func() { + panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest))) + }() } var regexSakeHost = regexp.MustCompile(`^([a-z\-]+\.)?sake\.gs\.`) diff --git a/natneg/main.go b/natneg/main.go index 4d386d0..7f6d07f 100644 --- a/natneg/main.go +++ b/natneg/main.go @@ -94,19 +94,21 @@ func StartServer() { natnegConn = conn - // Close the listener when the application closes. - defer conn.Close() - logging.Notice("NATNEG", "Listening on", address) + go func() { + // Close the listener when the application closes. + defer conn.Close() + logging.Notice("NATNEG", "Listening on", address) - for { - buffer := make([]byte, 1024) - size, addr, err := conn.ReadFrom(buffer) - if err != nil { - continue + for { + buffer := make([]byte, 1024) + size, addr, err := conn.ReadFrom(buffer) + if err != nil { + continue + } + + go handleConnection(conn, addr, buffer[:size]) } - - go handleConnection(conn, addr, buffer[:size]) - } + }() } func handleConnection(conn net.PacketConn, addr net.Addr, buffer []byte) { diff --git a/qr2/main.go b/qr2/main.go index 546f134..3d00a15 100644 --- a/qr2/main.go +++ b/qr2/main.go @@ -40,19 +40,21 @@ func StartServer() { masterConn = conn - // Close the listener when the application closes. - defer conn.Close() - logging.Notice("QR2", "Listening on", address) + go func() { + // Close the listener when the application closes. + defer conn.Close() + logging.Notice("QR2", "Listening on", address) - for { - buf := make([]byte, 1024) - _, addr, err := conn.ReadFrom(buf) - if err != nil { - continue + for { + buf := make([]byte, 1024) + _, addr, err := conn.ReadFrom(buf) + if err != nil { + continue + } + + go handleConnection(conn, addr, buf) } - - go handleConnection(conn, addr, buf) - } + }() } func handleConnection(conn net.PacketConn, addr net.Addr, buffer []byte) { diff --git a/serverbrowser/main.go b/serverbrowser/main.go index 91f9de6..838f65a 100644 --- a/serverbrowser/main.go +++ b/serverbrowser/main.go @@ -64,21 +64,23 @@ func StartServer() { panic(err) } - // Close the listener when the application closes. - defer l.Close() - logging.Notice(ModuleName, "Listening on", address) + go func() { + // Close the listener when the application closes. + defer l.Close() + logging.Notice(ModuleName, "Listening on", address) - for { - // Listen for an incoming connection. - conn, err := l.Accept() - if err != nil { - fmt.Println("Error accepting: ", err.Error()) - os.Exit(1) + for { + // Listen for an incoming connection. + conn, err := l.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + os.Exit(1) + } + + // Handle connections in a new goroutine. + go handleRequest(conn) } - - // Handle connections in a new goroutine. - go handleRequest(conn) - } + }() } // Handles incoming requests.