From 8fe0e0c60d4534e9930937ed246c0f57e9dc7f27 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 29 Dec 2021 20:38:26 +0100 Subject: [PATCH] reply handling --- modules/structs/WebClient.go | 22 +++++++++++----------- serverCommunication/minecraft.go | 6 +++--- serverCommunication/web.go | 14 +++++++++++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/modules/structs/WebClient.go b/modules/structs/WebClient.go index 4f8d585..64f6971 100644 --- a/modules/structs/WebClient.go +++ b/modules/structs/WebClient.go @@ -21,17 +21,17 @@ 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 - voiceCMDIDsByBackendMu 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 // 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 { diff --git a/serverCommunication/minecraft.go b/serverCommunication/minecraft.go index 816eade..62df18c 100644 --- a/serverCommunication/minecraft.go +++ b/serverCommunication/minecraft.go @@ -77,12 +77,12 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) { if status == kraProtocol.StatusGet { webClient := GetWebClientByUuid(playerUuid) - a := &structs.A{CmdID: 20, CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} - - webClient.VoiceCMDIDsByBackend = append(webClient.VoiceCMDIDsByBackend, a) + a := &structs.A{CmdIDFromMinecraftServer: cmdID, DestFromMinecraftServer: 10} cmdID = 20 + webClient.VoiceCMDIDsByBackend[cmdID] = a + raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusGet, cmdID, cmdNumber, args) } else { raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusSend, 0, cmdNumber, args) diff --git a/serverCommunication/web.go b/serverCommunication/web.go index 2331008..7bae0fd 100644 --- a/serverCommunication/web.go +++ b/serverCommunication/web.go @@ -45,7 +45,19 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt raw = kraProtocol.EncodeJavaMessage(kraProtocol.StatusReply, data.CmdIDFromMinecraftServer, 0, webClient.Uuid, 0, "") - mcClient.SendBinaryMessage(raw) + err = mcClient.SendBinaryMessage(raw) + + if err != nil { + logger.Web.Warnln("write:", err) + return + } + + // remove cmdID from VoiceCMDIDsByBackend list + delete(webClient.VoiceCMDIDsByBackend, cmdID) + + // remove cmdID from voiceCmdIDs + webClient.RemoveVoiceCmdID(cmdID) + return } }