From 897c03192aa535de24382379861be40a195d0339 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 1 Jan 2022 16:38:18 +0100 Subject: [PATCH] forwarding handling --- modules/structs/WebClient.go | 29 +++++++++++++++-------------- serverCommunication/minecraft.go | 31 ++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/modules/structs/WebClient.go b/modules/structs/WebClient.go index 46863e0..a375871 100644 --- a/modules/structs/WebClient.go +++ b/modules/structs/WebClient.go @@ -21,20 +21,21 @@ type A struct { } type WebClient struct { - Uuid string - MobileConn *websocket.Conn - mobileConnMu sync.Mutex - MobileCmdIDs []int - mobileCmdIDMu sync.Mutex - MobileCmdIDsByBackend []*A - mobileCmdIDsByBackendMu sync.Mutex - VoiceConn *websocket.Conn - voiceConnMu sync.Mutex - VoiceCmdIDs []int - voiceCmdIDMu sync.Mutex - VoiceCmdIDsByBackend []*A // messages from backend to voice, when response from voice then response to target requester - voiceCmdIDsByBackendMu sync.Mutex - CurrentCmdIDIndexByBackend int + Uuid string + MobileConn *websocket.Conn + mobileConnMu sync.Mutex + MobileCmdIDs []int + mobileCmdIDMu sync.Mutex + MobileCmdIDsByBackend []*A + mobileCmdIDsByBackendMu sync.Mutex + CurrentMobileVoiceCmdIDIndexByBackend int + VoiceConn *websocket.Conn + voiceConnMu sync.Mutex + VoiceCmdIDs []int + voiceCmdIDMu sync.Mutex + VoiceCmdIDsByBackend []*A // messages from backend to voice, when response from voice then response to target requester + voiceCmdIDsByBackendMu sync.Mutex + CurrentVoiceCmdIDIndexByBackend int } func (webClient *WebClient) SendBinaryMessage(conn *websocket.Conn, msg []byte) error { diff --git a/serverCommunication/minecraft.go b/serverCommunication/minecraft.go index 9f1202b..a2c33dd 100644 --- a/serverCommunication/minecraft.go +++ b/serverCommunication/minecraft.go @@ -21,7 +21,6 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { } if status == kraProtocol.StatusReply { - // TODO: if isCmdIDInList(mcClient.CmdIDs, cmdID) { mcClient.RemoveCmdID(cmdID) } @@ -31,6 +30,7 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { var raw []byte var err error + // queue handling when message already in process if status == kraProtocol.StatusGet { // cmdID already in queue logger.Minecraft.Debugln("cmdIDList", mcClient.CmdIDs) @@ -49,6 +49,7 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { mcClient.CmdIDs = append(mcClient.CmdIDs, cmdID) } + // dest handling if dest == kraProtocol.DestBackend { resArgs := minecraftCommandHandler(cmdNumber, playerUuid) @@ -78,17 +79,33 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { if status == kraProtocol.StatusGet { webClient := GetWebClientByUuid(playerUuid) - webCmdID := structs.GenerateWebCmdID(webClient.CurrentCmdIDIndexByBackend) + var webCmdID int - webClient.CurrentCmdIDIndexByBackend = webCmdID + if dest == kraProtocol.DestVoice { + webCmdID = structs.GenerateWebCmdID(webClient.CurrentVoiceCmdIDIndexByBackend) - logger.Minecraft.Debugln("webCmdID", webCmdID) + webClient.CurrentVoiceCmdIDIndexByBackend = webCmdID - a := &structs.A{WebCmdID: webCmdID, CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} + logger.Minecraft.Debugln("webCmdID", webCmdID) - webClient.VoiceCmdIDsByBackend = append(webClient.VoiceCmdIDsByBackend, a) + a := &structs.A{WebCmdID: webCmdID, CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} - logger.Minecraft.Debugln("VoiceCMDIDsByBackend", webClient.VoiceCmdIDsByBackend) + webClient.VoiceCmdIDsByBackend = append(webClient.VoiceCmdIDsByBackend, a) + + logger.Minecraft.Debugln("VoiceCMDIDsByBackend", webClient.VoiceCmdIDsByBackend) + } else { // dest mobile + webCmdID = structs.GenerateWebCmdID(webClient.CurrentMobileVoiceCmdIDIndexByBackend) + + webClient.CurrentMobileVoiceCmdIDIndexByBackend = webCmdID + + logger.Minecraft.Debugln("webCmdID", webCmdID) + + a := &structs.A{WebCmdID: webCmdID, CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} + + webClient.MobileCmdIDsByBackend = append(webClient.MobileCmdIDsByBackend, a) + + logger.Minecraft.Debugln("MobileCMDIDsByBackend", webClient.MobileCmdIDsByBackend) + } raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusGet, webCmdID, cmdNumber, args) } else {