diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 3ed7ffb..777c763 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -4,3 +4,15 @@ import "krakatoa.net/backend/modules/structs" var WebClients = make(map[string]*structs.WebClient) var MinecraftClients = make(map[string]*structs.MinecraftClient) + +var WebVoiceMessagesSendQueue = make(map[int]*structs.WebClientSendQueueMessage) +var WebMobileMessagesSendQueue = make(map[int]*structs.WebClientSendQueueMessage) + +func IsInWebMessagesSendQueue(queue map[int]*structs.WebClientSendQueueMessage, cmdID int) bool { + for CmdID, _ := range queue { + if CmdID == cmdID { + return true + } + } + return false +} diff --git a/modules/structs/WebClient.go b/modules/structs/WebClient.go index a375871..93ac240 100644 --- a/modules/structs/WebClient.go +++ b/modules/structs/WebClient.go @@ -3,6 +3,7 @@ package structs import ( "errors" "sync" + "time" "github.com/gofiber/websocket/v2" "krakatoa.net/backend/modules/logger" @@ -38,6 +39,13 @@ type WebClient struct { CurrentVoiceCmdIDIndexByBackend int } +type WebClientSendQueueMessage struct { + MessageRaw []byte + CmdID int + TrySendCount int + Time time.Time +} + func (webClient *WebClient) SendBinaryMessage(conn *websocket.Conn, msg []byte) error { if conn == webClient.MobileConn { webClient.mobileConnMu.Lock() diff --git a/serverCommunication/serverCommunication.go b/serverCommunication/serverCommunication.go index a0bcafc..bd600ae 100644 --- a/serverCommunication/serverCommunication.go +++ b/serverCommunication/serverCommunication.go @@ -1,5 +1,13 @@ package serverCommunication +import ( + "fmt" + "time" + + "krakatoa.net/backend/modules/cache" + "krakatoa.net/backend/modules/logger" +) + /* func getClientByDest(dest int, uuid string) (*structs.WebClient, *structs.MinecraftClient) { switch dest { @@ -17,3 +25,15 @@ func getClientByDest(dest int, uuid string) (*structs.WebClient, *structs.Minecr } } */ + +func AckHandler() { + ticker := time.NewTicker(20 * time.Millisecond) + + for _ = range ticker.C { + fmt.Println("") + + for cmdID, object := range cache.WebVoiceMessagesSendQueue { + logger.Web.Println("ackHandler", cmdID, object) + } + } +} diff --git a/serverCommunication/web.go b/serverCommunication/web.go index b70cc71..8419d8e 100644 --- a/serverCommunication/web.go +++ b/serverCommunication/web.go @@ -54,6 +54,14 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt return } } + + if isCmdIDInList(webClient.VoiceCmdIDs, cmdID) { + webClient.RemoveVoiceCmdID(cmdID) + } + + if cache.IsInWebMessagesSendQueue(cache.WebVoiceMessagesSendQueue, cmdID) { + delete(cache.WebVoiceMessagesSendQueue, cmdID) + } } else { for _, data := range webClient.MobileCmdIDsByBackend { if data.WebCmdID == cmdID { // forward to target dest @@ -76,12 +84,10 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt return } } - } - if isVoice && isCmdIDInList(webClient.VoiceCmdIDs, cmdID) { - webClient.RemoveVoiceCmdID(cmdID) - } else if isCmdIDInList(webClient.MobileCmdIDs, cmdID) { - webClient.RemoveMobileCmdID(cmdID) + if isCmdIDInList(webClient.MobileCmdIDs, cmdID) { + webClient.RemoveMobileCmdID(cmdID) + } } return } @@ -119,7 +125,6 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt webClient.MobileCmdIDs = append(webClient.MobileCmdIDs, cmdID) logger.Web.Debugln("after MobileCmdIDs", webClient.MobileCmdIDs) } - return } // no ack @@ -143,6 +148,10 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt if err != nil { logger.WebVoice.Warnln("write:", err) } + + if status == kraProtocol.StatusGet { + cache.WebVoiceMessagesSendQueue[cmdID] = &structs.WebClientSendQueueMessage{MessageRaw: raw, CmdID: cmdID, TrySendCount: 0, Time: time.Now()} + } } else { // mobile not connected raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusSend, 58299, cmdNumber, "") @@ -176,6 +185,10 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, msg []byt if err != nil { logger.WebMobile.Warnln("write:", err) } + + if status == kraProtocol.StatusGet { + cache.WebMobileMessagesSendQueue[cmdID] = &structs.WebClientSendQueueMessage{MessageRaw: raw, CmdID: cmdID, TrySendCount: 0, Time: time.Now()} + } } else { // voice not connected raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusSend, 5456, cmdNumber, "")