reply handling

master
Alex 2021-12-29 22:46:50 +01:00
parent 134c30fa9c
commit e92f715da7
3 changed files with 38 additions and 37 deletions

View File

@ -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
}
}

View File

@ -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 {

View File

@ -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)