From e92f715da7b2b443768ee01146e5f78a66c1d955 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 29 Dec 2021 22:46:50 +0100 Subject: [PATCH] reply handling --- modules/structs/WebClient.go | 57 ++++++++++++++++++-------------- serverCommunication/minecraft.go | 4 +-- serverCommunication/web.go | 14 ++------ 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/modules/structs/WebClient.go b/modules/structs/WebClient.go index b9bd8d0..2037739 100644 --- a/modules/structs/WebClient.go +++ b/modules/structs/WebClient.go @@ -21,17 +21,19 @@ type A struct { } type WebClient struct { - 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 []*A // messages from backend to voice, when response from voice then response to target requester - //voiceCMDIDsByBackendMu sync.Mutex + 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 } func (webClient *WebClient) SendBinaryMessage(conn *websocket.Conn, msg []byte) error { @@ -71,13 +73,27 @@ func (webClient *WebClient) RemoveMobileCmdID(cmdID int) { } } -func (webClient *WebClient) RemoveVoiceCMDIDsByBackend(cmdID int) { - logger.Web.Debugln("before", webClient.VoiceCMDIDsByBackend) - for i, data := range webClient.VoiceCMDIDsByBackend { +func (webClient *WebClient) RemoveVoiceCmdIDByBackend(cmdID int) { + webClient.voiceCmdIDsByBackendMu.Lock() + defer webClient.voiceCmdIDsByBackendMu.Unlock() + + for i, data := range webClient.VoiceCmdIDsByBackend { if data.WebCmdID == cmdID { - newArr := append(webClient.VoiceCMDIDsByBackend[:i], webClient.VoiceCMDIDsByBackend[i+1:]...) - webClient.VoiceCMDIDsByBackend = newArr - logger.Web.Debugln("after", webClient.VoiceCMDIDsByBackend) + newArr := append(webClient.VoiceCmdIDsByBackend[:i], webClient.VoiceCmdIDsByBackend[i+1:]...) + webClient.VoiceCmdIDsByBackend = newArr + return + } + } +} + +func (webClient *WebClient) RemoveMobileCmdIDByBackend(cmdID int) { + webClient.mobileCmdIDsByBackendMu.Lock() + defer webClient.mobileCmdIDsByBackendMu.Unlock() + + for i, data := range webClient.MobileCmdIDsByBackend { + if data.WebCmdID == cmdID { + newArr := append(webClient.MobileCmdIDsByBackend[:i], webClient.MobileCmdIDsByBackend[i+1:]...) + webClient.MobileCmdIDsByBackend = newArr return } } @@ -87,24 +103,17 @@ func removeCmdIDFromList(arr []int, cmdID int) ([]int, error) { index := getCmdIDIndexFromList(arr, cmdID) if index == -1 { - logger.Web.Warnln("index", index) return []int{}, errors.New("index not found") } - logger.Web.Debugln("removeFromList before append", arr) - newArr := append(arr[:index], arr[index+1:]...) - logger.Web.Debugln("removeFromList after append", newArr) - return newArr, nil } func getCmdIDIndexFromList(arr []int, cmdID int) int { for i := 0; i < len(arr); i++ { - logger.Minecraft.Info("getCmdIDIndexFromList", i, arr[i], cmdID) if arr[i] == cmdID { - logger.Web.Debugln("getCmdIDIndexFromList", i) return i } } diff --git a/serverCommunication/minecraft.go b/serverCommunication/minecraft.go index 5b497c0..0068b71 100644 --- a/serverCommunication/minecraft.go +++ b/serverCommunication/minecraft.go @@ -82,9 +82,9 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { a := &structs.A{WebCmdID: webCmdID, CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} - webClient.VoiceCMDIDsByBackend = append(webClient.VoiceCMDIDsByBackend, a) + webClient.VoiceCmdIDsByBackend = append(webClient.VoiceCmdIDsByBackend, a) - logger.Minecraft.Debugln("VoiceCMDIDsByBackend", webClient.VoiceCMDIDsByBackend) + logger.Minecraft.Debugln("VoiceCMDIDsByBackend", webClient.VoiceCmdIDsByBackend) raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusGet, webCmdID, cmdNumber, args) } else { diff --git a/serverCommunication/web.go b/serverCommunication/web.go index d093912..fceadeb 100644 --- a/serverCommunication/web.go +++ b/serverCommunication/web.go @@ -29,18 +29,10 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt status, cmdID, dest, cmdNumber, args := kraProtocol.DecodeWebMessage(msg) - logger.Web.Debugln("kraProtocol", status, cmdID, dest, cmdNumber, args) - if status == kraProtocol.StatusReply { - // TODO: TODO: handling reply - answering message to target dest (ex. GameServer-1) - if isVoice { - for i, data := range webClient.VoiceCMDIDsByBackend { - logger.Web.Debugln("vCmdID range", i, data, cmdID) - if data.WebCmdID == cmdID { - // forward to target dest - - logger.Web.Debugln("forward to target dest", data) + for _, data := range webClient.VoiceCmdIDsByBackend { + if data.WebCmdID == cmdID { // forward to target dest mcClient := GetMinecraftClientByName(getMinecraftServerNameByDest(data.DestFromMinecraftServer)) raw = kraProtocol.EncodeJavaMessage(kraProtocol.StatusReply, data.CmdIDFromMinecraftServer, 0, webClient.Uuid, 0, "") @@ -53,7 +45,7 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt } // remove cmdID from VoiceCMDIDsByBackend list - webClient.RemoveVoiceCMDIDsByBackend(cmdID) + webClient.RemoveVoiceCmdIDByBackend(cmdID) // remove cmdID from minecraftCmdIDs mcClient.RemoveCmdID(data.CmdIDFromMinecraftServer)