已测试并修正了新bug,择日再改
This commit is contained in:
parent
084f3644ff
commit
b5c2402fa5
@ -6,9 +6,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Timeout int `yaml:"timeout"` // minutes
|
Timeout int `yaml:"timeout"` // minutes
|
||||||
StartCommand string `yaml:"start_command"`
|
StartCommand string `yaml:"start_command"`
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
|
ConnectTimeout int `yaml:"connect_timeout"` //seconds
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is a global constant since it's shared
|
// this is a global constant since it's shared
|
||||||
|
|||||||
6
conn.go
6
conn.go
@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
//"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handle(client net.Conn) {
|
func handle(client net.Conn) {
|
||||||
@ -12,11 +13,12 @@ func handle(client net.Conn) {
|
|||||||
ChanChan <- queryChan
|
ChanChan <- queryChan
|
||||||
proceed := func() {
|
proceed := func() {
|
||||||
server, err := net.Dial("tcp", "127.0.0.1:25565")
|
server, err := net.Dial("tcp", "127.0.0.1:25565")
|
||||||
defer server.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
GetLogger().Errorf("Failed to connect to MC server: %v", err)
|
GetLogger().Errorf("Failed to connect to MC server: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// TODO:better way of setting connection timeout
|
||||||
|
defer server.Close()
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
@ -64,7 +66,7 @@ func Listen() {
|
|||||||
GetLogger().Errorf("ection error: %v", err)
|
GetLogger().Errorf("ection error: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
//conn.SetReadDeadline(time.Now().Add(time.Duration(config.ConnectTimeout) * time.Second))
|
||||||
GetLogger().Infof("New connection from %s", conn.RemoteAddr())
|
GetLogger().Infof("New connection from %s", conn.RemoteAddr())
|
||||||
go handle(conn)
|
go handle(conn)
|
||||||
}
|
}
|
||||||
|
|||||||
6
fsm.go
6
fsm.go
@ -60,6 +60,7 @@ var waitChan chan struct{}
|
|||||||
|
|
||||||
// no we don't need cmdChan, we just make it work immediately
|
// no we don't need cmdChan, we just make it work immediately
|
||||||
func handleWaitingToRunning() {
|
func handleWaitingToRunning() {
|
||||||
|
GetLogger().Infof("Server is currently at %s state", stateToStr[RUNNING])
|
||||||
waitChan <- struct{}{}
|
waitChan <- struct{}{}
|
||||||
_, ok := <-waitChan
|
_, ok := <-waitChan
|
||||||
if ok {
|
if ok {
|
||||||
@ -67,6 +68,7 @@ func handleWaitingToRunning() {
|
|||||||
} //else it's too late
|
} //else it's too late
|
||||||
}
|
}
|
||||||
func handleRunningToWaiting() {
|
func handleRunningToWaiting() {
|
||||||
|
GetLogger().Infof("Server is currently at %s state", stateToStr[WAITING])
|
||||||
waitChan = make(chan struct{})
|
waitChan = make(chan struct{})
|
||||||
go waitingThread()
|
go waitingThread()
|
||||||
state = WAITING
|
state = WAITING
|
||||||
@ -74,6 +76,7 @@ func handleRunningToWaiting() {
|
|||||||
|
|
||||||
// TODO: work with daemon
|
// TODO: work with daemon
|
||||||
func handleWaitingToStopping() {
|
func handleWaitingToStopping() {
|
||||||
|
GetLogger().Infof("Server is currently at %s state", stateToStr[STOPPED])
|
||||||
go stoppingThread()
|
go stoppingThread()
|
||||||
state = STOPPING
|
state = STOPPING
|
||||||
}
|
}
|
||||||
@ -83,6 +86,7 @@ func stoppingThread() {
|
|||||||
handleStoppingToStopped()
|
handleStoppingToStopped()
|
||||||
}
|
}
|
||||||
func handleStoppingToStopped() {
|
func handleStoppingToStopped() {
|
||||||
|
GetLogger().Infof("Server is currently at %s state", stateToStr[STOPPED])
|
||||||
state = STOPPED
|
state = STOPPED
|
||||||
}
|
}
|
||||||
func bootingThread() {
|
func bootingThread() {
|
||||||
@ -94,10 +98,12 @@ func bootingThread() {
|
|||||||
var RunningChan = make(chan struct{})
|
var RunningChan = make(chan struct{})
|
||||||
|
|
||||||
func handleBootingToRunning() {
|
func handleBootingToRunning() {
|
||||||
|
GetLogger().Infof("Server is currently at %s state", stateToStr[RUNNING])
|
||||||
state = RUNNING
|
state = RUNNING
|
||||||
RunningChan <- struct{}{}
|
RunningChan <- struct{}{}
|
||||||
}
|
}
|
||||||
func handleStoppedToBooting() {
|
func handleStoppedToBooting() {
|
||||||
|
GetLogger().Infof("Server is currently at %s state", stateToStr[BOOTING])
|
||||||
go bootingThread()
|
go bootingThread()
|
||||||
state = BOOTING
|
state = BOOTING
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user