From 20816d09f794cb741606b789eb0e703d930d3bc5 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Thu, 28 Mar 2024 18:38:14 -0400 Subject: [PATCH] Fix/lounge lost session (#351) * Maybe a fix * Update CHANGELOG.md * status reset --- CHANGELOG.md | 1 + .../Services/LoungeService/LongPoller.bs | 19 +++++++++--- .../Services/LoungeService/LoungeApi.bs | 23 ++++++++++---- .../Services/LoungeService/LoungeService.bs | 31 ++++++++++++++++++- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ad0c171..5feaadc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- [Attempt] A bug where the lounge would randomly lose the session id, making it unable to cast, and spam the same error over and over - Dialog with timer now stops as expected if any button is pressed ## [0.22.0] - 2024-03-24 diff --git a/playlet-lib/src/components/Services/LoungeService/LongPoller.bs b/playlet-lib/src/components/Services/LoungeService/LongPoller.bs index f88fd19b..353163cb 100644 --- a/playlet-lib/src/components/Services/LoungeService/LongPoller.bs +++ b/playlet-lib/src/components/Services/LoungeService/LongPoller.bs @@ -21,6 +21,7 @@ class LongPoller private socket_timer private status_code = 0 + private status_text private headers function new() @@ -182,16 +183,24 @@ class LongPoller newLineIndex = m.socket_buffer.InStr(`\r\n`) if newLineIndex > 0 operationLine = m.socket_buffer.Left(newLineIndex) - parts = operationLine.Split(" ") - if parts.Count() < 3 or parts[0] <> "HTTP/1.1" + if not operationLine.StartsWith("HTTP/1.1 ") LogError("Invalid operation line:", operationLine) m.CloseSocket() return false end if - m.status_code = Val(parts[1]) - if not m.status_code = 200 - LogError("Http status code:", m.status_code) + status = operationLine.Mid(9) + spaceIndex = status.InStr(" ") + if spaceIndex = -1 + LogError("Invalid operation line:", operationLine) + m.CloseSocket() + return false + end if + statusCode = status.Left(spaceIndex) + m.status_code = Val(statusCode) + m.status_text = status.Mid(spaceIndex + 1) + if m.status_code <> 200 + LogError("Invalid status code:", m.status_code, m.status_text) LogError("Response data:", m.socket_buffer) m.CloseSocket() return false diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs index a8a9554b..9ae27dfc 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs +++ b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs @@ -283,15 +283,15 @@ class LoungeApi end if commandIndex = command[0] - if commandIndex <= m.lastProcessedCommandIndex + commandData = command[1] + commandType = commandData[0] + + if commandIndex <= m.lastProcessedCommandIndex and commandType <> COMMAND_TYPE_G_SESSION_ID and commandType <> COMMAND_TYPE_SESSION_ID LogDebug("Skipping command already processed:", command) continue for end if m.lastProcessedCommandIndex = commandIndex - commandId = command[0] - commandData = command[1] - commandType = commandData[0] commandArgs = invalid if commandData.Count() > 1 commandArgs = commandData[1] @@ -300,7 +300,7 @@ class LoungeApi handler = COMMAND_HANDLER_PREFIX + commandType if m.DoesExist(handler) LogInfo(commandType) - m[handler](commandId, commandType, commandArgs, messagesToSend) + m[handler](commandIndex, commandType, commandArgs, messagesToSend) else LogWarn("Unknown command type:", commandType, "command:", command) end if @@ -502,11 +502,20 @@ class LoungeApi m.outgoingMessagesRequests[requestId] = request end function - function ProcessPendingOutgoingMessages() + function ProcessPendingOutgoingMessages() as object + results = [] for each key in m.outgoingMessagesRequests request = m.outgoingMessagesRequests[key] if request.IsCompleted() response = request.Await() + status = response.StatusCode() + error = response.ErrorMessage() + + results.Push({ + "status": status + "error": error + }) + if not response.IsSuccess() LogError(response.ErrorMessage()) end if @@ -514,6 +523,8 @@ class LoungeApi m.outgoingMessagesRequests.Delete(key) end if end for + + return results end function function SaveCurrentLoungeData() as void diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeService.bs b/playlet-lib/src/components/Services/LoungeService/LoungeService.bs index 19688d1c..913543f4 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeService.bs +++ b/playlet-lib/src/components/Services/LoungeService/LoungeService.bs @@ -117,10 +117,39 @@ function LoungeServiceLoop() as void isOpen = api.Poll(socketMessages, outgoingLoungeMessages) api.SendMessages(outgoingLoungeMessages) - api.ProcessPendingOutgoingMessages() + outgoingLoungeMessagesResults = api.ProcessPendingOutgoingMessages() if not isOpen sleep(3000) + + unknownSid = false + ' TODO:P2 move this check somewhere else + if api.poller.status_code = 400 and ValidString(api.poller.status_text) = "Unknown SID" + api.poller.status_code = 0 + unknownSid = true + end if + + if not unknownSid + for each result in outgoingLoungeMessagesResults + if result.status = 400 and result.error.InStr("Unknown SID") <> -1 + unknownSid = true + exit for + end if + end for + end if + + if unknownSid + ' We lost the session for some reason, so we need to rejoin the lounge + LogWarn("Lost session, rejoining") + if not api.GetSessionData() + m.top.unobserveField("outgoingMessages", messagePort) + m.top.isRunning = false + return + end if + + api.SaveCurrentLoungeData() + end if + api.FetchCommandsRpc(messagePort) end if end while