Add matching frame and message handling, per-connection session encryption, and loopback traffic dispatch. Serve the decorative content manifest and empty indices to remove startup download waits. Resolve character requests to their explicit account ID, include the nickname in profile replies, and redact matching traffic logs.
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
/**
|
|
* Assemble the local service: data layer, save layer, rules, the
|
|
* protocol-layer services, and the listening port.
|
|
*/
|
|
import type { Server } from 'node:net';
|
|
import { AccountStore } from './accounts/account-store.ts';
|
|
import { actionTable } from './char/actions/action-table.ts';
|
|
import { CharService } from './char/char-service.ts';
|
|
import { DownloadableContentService } from './downloadable-content/downloadable-content-service.ts';
|
|
import { GameFiles } from './game-data/game-files.ts';
|
|
import { LoginService } from './login/login-service.ts';
|
|
import { PlayerProfiles } from './profile/player-profiles.ts';
|
|
import { SaveStore } from './saves/save-store.ts';
|
|
import { createPortListener } from './server/port-listener.ts';
|
|
import { Router } from './server/router.ts';
|
|
import { TrafficLog } from './traffic-log/traffic-log.ts';
|
|
|
|
export interface LocalServiceOptions {
|
|
/** The player's game folder; its data packs are read, never written. */
|
|
readonly gameFolder: string;
|
|
/** Accounts, saves and logs. */
|
|
readonly dataFolder: string;
|
|
readonly port: number;
|
|
}
|
|
|
|
export const LOOPBACK = '127.0.0.1';
|
|
|
|
export interface RunningLocalService {
|
|
readonly logFolder: string;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
export async function startLocalService(options: LocalServiceOptions): Promise<RunningLocalService> {
|
|
const gameFiles = new GameFiles(options.gameFolder);
|
|
const profiles = new PlayerProfiles(new SaveStore(options.dataFolder), gameFiles);
|
|
// One store for both services: accounts registered at sign-in must be visible to char.
|
|
const accounts = new AccountStore(options.dataFolder);
|
|
const router = new Router([
|
|
new LoginService(accounts),
|
|
new CharService(actionTable({ profiles, accounts })),
|
|
new DownloadableContentService(),
|
|
]);
|
|
const log = new TrafficLog(options.dataFolder);
|
|
const listener = createPortListener(router, log);
|
|
await listen(listener.server, options.port);
|
|
return {
|
|
logFolder: log.folder,
|
|
close: () => new Promise((resolve) => {
|
|
listener.server.close(() => resolve());
|
|
listener.destroyConnections();
|
|
}),
|
|
};
|
|
}
|
|
|
|
function listen(server: Server, port: number): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
server.once('error', reject);
|
|
server.listen(port, LOOPBACK, () => {
|
|
server.off('error', reject);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|