Backend/modules/structs/WebClient.go

302 lines
10 KiB
Go

package structs
import (
"errors"
"sync"
"time"
"github.com/gofiber/websocket/v2"
"krakatoa.net/backend/modules/logger"
)
type WebClientDb struct {
Uuid string
VoiceSessionToken string
MobileSessionToken string
}
type A struct {
WebCmdID int
CmdIDFromMinecraftServer int
DestFromMinecraftServer int
}
type WebClient struct {
Uuid string
MobileConn *websocket.Conn
mobileConnMu sync.Mutex
//MobileCmdIDs []int // cache for received cmdIDs
//mobileCmdIDMu sync.Mutex
//MobileCmdIDsByBackend []*A
//mobileCmdIDsByBackendMu sync.Mutex
CurrentMobileSendMessageCmdIDIndexByBackend int
VoiceConn *websocket.Conn
voiceConnMu sync.Mutex
//VoiceCmdIDs []int // cache for received cmdIDs
//voiceCmdIDMu sync.Mutex
//VoiceCmdIDsByBackend []*A // messages from backend to voice, when response from voice then response to target requester
//voiceCmdIDsByBackendMu sync.Mutex
CurrentVoiceSendMessageCmdIDIndexByBackend int
SendVoiceQueueMessages []*SendQueueMessage
sendVoiceQueueMessagesMu sync.Mutex
SendMobileQueueMessages []*SendQueueMessage
sendMobileQueueMessagesMu sync.Mutex
ReceivedVoiceQueueMessages []*ReceivedQueueMessage
receivedVoiceQueueMessagesMu sync.Mutex
ReceivedMobileQueueMessages []*ReceivedQueueMessage
receivedMobileQueueMessagesMu sync.Mutex
}
func (webClient *WebClient) AddMessageToVoiceSendQueue(originDest int, originCmdID int, messageRaw []byte, cmdID int) {
webClient.SendVoiceQueueMessages = append(webClient.SendVoiceQueueMessages, &SendQueueMessage{MessageRaw: messageRaw, CmdID: cmdID, TrySendCount: 0, OriginDest: originDest, OriginCmdID: originCmdID, Time: time.Now()})
}
func (webClient *WebClient) AddMessageToVoiceReceivedQueue(originDest int, originCmdID int, messageRaw []byte) {
webClient.ReceivedVoiceQueueMessages = append(webClient.ReceivedVoiceQueueMessages, &ReceivedQueueMessage{MessageRaw: messageRaw, OriginDest: originDest, OriginCmdID: originCmdID})
}
func (webClient *WebClient) AddMessageToMobileSendQueue(originDest int, originCmdID int, messageRaw []byte, cmdID int) {
webClient.SendMobileQueueMessages = append(webClient.SendMobileQueueMessages, &SendQueueMessage{MessageRaw: messageRaw, CmdID: cmdID, TrySendCount: 0, OriginDest: originDest, OriginCmdID: originCmdID, Time: time.Now()})
}
func (webClient *WebClient) AddMessageToMobileReceivedQueue(originDest int, originCmdID int, messageRaw []byte) {
webClient.ReceivedMobileQueueMessages = append(webClient.ReceivedMobileQueueMessages, &ReceivedQueueMessage{MessageRaw: messageRaw, OriginDest: originDest, OriginCmdID: originCmdID})
}
func (webClient *WebClient) IsCmdIDInSendVoiceMessagesQueue(cmdID int) bool {
return isCmdIDInSendMessagesQueue(webClient.SendVoiceQueueMessages, cmdID)
}
func (webClient *WebClient) IsCmdIDInSendMobileMessagesQueue(cmdID int) bool {
return isCmdIDInSendMessagesQueue(webClient.SendMobileQueueMessages, cmdID)
}
func isCmdIDInSendMessagesQueue(sendMessagesQueue []*SendQueueMessage, cmdID int) bool {
for _, queueMsg := range sendMessagesQueue {
if cmdID == queueMsg.CmdID {
return true
}
}
return false
}
func (webClient *WebClient) IsCmdIDInReceivedVoiceMessagesQueue(cmdID int) bool {
return isCmdIDInReceivedMessagesQueue(webClient.ReceivedVoiceQueueMessages, cmdID)
}
func (webClient *WebClient) IsCmdIDInReceivedMobileMessagesQueue(cmdID int) bool {
return isCmdIDInReceivedMessagesQueue(webClient.ReceivedMobileQueueMessages, cmdID)
}
func isCmdIDInReceivedMessagesQueue(receivedMessagesQueue []*ReceivedQueueMessage, cmdID int) bool {
for _, queueMsg := range receivedMessagesQueue {
if cmdID == queueMsg.OriginCmdID {
return true
}
}
return false
}
func (webClient *WebClient) GetMessageFromSendVoiceQueueByCmdID(cmdID int) (queueMsg SendQueueMessage, err error) {
return getMessageFromSendQueueByCmdID(webClient.SendVoiceQueueMessages, cmdID)
}
func (webClient *WebClient) GetMessageFromSendMobileQueueByCmdID(cmdID int) (queueMsg SendQueueMessage, err error) {
return getMessageFromSendQueueByCmdID(webClient.SendMobileQueueMessages, cmdID)
}
func getMessageFromSendQueueByCmdID(sendQueueMessages []*SendQueueMessage, cmdID int) (queueMsg SendQueueMessage, err error) {
for _, queueMsg := range sendQueueMessages {
if cmdID == queueMsg.CmdID {
return *queueMsg, nil
}
}
return SendQueueMessage{}, errors.New("message from send queue by cmdID not found")
}
func (webClient *WebClient) GetMessageFromReceivedVoiceQueueByCmdID(cmdID int) (queueMsg ReceivedQueueMessage, err error) {
return getMessageFromReceivedQueueByCmdID(webClient.ReceivedVoiceQueueMessages, cmdID)
}
func (webClient *WebClient) GetMessageFromReceivedMobileQueueByCmdID(cmdID int) (queueMsg ReceivedQueueMessage, err error) {
return getMessageFromReceivedQueueByCmdID(webClient.ReceivedMobileQueueMessages, cmdID)
}
func getMessageFromReceivedQueueByCmdID(receivedQueueMessages []*ReceivedQueueMessage, cmdID int) (queueMsg ReceivedQueueMessage, err error) {
for _, queueMsg := range receivedQueueMessages {
if cmdID == queueMsg.OriginCmdID {
return *queueMsg, nil
}
}
return ReceivedQueueMessage{}, errors.New("message from received queue by cmdID not found")
}
func (webClient *WebClient) RemoveMessageFromVoiceSendQueueByCmdID(cmdID int) {
webClient.SendVoiceQueueMessages = removeMessageFromSendQueueByCmdID(webClient.SendVoiceQueueMessages, &webClient.sendVoiceQueueMessagesMu, cmdID)
logger.WebVoice.Warnln("RemoveMessageFromVoiceSendQueueByCmdID list", webClient.SendVoiceQueueMessages)
}
func (webClient *WebClient) RemoveMessageFromMobileSendQueueByCmdID(cmdID int) {
webClient.SendMobileQueueMessages = removeMessageFromSendQueueByCmdID(webClient.SendMobileQueueMessages, &webClient.sendMobileQueueMessagesMu, cmdID)
}
func removeMessageFromSendQueueByCmdID(queueMessages []*SendQueueMessage, mutex *sync.Mutex, cmdID int) []*SendQueueMessage {
mutex.Lock()
defer mutex.Unlock()
for index, message := range queueMessages {
if message.CmdID == cmdID {
logger.Web.Debugln("removeMessageFromSendQueueByCmdID before", queueMessages)
newArr := append(queueMessages[:index], queueMessages[index+1:]...)
queueMessages = newArr
logger.Web.Debugln("removeMessageFromSendQueueByCmdID after", queueMessages)
break
}
}
return queueMessages
}
func (webClient *WebClient) RemoveMessageFromVoiceReceivedQueueByCmdID(cmdID int) {
removeMessageFromReceivedQueueByCmdID(webClient.ReceivedVoiceQueueMessages, &webClient.receivedVoiceQueueMessagesMu, cmdID)
}
func (webClient *WebClient) RemoveMessageFromMobileReceivedQueueByCmdID(cmdID int) {
removeMessageFromReceivedQueueByCmdID(webClient.ReceivedMobileQueueMessages, &webClient.receivedMobileQueueMessagesMu, cmdID)
}
func removeMessageFromReceivedQueueByCmdID(queueMessages []*ReceivedQueueMessage, mutex *sync.Mutex, cmdID int) {
mutex.Lock()
defer mutex.Unlock()
for index, message := range queueMessages {
if message.OriginCmdID == cmdID {
logger.Web.Debugln("removeMessageFromReceivedQueueByCmdID before", queueMessages)
newArr := append(queueMessages[:index], queueMessages[index+1:]...)
queueMessages = newArr
logger.Web.Debugln("removeMessageFromReceivedQueueByCmdID after", queueMessages)
break
}
}
}
func (webClient *WebClient) SendBinaryMessage(conn *websocket.Conn, msg []byte) error {
logger.Web.Infoln("SendBinaryMessage")
var err error
if conn == webClient.MobileConn {
webClient.mobileConnMu.Lock()
defer webClient.mobileConnMu.Unlock()
if conn == nil {
logger.WebMobile.Warnln("err conn nil SendBinaryMessage")
return errors.New("conn nil")
}
err = conn.WriteMessage(websocket.BinaryMessage, msg)
if err != nil {
logger.WebMobile.Warnln("err SendBinaryMessage to mobile client")
}
return err
}
webClient.voiceConnMu.Lock()
defer webClient.voiceConnMu.Unlock()
if conn == nil {
logger.WebVoice.Warnln("err conn nil SendBinaryMessage")
return errors.New("conn nil")
}
err = conn.WriteMessage(websocket.BinaryMessage, msg)
if err != nil {
logger.WebVoice.Warnln("err SendBinaryMessage to voice client")
}
return err
}
/*
func (webClient *WebClient) RemoveVoiceCmdID(cmdID int) {
webClient.voiceCmdIDMu.Lock()
defer webClient.voiceCmdIDMu.Unlock()
newArr, err := removeCmdIDFromList(webClient.VoiceCmdIDs, cmdID)
if err != nil {
logger.WebVoice.Warnln("removeCmdIDFromList:", err)
} else {
webClient.VoiceCmdIDs = newArr
}
}
func (webClient *WebClient) RemoveMobileCmdID(cmdID int) {
webClient.mobileCmdIDMu.Lock()
defer webClient.mobileCmdIDMu.Unlock()
newArr, err := removeCmdIDFromList(webClient.MobileCmdIDs, cmdID)
if err != nil {
logger.WebMobile.Warnln("removeCmdIDFromList:", err)
} else {
webClient.MobileCmdIDs = newArr
}
}
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
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
}
}
}
func removeCmdIDFromList(arr []int, cmdID int) ([]int, error) {
index := getCmdIDIndexFromList(arr, cmdID)
if index == -1 {
return []int{}, errors.New("index not found")
}
newArr := append(arr[:index], arr[index+1:]...)
return newArr, nil
}
func getCmdIDIndexFromList(arr []int, cmdID int) int {
for i := 0; i < len(arr); i++ {
if arr[i] == cmdID {
return i
}
}
return -1
}
*/
func GenerateWebCmdID(currentIndex int) int {
if currentIndex >= 250 || currentIndex < 10 {
return 10
}
currentIndex++
return currentIndex
}