Files
WT-Offline/apps/server/src/matching/matching-message.ts
T

104 lines
4.6 KiB
TypeScript

/**
* Messages inside matching packets: one byte naming the kind of message, then JSON.
*
* Source: aces.exe 2.61.0.37.
* - Requests from the client: kind 0x1a; JSON {"data": {"data": <parameters>, "name": <request
* name>}, "id": <request number>, "type": 52} (envelope built at 0x44ea0e0, request body at
* 0x44e9bc0, kind passed to the sender at 0x44ea3b9).
* - The client sorts what it receives by kind (matching::ClientImpl::handleServerPacket,
* 0x44e7540): kind 0x1b goes to the answer queue, handled at 0x44ec4b0, which finds the
* pending request by "id", treats a missing or zero "error" as success and passes "data" on
* (the result itself is inside it, see answerTo);
* every other kind is a server notification (0x44eab40). Every message needs a "type",
* otherwise the client drops it ("bad message. type not set").
*/
export const MessageKind = {
Request: 0x1a,
Answer: 0x1b,
} as const;
/** Value of "type" in the client's requests (0x44e9fcd). */
export const REQUEST_TYPE = 52;
/** Value of "type" in server push notifications (0x44eaba6 -> 0x44ec1ba). */
export const NOTIFY_TYPE = 49;
export interface MatchingMessage {
readonly kind: number;
readonly json: Record<string, unknown>;
}
export function decodeMessage(payload: Uint8Array): MatchingMessage {
if (payload.length === 0) throw new Error('matching: empty message');
const text = Buffer.from(payload.subarray(1)).toString('utf8');
const json: unknown = JSON.parse(text);
if (typeof json !== 'object' || json === null || Array.isArray(json)) throw new Error('matching: message is not a JSON object');
return { kind: payload[0]!, json: json as Record<string, unknown> };
}
export function encodeMessage(message: MatchingMessage): Uint8Array {
const text = Buffer.from(JSON.stringify(message.json), 'utf8');
const payload = new Uint8Array(1 + text.length);
payload[0] = message.kind;
payload.set(text, 1);
return payload;
}
export interface MatchingRequest {
readonly id: number;
readonly type: number;
readonly name: string;
readonly params: Record<string, unknown>;
}
/**
* Value of "type" in the client's keep-alive: about 30 s after the last traffic the client
* sends {"id", "type": 100} through the same request sender as named requests (0x44ea0e0,
* called at 0x44edf0b) with its own completion callback (0x44f21d0). The answer handler
* finds it by "id" like any request; status 0 logs "got CLN_PING response", and no answer
* within 15 s closes the connection ("CLN_PING got no response").
*/
export const PING_TYPE = 100;
/** The ping's id, or undefined when the message is not a keep-alive. */
export function asPing(message: MatchingMessage): number | undefined {
const { id, type } = message.json;
return message.kind === MessageKind.Request && type === PING_TYPE && typeof id === 'number' ? id : undefined;
}
export function pingAnswer(id: number): MatchingMessage {
return { kind: MessageKind.Answer, json: { id, type: PING_TYPE } };
}
/** Undefined when the message is not a request the client expects an answer to. */
export function asRequest(message: MatchingMessage): MatchingRequest | undefined {
if (message.kind !== MessageKind.Request) return undefined;
const { id, type, data } = message.json;
if (typeof id !== 'number' || typeof data !== 'object' || data === null) return undefined;
const { name, data: params } = data as Record<string, unknown>;
return {
id,
type: typeof type === 'number' ? type : REQUEST_TYPE,
name: typeof name === 'string' ? name : '',
params: typeof params === 'object' && params !== null ? params as Record<string, unknown> : {},
};
}
/**
* The answer's result sits one level deeper than "data", mirroring the request envelope:
* {"id", "type", "data": {"data": <result>}}. The answer handler (0x44ec4b0) passes the
* message's "data" to the request's completion wrapper (0x44f1c00, set by the request sender
* 0x44e9bc0), which logs "got response on …" and hands that object's own "data" to the
* caller; for script requests (matching_call, 0x46e1e70) that is the table the UI script
* receives, with "error" added (0x46e5ee0). A result placed directly in "data"
* reached the script as an empty table.
*/
export function answerTo(request: MatchingRequest, result: Record<string, unknown>): MatchingMessage {
return { kind: MessageKind.Answer, json: { id: request.id, type: request.type, data: { data: result } } };
}
/** Server notification (any packet kind != 0x1b, type 49; aces.exe 0x44eaba6..0x44ec1d5). */
export function notifyMessage(name: string, data: Record<string, unknown>): MatchingMessage {
return { kind: MessageKind.Request, json: { type: NOTIFY_TYPE, data: { name, data } } };
}