Skip to content

Commit

Permalink
Allow requesting a secondary link from a session.
Browse files Browse the repository at this point in the history
This is provided for Surfer integration.
  • Loading branch information
whitequark committed Nov 28, 2024
1 parent 0946e8f commit 0f1bbdb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/cxxrtl/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Connection {
this._events = packet.events;
this._itemValuesEncodings = packet.features.item_values_encoding;
this._state = ConnectionState.Connected;
await this.onConnected();
await this.onConnected(packet);
} else {
this.rejectPromises(new Error(`unexpected CXXRTL protocol version ${packet.version}`));
}
Expand Down Expand Up @@ -119,14 +119,14 @@ export class Connection {
}
}

private async perform(command: proto.AnyCommand): Promise<proto.AnyResponse> {
async perform(command: proto.AnyCommand): Promise<proto.AnyResponse> {
await this.send(command);
return new Promise((resolve, reject) => {
this.promises.push({ resolve, reject });
});
}

async onConnected(): Promise<void> {}
async onConnected(greetingPacket: proto.ServerGreeting): Promise<void> {}

async onDisconnected(): Promise<void> {}

Expand Down
43 changes: 43 additions & 0 deletions src/debug/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,23 @@ export interface ISimulationStatus {
export class Session {
private connection: Connection;

private secondaryLinks: ILink[] = [];
private greetingPacketPromise: Promise<proto.ServerGreeting>;

constructor(link: ILink) {
this.connection = new Connection(link);
this.greetingPacketPromise = new Promise((resolve, _reject) => {
this.connection.onConnected = async (greetingPacket) => resolve(greetingPacket);
});
this.connection.onDisconnected = async () => {
for (const secondaryLink of this.secondaryLinks) {
secondaryLink.onDone();
}
};
this.connection.onEvent = async (event) => {
for (const secondaryLink of this.secondaryLinks) {
secondaryLink.onRecv(event);
}
if (event.event === 'simulation_paused' || event.event === 'simulation_finished') {
await this.querySimulationStatus();
}
Expand All @@ -49,6 +63,35 @@ export class Session {
this.connection.dispose();
}

createSecondaryLink(): ILink {
const link: ILink = {
dispose: () => {
this.secondaryLinks.splice(this.secondaryLinks.indexOf(link));
},

send: async (clientPacket) => {
if (clientPacket.type === 'greeting') {
const serverGreetingPacket = await this.greetingPacketPromise;
if (clientPacket.version === serverGreetingPacket.version) {
await link.onRecv(serverGreetingPacket);
} else {
throw new Error(
`Secondary link requested greeting version ${clientPacket.version}, ` +
`but server greeting version is ${serverGreetingPacket.version}`
);
}
} else {
const serverPacket = await this.connection.perform(clientPacket);
await link.onRecv(serverPacket);
}
},

onRecv: async (serverPacket) => {},
onDone: async () => {},
};
return link;
}

// ======================================== Inspecting the design

private itemCache: Map<string, proto.ItemDescriptionMap> = new Map();
Expand Down

0 comments on commit 0f1bbdb

Please sign in to comment.