From b5c2402fa53dcd58b2f702e6582ae109913ee250 Mon Sep 17 00:00:00 2001 From: MahnoKropotkinvich Date: Mon, 23 Dec 2024 23:15:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E6=B5=8B=E8=AF=95=E5=B9=B6=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E4=BA=86=E6=96=B0bug=EF=BC=8C=E6=8B=A9=E6=97=A5?= =?UTF-8?q?=E5=86=8D=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 7 ++++--- conn.go | 6 ++++-- fsm.go | 6 ++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index ab0395e..d014734 100644 --- a/config.go +++ b/config.go @@ -6,9 +6,10 @@ import ( ) type Config struct { - Timeout int `yaml:"timeout"` // minutes - StartCommand string `yaml:"start_command"` - Port string `yaml:"port"` + Timeout int `yaml:"timeout"` // minutes + StartCommand string `yaml:"start_command"` + Port string `yaml:"port"` + ConnectTimeout int `yaml:"connect_timeout"` //seconds } // this is a global constant since it's shared diff --git a/conn.go b/conn.go index 6921d62..6591d11 100644 --- a/conn.go +++ b/conn.go @@ -4,6 +4,7 @@ import ( "io" "net" "sync" + //"time" ) func handle(client net.Conn) { @@ -12,11 +13,12 @@ func handle(client net.Conn) { ChanChan <- queryChan proceed := func() { server, err := net.Dial("tcp", "127.0.0.1:25565") - defer server.Close() if err != nil { GetLogger().Errorf("Failed to connect to MC server: %v", err) return } + // TODO:better way of setting connection timeout + defer server.Close() var wg sync.WaitGroup wg.Add(2) go func() { @@ -64,7 +66,7 @@ func Listen() { GetLogger().Errorf("ection error: %v", err) continue } - + //conn.SetReadDeadline(time.Now().Add(time.Duration(config.ConnectTimeout) * time.Second)) GetLogger().Infof("New connection from %s", conn.RemoteAddr()) go handle(conn) } diff --git a/fsm.go b/fsm.go index 0e9ac7b..c30a342 100644 --- a/fsm.go +++ b/fsm.go @@ -60,6 +60,7 @@ var waitChan chan struct{} // no we don't need cmdChan, we just make it work immediately func handleWaitingToRunning() { + GetLogger().Infof("Server is currently at %s state", stateToStr[RUNNING]) waitChan <- struct{}{} _, ok := <-waitChan if ok { @@ -67,6 +68,7 @@ func handleWaitingToRunning() { } //else it's too late } func handleRunningToWaiting() { + GetLogger().Infof("Server is currently at %s state", stateToStr[WAITING]) waitChan = make(chan struct{}) go waitingThread() state = WAITING @@ -74,6 +76,7 @@ func handleRunningToWaiting() { // TODO: work with daemon func handleWaitingToStopping() { + GetLogger().Infof("Server is currently at %s state", stateToStr[STOPPED]) go stoppingThread() state = STOPPING } @@ -83,6 +86,7 @@ func stoppingThread() { handleStoppingToStopped() } func handleStoppingToStopped() { + GetLogger().Infof("Server is currently at %s state", stateToStr[STOPPED]) state = STOPPED } func bootingThread() { @@ -94,10 +98,12 @@ func bootingThread() { var RunningChan = make(chan struct{}) func handleBootingToRunning() { + GetLogger().Infof("Server is currently at %s state", stateToStr[RUNNING]) state = RUNNING RunningChan <- struct{}{} } func handleStoppedToBooting() { + GetLogger().Infof("Server is currently at %s state", stateToStr[BOOTING]) go bootingThread() state = BOOTING }