Skip to content

Commit

Permalink
Fix high CPU usage issue
Browse files Browse the repository at this point in the history
  • Loading branch information
danorris709 authored Jan 27, 2025
1 parent 43472fe commit 93ded7a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,7 @@ public class RtaWebsocketClient extends WebSocketClient {
private final Logger logger;
private final String xuid;
private boolean firstConnectionId = true;
private CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();

/**
* Create a new websocket and add the Authorization header
Expand All @@ -39,6 +41,10 @@ public String getConnectionId() {
return connectionId;
}

public CompletableFuture<String> getConnectionIdFuture() {
return connectionIdFuture;
}

/**
* When the web socket connects send the request for the connection ID
*
Expand All @@ -64,6 +70,7 @@ public void onMessage(String message) {
if (message.contains("ConnectionId") && firstConnectionId) {
Object[] parts = Constants.GSON.fromJson(message, Object[].class);
connectionId = ((Map<String, String>) parts[4]).get("ConnectionId");
connectionIdFuture.complete(connectionId);
firstConnectionId = false;

// Let xbox know we want friend updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@
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;
import java.net.http.HttpClient;
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;
Expand Down Expand Up @@ -342,33 +338,11 @@ public String getTokenHeader() {
* @return The received connection ID
*/
protected Future<String> waitForConnectionId() {
CompletableFuture<String> 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<Void> waitForRTCConnection() {
CompletableFuture<Void> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class RtcWebsocketClient extends WebSocketClient {
private final List<CandidateHarvester> candidateHarvesters;

private ScheduledFuture<?> heartbeatFuture;
private CompletableFuture<Void> onOpenFuture = new CompletableFuture<>();

/**
* Create a new websocket and add the Authorization header
Expand All @@ -69,6 +71,10 @@ public RtcWebsocketClient(String authenticationToken, ExpandedSessionInfo sessio
this.candidateHarvesters = new ArrayList<>();
}

public CompletableFuture<Void> onOpenFuture() {
return onOpenFuture;
}

/**
* When the web socket connects start the heartbeat to keep the connection alive
*
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 93ded7a

Please sign in to comment.