Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DebugProtocol fixes #186

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/adapters/DebugProtocolAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { Variable } from '../debugProtocol/events/responses/VariablesRespon
import { VariableType } from '../debugProtocol/events/responses/VariablesResponse';
import type { TelnetAdapter } from './TelnetAdapter';
import type { DeviceInfo } from 'roku-deploy';
import type { ThreadsResponse } from '../debugProtocol/events/responses/ThreadsResponse';

/**
* A class that connects to a Roku device over telnet debugger port and provides a standardized way of interacting with it.
Expand Down Expand Up @@ -228,7 +229,6 @@ export class DebugProtocolAdapter {
this.client = undefined;
}
this.client = new DebugProtocolClient(this.options);
await this.client.connect();
try {
// Emit IO from the debugger.
// eslint-disable-next-line @typescript-eslint/no-misused-promises
Expand Down Expand Up @@ -266,14 +266,14 @@ export class DebugProtocolAdapter {
this.client.on('close', () => {
this.emit('close');
this.beginAppExit();
void this.client.destroy();
void this.client?.destroy();
this.client = undefined;
});

// Listen for the app exit event
this.client.on('app-exit', () => {
this.emit('app-exit');
void this.client.destroy();
void this.client?.destroy();
this.client = undefined;
});

Expand Down Expand Up @@ -325,13 +325,12 @@ export class DebugProtocolAdapter {
this.emit('diagnostics', diagnostics);
});

this.client.on('control-socket-connected', () => {
this.emit('app-ready');
});
await this.client.connect();

this.logger.log(`Connected to device`, { host: this.options.host, connected: this.connected });
this.connected = true;
this.emit('connected', this.connected);
this.emit('app-ready');

//the adapter is connected and running smoothly. resolve the promise
deferred.resolve();
Expand Down Expand Up @@ -718,7 +717,21 @@ export class DebugProtocolAdapter {
}
return this.resolve('threads', async () => {
let threads: Thread[] = [];
let threadsResponse = await this.client.threads();
let threadsResponse: ThreadsResponse;
for (let i = 0; i < 3; i++) {
Christian-Holbrook marked this conversation as resolved.
Show resolved Hide resolved
threadsResponse = await this.client.threads();
if (threadsResponse.data.errorCode === ErrorCode.NOT_STOPPED) {
this.logger.log(`Threads request retrying... ${i}:\n`, threadsResponse);
threadsResponse = undefined;
const pauseResponse = await this.client.pause(true);
await util.sleep(100);
} else {
break;
}
}
if (!threadsResponse) {
throw new Error('Failed to get threads data');
Christian-Holbrook marked this conversation as resolved.
Show resolved Hide resolved
}

for (let i = 0; i < threadsResponse.data?.threads?.length ?? 0; i++) {
let threadInfo = threadsResponse.data.threads[i];
Expand Down
13 changes: 6 additions & 7 deletions src/debugProtocol/client/DebugProtocolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ export class DebugProtocolClient {
public on<T = AllThreadsStoppedUpdate | ThreadAttachedUpdate>(eventName: 'runtime-error' | 'suspend', handler: (data: T) => void);
public on(eventName: 'io-output', handler: (output: string) => void);
public on(eventName: 'protocol-version', handler: (data: ProtocolVersionDetails) => void);
public on(eventName: 'control-socket-connected', handler: () => void);
public on(eventName: 'handshake-verified', handler: (data: HandshakeResponse) => void);
// public on(eventname: 'rendezvous', handler: (output: RendezvousHistory) => void);
// public on(eventName: 'runtime-error', handler: (error: BrightScriptRuntimeError) => void);
Expand All @@ -202,7 +201,7 @@ export class DebugProtocolClient {
private emit(eventName: 'data', update: Buffer);
private emit(eventName: 'breakpoints-verified', event: BreakpointsVerifiedEvent);
private emit(eventName: 'suspend' | 'runtime-error', data: AllThreadsStoppedUpdate | ThreadAttachedUpdate);
private emit(eventName: 'app-exit' | 'cannot-continue' | 'close' | 'handshake-verified' | 'io-output' | 'protocol-version' | 'control-socket-connected' | 'start', data?);
private emit(eventName: 'app-exit' | 'cannot-continue' | 'close' | 'handshake-verified' | 'io-output' | 'protocol-version' | 'start', data?);
private async emit(eventName: string, data?) {
//emit these events on next tick, otherwise they will be processed immediately which could cause issues
await util.sleep(0);
Expand Down Expand Up @@ -230,7 +229,6 @@ export class DebugProtocolClient {
client: this,
server: connection
});
await this.emit('control-socket-connected');
return connection;
}

Expand Down Expand Up @@ -337,16 +335,17 @@ export class DebugProtocolClient {
}
}

public pause() {
public pause(force = false) {
return this.processStopRequest(
StopRequest.fromJson({
requestId: this.requestIdSequence++
})
}),
force
);
}

private async processStopRequest(request: StopRequest) {
if (this.isStopped === false) {
private async processStopRequest(request: StopRequest, force = false) {
if (this.isStopped === false || force) {
return this.sendRequest<GenericResponse>(request);
}
}
Expand Down