Skip to content

Commit

Permalink
Fixed Go Docker connection problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lubocode committed Oct 14, 2020
1 parent 2c697bd commit 314fcc7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
8 changes: 4 additions & 4 deletions go-version/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Image for compiling script
FROM golang:alpine AS buildstage
# Copy script, change to subdir and compile
COPY minecraft-vanilla-server-hibernation.go /usr/src/
COPY minecraft-server-hibernation.go /usr/src/
WORKDIR /usr/src/
RUN go build minecraft-vanilla-server-hibernation.go
RUN go build minecraft-server-hibernation.go

# Image for running script
FROM openjdk:8-jre-alpine
Expand All @@ -28,5 +28,5 @@ ENV minRAM=512M \
mcPath=/minecraftserver/ \
mcFile=minecraft_server.jar
# Copy compiled go script from first stage
COPY --from=buildstage /usr/src/minecraft-vanilla-server-hibernation .
ENTRYPOINT ./minecraft-vanilla-server-hibernation -minRAM=${minRAM} -maxRAM=${maxRAM} -mcPath=${mcPath} -mcFile=${mcFile}
COPY --from=buildstage /usr/src/minecraft-server-hibernation .
ENTRYPOINT ./minecraft-server-hibernation -minRAM=${minRAM} -maxRAM=${maxRAM} -mcPath=${mcPath} -mcFile=${mcFile}
36 changes: 29 additions & 7 deletions go-version/minecraft-server-hibernation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var info []string = []string{
"Minecraft-Server-Hibernation is used to auto-start/stop a vanilla/modded minecraft server",
"Copyright (C) 2019-2020 gekigek99",
"v2.4 (Go)",
"Visit github page: github.com/gekigek99",
"Visit the github page of og author: github.com/gekigek99",
"Script slightly modified for Docker usage by github.com/lubocode",
"Support the og author at: buymeacoffee.com/gekigek99",
}
Expand Down Expand Up @@ -64,7 +64,7 @@ var dataCountBytesToClients, dataCountBytesToServer float64 = 0, 0
var stopInstances int = 0

// to keep track of how many seconds are still needed to reach serverStatus == "online"
var timeLeftUntilUp int
var timeLeftUntilUp int = minecraftServerStartupTime
var mutex = &sync.Mutex{}

//--------------------------PROGRAM---------------------------//
Expand Down Expand Up @@ -168,7 +168,7 @@ func main() {
minRAM = "-Xms" + minRAM
maxRAM = "-Xmx" + maxRAM

startminecraftserver = "cd " + mcPath + "; screen -dmS minecraftSERVER nice -19 java " + minRAM + " " + maxRAM + " -jar " + mcFile + "nogui"
startminecraftserver = "cd " + mcPath + "; sudo screen -dmS minecraftSERVER nice -19 java " + minRAM + " " + maxRAM + " -jar " + mcFile + "nogui"

fmt.Println("Container started with the following arguments: \n\tminRAM:" + minRAM + " maxRAM:" + maxRAM + " mcPath:" + mcPath + " mcFile:" + mcFile)
// end of flag parsing
Expand Down Expand Up @@ -247,10 +247,7 @@ func handleClientSocket(clientSocket net.Conn) {

// answer to client with ping
answerPingReq(clientSocket)
}

// the client first message is {data, 2} --> the client is trying to join the server
if buffer[dataLen-1] == 2 {
} else if buffer[dataLen-1] == 2 { // the client first message is {data, 2} --> the client is trying to join the server
// read second packet (contains the playerName)
dataLen, err = clientSocket.Read(buffer)
if err != nil {
Expand All @@ -271,6 +268,31 @@ func handleClientSocket(clientSocket net.Conn) {
// answer to client with text in the loadscreen
clientSocket.Write(buildMessage("txt", fmt.Sprintf("Server is starting. Please wait... Time left: %d seconds", timeLeftUntilUp)))
}
} else {
// Go probably entangled two packets again and player tried to join. Search for the two bytes 211 2 and split byte array after them.
// If no 211 2 in byte array, then ignore request and continue listening.
clientMessage := fmt.Sprintln(buffer[:dataLen])
logger("Message incomprehensible: \n" + clientMessage)
if bytes.Contains(buffer, []byte{211, 2}) {
logger("Disentangling join message.")
splitBuffer := bytes.SplitAfter(buffer, []byte{211, 2})
playerName := splitBuffer[1][3:dataLen]

if serverStatus == "offline" {
// client is trying to join the server and serverStatus == "offline" --> issue startMinecraftServer()
startMinecraftServer()
log.Printf("*** %s tried to join from %s:%s to %s:%s\n", playerName, clientAddress, listenPort, targetHost, targetPort)
// answer to client with text in the loadscreen
clientSocket.Write(buildMessage("txt", fmt.Sprintf("Server start command issued. Please wait... Time left: %d seconds", timeLeftUntilUp)))

} else if serverStatus == "starting" {
log.Printf("*** %s tried to join from %s:%s to %s:%s during server startup\n", playerName, clientAddress, listenPort, targetHost, targetPort)
// answer to client with text in the loadscreen
clientSocket.Write(buildMessage("txt", fmt.Sprintf("Server is starting. Please wait... Time left: %d seconds", timeLeftUntilUp)))
}
} else {
logger("Cannot interpret message. Will ignore and go on to listen for new messages.")
}
}

// since the server is still not online, close the client connection
Expand Down

1 comment on commit 314fcc7

@lubocode
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corresponding d556e64

Please sign in to comment.