From 93ded7acd6b8e9af480672450639afa220a6161c Mon Sep 17 00:00:00 2001 From: Daniel Norris <33832062+danorris709@users.noreply.github.com> Date: Mon, 27 Jan 2025 23:51:35 +0000 Subject: [PATCH] Fix high CPU usage issue --- .../core/RtaWebsocketClient.java | 7 ++++ .../core/SessionManagerCore.java | 32 ++----------------- .../core/webrtc/RtcWebsocketClient.java | 7 ++++ 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/RtaWebsocketClient.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/RtaWebsocketClient.java index 8f013f2..d76d625 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/RtaWebsocketClient.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/RtaWebsocketClient.java @@ -5,6 +5,7 @@ import org.java_websocket.handshake.ServerHandshake; import java.util.Map; +import java.util.concurrent.CompletableFuture; /** * Handle the connection and authentication with the RTA websocket @@ -16,6 +17,7 @@ public class RtaWebsocketClient extends WebSocketClient { private final Logger logger; private final String xuid; private boolean firstConnectionId = true; + private CompletableFuture connectionIdFuture = new CompletableFuture<>(); /** * Create a new websocket and add the Authorization header @@ -39,6 +41,10 @@ public String getConnectionId() { return connectionId; } + public CompletableFuture getConnectionIdFuture() { + return connectionIdFuture; + } + /** * When the web socket connects send the request for the connection ID * @@ -64,6 +70,7 @@ public void onMessage(String message) { if (message.contains("ConnectionId") && firstConnectionId) { Object[] parts = Constants.GSON.fromJson(message, Object[].class); connectionId = ((Map) parts[4]).get("ConnectionId"); + connectionIdFuture.complete(connectionId); firstConnectionId = false; // Let xbox know we want friend updates diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java index 27a668f..d453d7f 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java @@ -6,17 +6,15 @@ import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException; import com.rtm516.mcxboxbroadcast.core.models.auth.SessionStartBody; import com.rtm516.mcxboxbroadcast.core.models.auth.SessionStartResponse; +import com.rtm516.mcxboxbroadcast.core.models.auth.XboxTokenInfo; import com.rtm516.mcxboxbroadcast.core.models.other.ProfileSettingsResponse; import com.rtm516.mcxboxbroadcast.core.models.session.CreateHandleRequest; import com.rtm516.mcxboxbroadcast.core.models.session.CreateHandleResponse; import com.rtm516.mcxboxbroadcast.core.models.session.SessionRef; import com.rtm516.mcxboxbroadcast.core.models.session.SocialSummaryResponse; -import com.rtm516.mcxboxbroadcast.core.models.auth.XboxTokenInfo; import com.rtm516.mcxboxbroadcast.core.notifications.NotificationManager; import com.rtm516.mcxboxbroadcast.core.storage.StorageManager; - import com.rtm516.mcxboxbroadcast.core.webrtc.RtcWebsocketClient; -import org.java_websocket.enums.ReadyState; import java.io.IOException; import java.net.URI; @@ -24,9 +22,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -342,33 +338,11 @@ public String getTokenHeader() { * @return The received connection ID */ protected Future waitForConnectionId() { - CompletableFuture completableFuture = new CompletableFuture<>(); - - Executors.newCachedThreadPool().submit(() -> { - while (rtaWebsocket.getConnectionId() == null) { - Thread.sleep(100); - } - completableFuture.complete(rtaWebsocket.getConnectionId()); - - return null; - }); - - return completableFuture; + return this.rtaWebsocket.getConnectionIdFuture(); } protected Future waitForRTCConnection() { - CompletableFuture completableFuture = new CompletableFuture<>(); - - Executors.newCachedThreadPool().submit(() -> { - while (rtcWebsocket.getReadyState() == ReadyState.NOT_YET_CONNECTED) { - Thread.sleep(100); - } - completableFuture.complete(null); - - return null; - }); - - return completableFuture; + return this.rtcWebsocket.onOpenFuture(); } protected String setupSession() { diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java index 832a5a2..10cde78 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -45,6 +46,7 @@ public class RtcWebsocketClient extends WebSocketClient { private final List candidateHarvesters; private ScheduledFuture heartbeatFuture; + private CompletableFuture onOpenFuture = new CompletableFuture<>(); /** * Create a new websocket and add the Authorization header @@ -69,6 +71,10 @@ public RtcWebsocketClient(String authenticationToken, ExpandedSessionInfo sessio this.candidateHarvesters = new ArrayList<>(); } + public CompletableFuture onOpenFuture() { + return onOpenFuture; + } + /** * When the web socket connects start the heartbeat to keep the connection alive * @@ -82,6 +88,7 @@ public void onOpen(ServerHandshake serverHandshake) { heartbeatFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> { send(Constants.GSON.toJson(new WsToMessage(0, null, null))); }, 40, 40, TimeUnit.SECONDS); + onOpenFuture.complete(null); } /**