Skip to content

Commit

Permalink
clean up vestigial remote reconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Apr 16, 2024
1 parent e4df2d5 commit b136e8c
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 83 deletions.
8 changes: 6 additions & 2 deletions src/broadcast/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ipc_broadcastErrorOccurredEvent,
ipc_broadcastListUpdatedEvent,
ipc_broadcastReconnectEvent,
ipc_connect,
ipc_dolphinStatusChangedEvent,
ipc_refreshBroadcastList,
ipc_slippiStatusChangedEvent,
Expand Down Expand Up @@ -58,8 +59,11 @@ const broadcastApi: BroadcastService = {
});
return destroy;
},
async refreshBroadcastList(authToken: string): Promise<void> {
await ipc_refreshBroadcastList.renderer!.trigger({ authToken });
async connect(authToken: string): Promise<void> {
await ipc_connect.renderer!.trigger({ authToken });
},
async refreshBroadcastList(): Promise<void> {
await ipc_refreshBroadcastList.renderer!.trigger({});
},
async watchBroadcast(broadcasterId: string): Promise<void> {
await ipc_watchBroadcast.renderer!.trigger({ broadcasterId });
Expand Down
7 changes: 2 additions & 5 deletions src/broadcast/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { _, makeEndpoint } from "utils/ipc";
import type { BroadcasterItem, StartBroadcastConfig } from "./types";

// Handlers
export const ipc_connect = makeEndpoint.main("connect", <{ authToken: string }>_, <SuccessPayload>_);

export const ipc_refreshBroadcastList = makeEndpoint.main(
"refreshBroadcastList",
<{ authToken: string }>_,
<SuccessPayload>_,
);
export const ipc_refreshBroadcastList = makeEndpoint.main("refreshBroadcastList", <EmptyPayload>_, <SuccessPayload>_);

export const ipc_watchBroadcast = makeEndpoint.main("watchBroadcast", <{ broadcasterId: string }>_, <SuccessPayload>_);

Expand Down
22 changes: 19 additions & 3 deletions src/broadcast/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import log from "electron-log";

import type { BroadcastWorker } from "./broadcast.worker.interface";
import { createBroadcastWorker } from "./broadcast.worker.interface";
import { ipc_refreshBroadcastList, ipc_startBroadcast, ipc_stopBroadcast, ipc_watchBroadcast } from "./ipc";
import {
ipc_connect,
ipc_refreshBroadcastList,
ipc_startBroadcast,
ipc_stopBroadcast,
ipc_watchBroadcast,
} from "./ipc";
import type { SpectateWorker } from "./spectate.worker.interface";
import { createSpectateWorker } from "./spectate.worker.interface";
import type { SpectateController } from "./types";
Expand Down Expand Up @@ -35,11 +41,21 @@ export default function setupBroadcastIpc({
}
});

ipc_refreshBroadcastList.main!.handle(async ({ authToken }) => {
ipc_connect.main!.handle(async ({ authToken }) => {
if (!spectateWorker) {
spectateWorker = await createSpectateWorker(dolphinManager);
}
await spectateWorker.refreshBroadcastList(authToken);
await spectateWorker.connect(authToken);
return { success: true };
});

ipc_refreshBroadcastList.main!.handle(async () => {
Preconditions.checkExists(
spectateWorker,
"Could not refresh broadcast list, make sure spectateWorker is connected.",
);

await spectateWorker.refreshBroadcastList();
return { success: true };
});

Expand Down
7 changes: 5 additions & 2 deletions src/broadcast/spectate.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface Methods {
startSpectate(broadcastId: string, targetPath: string, dolphinOptions: SpectateDolphinOptions): Promise<string>;
stopSpectate(broadcastId: string): Promise<void>;
dolphinClosed(playbackId: string): Promise<void>;
refreshBroadcastList(authToken: string): Promise<void>;
connect(authToken: string): Promise<void>;
refreshBroadcastList(): Promise<void>;
getLogObservable(): Observable<string>;
getErrorObservable(): Observable<Error | string>;
getBroadcastListObservable(): Observable<BroadcasterItem[]>;
Expand Down Expand Up @@ -84,8 +85,10 @@ const methods: WorkerSpec = {
async dolphinClosed(playbackId: string): Promise<void> {
spectateManager.handleClosedDolphin(playbackId);
},
async refreshBroadcastList(authToken: string): Promise<void> {
async connect(authToken: string): Promise<void> {
await spectateManager.connect(authToken);
},
async refreshBroadcastList(): Promise<void> {
await spectateManager.refreshBroadcastList();
},
getLogObservable(): Observable<string> {
Expand Down
6 changes: 4 additions & 2 deletions src/broadcast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export type BroadcastService = {
onDolphinStatusChanged(handle: (status: number) => void): () => void;
onSlippiStatusChanged(handle: (status: number) => void): () => void;
onSpectateErrorMessage(handle: (message: string | null) => void): () => void;
refreshBroadcastList(authToken: string): Promise<void>;
connect(authToken: string): Promise<void>;
refreshBroadcastList(): Promise<void>;
watchBroadcast(broadcasterId: string): Promise<void>;
startBroadcast(config: StartBroadcastConfig): Promise<void>;
stopBroadcast(): Promise<void>;
Expand All @@ -87,7 +88,8 @@ export type SpectateDolphinOptions = {
export interface SpectateController {
startSpectate(broadcastId: string, targetPath: string, dolphinOptions: SpectateDolphinOptions): Promise<string>;
dolphinClosed(playbackId: string): Promise<void>;
refreshBroadcastList(authToken: string): Promise<void>;
connect(authToken: string): Promise<void>;
refreshBroadcastList(): Promise<void>;
getBroadcastListObservable(): Observable<BroadcasterItem[]>;
getSpectateDetailsObservable(): Observable<{ playbackId: string; filePath: string; broadcasterName: string }>;
getGameEndObservable(): Observable<string>;
Expand Down
18 changes: 1 addition & 17 deletions src/remote/api.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import {
ipc_reconnectRemoteServer,
ipc_remoteReconnectEvent,
ipc_remoteStateEvent,
ipc_startRemoteServer,
ipc_stopRemoteServer,
} from "./ipc";
import { ipc_remoteStateEvent, ipc_startRemoteServer, ipc_stopRemoteServer } from "./ipc";
import type { RemoteService } from "./types";

const remoteApi: RemoteService = {
onReconnect(handle: () => void) {
const { destroy } = ipc_remoteReconnectEvent.renderer!.handle(async () => {
handle();
});
return destroy;
},
onState(handle: (connected: boolean, started: boolean, port?: number) => void) {
const { destroy } = ipc_remoteStateEvent.renderer!.handle(async ({ connected, started, port }) => {
handle(connected, started, port);
Expand All @@ -24,10 +12,6 @@ const remoteApi: RemoteService = {
const { result } = await ipc_startRemoteServer.renderer!.trigger({ authToken, port });
return result;
},
async reconnectRemoteServer(authToken: string) {
const { result } = await ipc_reconnectRemoteServer.renderer!.trigger({ authToken });
return result;
},
async stopRemoteServer() {
await ipc_stopRemoteServer.renderer!.trigger({});
},
Expand Down
6 changes: 0 additions & 6 deletions src/remote/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { EmptyPayload, SuccessPayload } from "utils/ipc";
import { _, makeEndpoint } from "utils/ipc";

export const ipc_remoteReconnectEvent = makeEndpoint.renderer("remote_reconnect", <EmptyPayload>_);
export const ipc_remoteStateEvent = makeEndpoint.renderer(
"remote_state",
<{ connected: boolean; started: boolean; port?: number }>_,
Expand All @@ -11,9 +10,4 @@ export const ipc_startRemoteServer = makeEndpoint.main(
<{ authToken: string; port: number }>_,
<{ success: boolean; err?: string }>_,
);
export const ipc_reconnectRemoteServer = makeEndpoint.main(
"remote_reconnectServer",
<{ authToken: string }>_,
<{ success: boolean }>_,
);
export const ipc_stopRemoteServer = makeEndpoint.main("remote_stopServer", <EmptyPayload>_, <SuccessPayload>_);
24 changes: 4 additions & 20 deletions src/remote/remote_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DolphinEventType, DolphinLaunchType } from "@dolphin/types";
import type { SettingsManager } from "@settings/settings_manager";
import electronLog from "electron-log";
import http from "http";
import { throttle } from "lodash";
import throttle from "lodash/throttle";
import type { AddressInfo } from "net";
import type { connection } from "websocket";
import { server as WebSocketServer } from "websocket";
Expand All @@ -16,7 +16,6 @@ const SPECTATE_PROTOCOL = "spectate-protocol";
const log = electronLog.scope("remote_server");

export class RemoteServer {
private authToken: string;
private dolphinLaunchTimes: Map<string, number>;

private spectateController: SpectateController | null;
Expand All @@ -31,7 +30,6 @@ export class RemoteServer {
private readonly settingsManager: SettingsManager,
private readonly getSpectateController: () => Promise<SpectateController>,
) {
this.authToken = "";
this.dolphinLaunchTimes = new Map();
this.spectateController = null;
this.httpServer = null;
Expand Down Expand Up @@ -117,18 +115,18 @@ export class RemoteServer {
});
}

public async start(authToken: string, port: number): Promise<{ success: boolean; err?: string }> {
public async start(initialAuthToken: string, port: number): Promise<{ success: boolean; err?: string }> {
if (!this.spectateController) {
await this.setupSpectateController();
}
await this.spectateController!.connect(initialAuthToken);

if (this.httpServer && this.remoteServer) {
return (<AddressInfo>this.httpServer.address()).port === port
? { success: true }
: { success: false, err: `server already started on port ${port}` };
}

this.authToken = authToken;
try {
this.httpServer = http.createServer();
await new Promise<void>((resolve, reject) => {
Expand All @@ -153,7 +151,7 @@ export class RemoteServer {
const newConnection = request.accept(SPECTATE_PROTOCOL, request.origin);
const throttledRefresh = throttle(async () => {
try {
await this.spectateController!.refreshBroadcastList(this.authToken);
await this.spectateController!.refreshBroadcastList();
} catch (e) {
const err = typeof e === "string" ? e : e instanceof Error ? e.message : "unknown";
newConnection.sendUTF(JSON.stringify({ op: "list-broadcasts-response", err }));
Expand Down Expand Up @@ -213,20 +211,6 @@ export class RemoteServer {
}
}

public async reconnect(authToken: string) {
if (!authToken) {
return false;
}

this.authToken = authToken;
try {
await this.spectateController!.refreshBroadcastList(this.authToken);
return true;
} catch (e) {
return false;
}
}

public stop() {
if (this.httpServer && this.remoteServer) {
if (this.connection) {
Expand Down
5 changes: 1 addition & 4 deletions src/remote/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SpectateController } from "@broadcast/types";
import type { DolphinManager } from "@dolphin/manager";
import type { SettingsManager } from "@settings/settings_manager";

import { ipc_reconnectRemoteServer, ipc_startRemoteServer, ipc_stopRemoteServer } from "./ipc";
import { ipc_startRemoteServer, ipc_stopRemoteServer } from "./ipc";
import { RemoteServer } from "./remote_server";

export default function setupRemoteIpc({
Expand All @@ -18,9 +18,6 @@ export default function setupRemoteIpc({
ipc_startRemoteServer.main!.handle(async ({ authToken, port }) => {
return await remoteServer.start(authToken, port);
});
ipc_reconnectRemoteServer.main!.handle(async ({ authToken }) => {
return { success: await remoteServer.reconnect(authToken) };
});
ipc_stopRemoteServer.main!.handle(async () => {
remoteServer.stop();
return { success: true };
Expand Down
2 changes: 0 additions & 2 deletions src/remote/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export type RemoteService = {
onReconnect(handle: () => void): () => void;
onState(handle: (connected: boolean, started: boolean, port?: number) => void): () => void;
startRemoteServer(authToken: string, port: number): Promise<{ success: boolean; err?: string }>;
reconnectRemoteServer(authToken: string): Promise<{ success: boolean }>;
stopRemoteServer(): Promise<void>;
};
13 changes: 4 additions & 9 deletions src/renderer/lib/hooks/use_app_listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useBroadcast } from "./use_broadcast";
import { useBroadcastList, useBroadcastListStore } from "./use_broadcast_list";
import { useConsoleDiscoveryStore } from "./use_console_discovery";
import { useIsoVerification } from "./use_iso_verification";
import { useRemoteServer, useRemoteServerStateStore } from "./use_remote_server";
import { useRemoteServerStateStore } from "./use_remote_server";
import { useReplayBrowserNavigation } from "./use_replay_browser_list";
import { useSettings } from "./use_settings";
import { useSettingsModal } from "./use_settings_modal";
Expand Down Expand Up @@ -188,15 +188,10 @@ export const useAppListeners = () => {
});
}, [startBroadcast, broadcastService]);

const [, refreshBroadcasts] = useBroadcastList();
const [, connect] = useBroadcastList();
React.useEffect(() => {
return broadcastService.onSpectateReconnect(refreshBroadcasts);
}, [refreshBroadcasts, broadcastService]);

const [, , reconnectRemoteServer] = useRemoteServer();
React.useEffect(() => {
return remoteService.onReconnect(reconnectRemoteServer);
}, [reconnectRemoteServer, remoteService]);
return broadcastService.onSpectateReconnect(connect);
}, [connect, broadcastService]);

const updateRemoteServerState = useRemoteServerStateStore((store) => store.setState);
React.useEffect(() => {
Expand Down
9 changes: 6 additions & 3 deletions src/renderer/lib/hooks/use_broadcast_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ export const useBroadcastList = () => {
const items = useBroadcastListStore((store) => store.items);
const { showError } = useToasts();

const refresh = async () => {
const connect = async () => {
const authToken = await authService.getUserToken();
await broadcastService.refreshBroadcastList(authToken);
await broadcastService.connect(authToken);
};
const refresh = async () => {
await broadcastService.refreshBroadcastList();
};

// Limit refreshing to once every 2 seconds
const throttledRefresh = throttle(() => {
refresh().catch(showError);
}, 2000);

return [items, throttledRefresh] as const;
return [items, connect, throttledRefresh] as const;
};
6 changes: 1 addition & 5 deletions src/renderer/lib/hooks/use_remote_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ export const useRemoteServer = () => {
const authToken = await authService.getUserToken();
return remoteService.startRemoteServer(authToken, port);
};
const reconnectRemoteServer = async () => {
const authToken = await authService.getUserToken();
return remoteService.reconnectRemoteServer(authToken);
};
const stopRemoteServer = async () => {
return remoteService.stopRemoteServer();
};
return [state, startRemoteServer, reconnectRemoteServer, stopRemoteServer] as const;
return [state, startRemoteServer, stopRemoteServer] as const;
};
9 changes: 6 additions & 3 deletions src/renderer/pages/spectate/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ export function createSpectatePage({ broadcastService }: CreateSpectatePageArgs)

const Page = React.memo(() => {
const user = useAccount((store) => store.user);
const [currentBroadcasts, refreshBroadcasts] = useBroadcastList();
const [remoteServerState, startRemoteServer, , stopRemoteServer] = useRemoteServer();
const [currentBroadcasts, connect, refreshBroadcasts] = useBroadcastList();
const [remoteServerState, startRemoteServer, stopRemoteServer] = useRemoteServer();
return (
<SpectatePage
userId={user?.uid}
watchBroadcast={watchBroadcast}
broadcasts={currentBroadcasts}
onRefreshBroadcasts={refreshBroadcasts}
onRefreshBroadcasts={async () => {
await connect();
await refreshBroadcasts();
}}
remoteServerState={remoteServerState}
startRemoteServer={startRemoteServer}
stopRemoteServer={stopRemoteServer}
Expand Down

0 comments on commit b136e8c

Please sign in to comment.