Skip to content

Commit

Permalink
GO added join message disentangling
Browse files Browse the repository at this point in the history
Go sometimes reads the two join messages as one buffer and causes the script to misinterpret what the client sent.
Attempt to disentangle these two messages added.
  • Loading branch information
lubocode committed Oct 14, 2020
1 parent d17ce4e commit d556e64
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions go-version/minecraft-server-hibernation.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,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 @@ -284,6 +281,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, config.Advanced.ListenPort, config.Advanced.TargetHost, config.Advanced.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, config.Advanced.ListenPort, config.Advanced.TargetHost, config.Advanced.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

0 comments on commit d556e64

Please sign in to comment.