From c69201ff616685f327b9bfd8f0c2c5eba7262c9b Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 29 Dec 2021 19:34:21 +0100 Subject: [PATCH] reply handling --- modules/structs/WebClient.go | 25 ++++++++++++++++--------- serverCommunication/minecraft.go | 11 +++++++++-- serverCommunication/web.go | 16 ++++++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/modules/structs/WebClient.go b/modules/structs/WebClient.go index 8cc4ff8..9953afd 100644 --- a/modules/structs/WebClient.go +++ b/modules/structs/WebClient.go @@ -14,16 +14,23 @@ type WebClientDb struct { MobileSessionToken string } +type A struct { + CmdIDFromMinecraftServer int + DestFromMinecraftServer int +} + type WebClient struct { - Uuid string - MobileConn *websocket.Conn - mobileConnMu sync.Mutex - VoiceConn *websocket.Conn - voiceConnMu sync.Mutex - MobileCmdIDs []int - mobileCmdIDMu sync.Mutex - VoiceCmdIDs []int - voiceCmdIDMu sync.Mutex + Uuid string + MobileConn *websocket.Conn + mobileConnMu sync.Mutex + MobileCmdIDs []int + mobileCmdIDMu sync.Mutex + VoiceConn *websocket.Conn + voiceConnMu sync.Mutex + VoiceCmdIDs []int + voiceCmdIDMu sync.Mutex + VoiceCMDIDsByBackend map[int]*A // key: cmdID, value: minecraftServerDest + voiceCMDIDsByBackendMu sync.Mutex } func (webClient *WebClient) SendBinaryMessage(conn *websocket.Conn, msg []byte) error { diff --git a/serverCommunication/minecraft.go b/serverCommunication/minecraft.go index b40d335..8f220dc 100644 --- a/serverCommunication/minecraft.go +++ b/serverCommunication/minecraft.go @@ -21,6 +21,7 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { } if status == kraProtocol.StatusReply { + // TODO: if isCmdIDInList(mcClient.CmdIDs, cmdID) { mcClient.RemoveCmdID(cmdID) } @@ -74,9 +75,15 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { } if status == kraProtocol.StatusGet { + webClient := GetWebClientByUuid(playerUuid) + + cmdID = 20 + + webClient.VoiceCMDIDsByBackend[20] = &structs.A{CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} + raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusGet, cmdID, cmdNumber, args) } else { - raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusSend, cmdID, cmdNumber, args) + raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusSend, 0, cmdNumber, args) } if dest == kraProtocol.DestVoice { @@ -101,7 +108,7 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { if status == kraProtocol.StatusGet { raw = kraProtocol.EncodeJavaMessage(kraProtocol.StatusGet, cmdID, dest, playerUuid, cmdNumber, args) } else { - raw = kraProtocol.EncodeJavaMessage(kraProtocol.StatusSend, cmdID, dest, playerUuid, cmdNumber, args) + raw = kraProtocol.EncodeJavaMessage(kraProtocol.StatusSend, 0, dest, playerUuid, cmdNumber, args) } err = client.SendBinaryMessage(raw) diff --git a/serverCommunication/web.go b/serverCommunication/web.go index c04547b..e16c430 100644 --- a/serverCommunication/web.go +++ b/serverCommunication/web.go @@ -34,6 +34,22 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt if status == kraProtocol.StatusReply { // TODO: TODO: handling reply - answering message to target dest (ex. GameServer-1) + if isVoice { + for vCmdID := range webClient.VoiceCMDIDsByBackend { + if vCmdID == cmdID { + // forward to target dest + targetMcServer := webClient.VoiceCMDIDsByBackend[cmdID] + + mcClient := GetMinecraftClientByName(getMinecraftServerNameByDest(targetMcServer.DestFromMinecraftServer)) + + raw = kraProtocol.EncodeJavaMessage(kraProtocol.StatusReply, targetMcServer.CmdIDFromMinecraftServer, 0, webClient.Uuid, 0, "") + + mcClient.SendBinaryMessage(raw) + return + } + } + } + if isVoice && isCmdIDInList(webClient.VoiceCmdIDs, cmdID) { webClient.RemoveVoiceCmdID(cmdID) } else if isCmdIDInList(webClient.MobileCmdIDs, cmdID) {