give each mc client a destination number when connecting

master
Alex 2022-03-15 21:28:47 +01:00
parent 194d5f4510
commit 33e5f71f8e
3 changed files with 33 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import (
type MinecraftClient struct { type MinecraftClient struct {
Name string Name string
Dest int
Conn *websocket.Conn Conn *websocket.Conn
connMu sync.Mutex connMu sync.Mutex
CmdIDs []int // cache for received cmdIDs CmdIDs []int // cache for received cmdIDs

View File

@ -171,8 +171,8 @@ func HandleMinecraftMessage(conn *websocket.Conn, msg []byte) {
raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusGet, webCmdID, cmdNumber, args) raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusGet, webCmdID, cmdNumber, args)
if dest == kraProtocol.DestVoice { if dest == kraProtocol.DestVoice { // TODO: correct dest
webClient.SendVoiceQueueMessages = append(webClient.SendVoiceQueueMessages, &structs.SendQueueMessage{MessageRaw: raw, CmdID: webCmdID, TrySendCount: 0, Time: time.Now()}) webClient.SendVoiceQueueMessages = append(webClient.SendVoiceQueueMessages, &structs.SendQueueMessage{MessageRaw: raw, CmdID: webCmdID, TrySendCount: 0, OriginDest: 111111, OriginCmdID: cmdID, Time: time.Now()})
} else { } else {
webClient.SendMobileQueueMessages = append(webClient.SendMobileQueueMessages, &structs.SendQueueMessage{MessageRaw: raw, CmdID: webCmdID, TrySendCount: 0, Time: time.Now()}) webClient.SendMobileQueueMessages = append(webClient.SendMobileQueueMessages, &structs.SendQueueMessage{MessageRaw: raw, CmdID: webCmdID, TrySendCount: 0, Time: time.Now()})
} }
@ -295,3 +295,32 @@ func SendMessageToMinecraftServer(originDest int, originCmdID int, status int, d
return nil return nil
} }
func GenerateDestForNewMinecraftClient() int {
availableDestFound := false
newDest := 10
for !availableDestFound {
if isDestInUsageFromOneMinecraftClient(newDest) {
logger.Minecraft.Debugln("found dest", newDest)
availableDestFound = true
} else {
logger.Minecraft.Debugln("count dest up")
newDest++
}
}
logger.Minecraft.Debugln("finally found this dest", newDest)
return newDest
}
func isDestInUsageFromOneMinecraftClient(dest int) bool {
for _, mcClient := range cache.MinecraftClients {
if mcClient.Dest == dest {
return true
}
}
return false
}

View File

@ -18,7 +18,7 @@ func RunHub() {
case connection := <-register: case connection := <-register:
servername := connection.Query("s") servername := connection.Query("s")
cache.MinecraftClients[servername] = &structs.MinecraftClient{Name: servername, Conn: connection} cache.MinecraftClients[servername] = &structs.MinecraftClient{Name: servername, Dest: serverCommunication.GenerateDestForNewMinecraftClient(), Conn: connection}
logger.Minecraft.Debugln("connection registered ", connection.Query("c"), connection) logger.Minecraft.Debugln("connection registered ", connection.Query("c"), connection)