/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import stream = require('stream'); import * as ee from 'events'; import {DebugProtocol} from '@vscode/debugprotocol'; export class ProtocolClient extends ee.EventEmitter { private static TWO_CRLF = '\r\n\r\n'; private outputStream: stream.Writable; private sequence: number; private pendingRequests = new Map void>(); private rawData = Buffer.alloc(0); private contentLength: number; constructor() { super(); this.sequence = 1; this.contentLength = -1; } protected connect(readable: stream.Readable, writable: stream.Writable): void { this.outputStream = writable; readable.on('data', (data: Buffer) => { this.handleData(data); }); } public send(command: 'initialize', args: DebugProtocol.InitializeRequestArguments) : Promise; public send(command: 'configurationDone', args: DebugProtocol.ConfigurationDoneArguments) : Promise; public send(command: 'launch', args: DebugProtocol.LaunchRequestArguments) : Promise; public send(command: 'attach', args: DebugProtocol.AttachRequestArguments) : Promise; public send(command: 'restart', args: DebugProtocol.RestartArguments) : Promise; public send(command: 'disconnect', args: DebugProtocol.DisconnectArguments) : Promise; public send(command: 'setBreakpoints', args: DebugProtocol.SetBreakpointsArguments) : Promise; public send(command: 'setFunctionBreakpoints', args: DebugProtocol.SetFunctionBreakpointsArguments) : Promise; public send(command: 'setExceptionBreakpoints', args: DebugProtocol.SetExceptionBreakpointsArguments) : Promise; public send(command: 'setInstructionBreakpoints', args: DebugProtocol.SetInstructionBreakpointsArguments) : Promise; public send(command: 'dataBreakpointInfo', args: DebugProtocol.DataBreakpointInfoArguments) : Promise; public send(command: 'setDataBreakpoints', args: DebugProtocol.SetDataBreakpointsArguments) : Promise; public send(command: 'continue', args: DebugProtocol.ContinueArguments) : Promise; public send(command: 'next', args: DebugProtocol.NextArguments) : Promise; public send(command: 'stepIn', args: DebugProtocol.StepInArguments) : Promise; public send(command: 'stepOut', args: DebugProtocol.StepOutArguments) : Promise; public send(command: 'stepBack', args: DebugProtocol.StepBackArguments) : Promise; public send(command: 'reverseContinue', args: DebugProtocol.ReverseContinueArguments) : Promise; public send(command: 'restartFrame', args: DebugProtocol.RestartFrameArguments) : Promise; public send(command: 'goto', args: DebugProtocol.GotoArguments) : Promise; public send(command: 'pause', args: DebugProtocol.PauseArguments) : Promise; public send(command: 'stackTrace', args: DebugProtocol.StackTraceArguments) : Promise; public send(command: 'scopes', args: DebugProtocol.ScopesArguments) : Promise; public send(command: 'variables', args: DebugProtocol.VariablesArguments) : Promise; public send(command: 'setVariable', args: DebugProtocol.SetVariableArguments) : Promise; public send(command: 'source', args: DebugProtocol.SourceArguments) : Promise; public send(command: 'threads') : Promise; public send(command: 'modules') : Promise; public send(command: 'evaluate', args: DebugProtocol.EvaluateArguments) : Promise; public send(command: 'stepInTargets', args: DebugProtocol.StepInTargetsArguments) : Promise; public send(command: 'gotoTargets', args: DebugProtocol.GotoTargetsArguments) : Promise; public send(command: 'completions', args: DebugProtocol.CompletionsArguments) : Promise; public send(command: 'exceptionInfo', args: DebugProtocol.ExceptionInfoArguments) : Promise; public send(command: 'readMemoryRequest', args: DebugProtocol.ReadMemoryArguments) : Promise; public send(command: 'writeMemoryRequest', args: DebugProtocol.WriteMemoryArguments) : Promise; public send(command: string, args?: any) : Promise; public send(command: string, args?: any): Promise { return new Promise((completeDispatch, errorDispatch) => { this.doSend(command, args, (result: DebugProtocol.Response) => { if (result.success) { completeDispatch(result); } else { errorDispatch(new Error(result.message)); } }); }); } private doSend(command: string, args: any, clb: (result: DebugProtocol.Response) => void): void { const request: DebugProtocol.Request = { type: 'request', seq: this.sequence++, command: command }; if (args && Object.keys(args).length > 0) { request.arguments = args; } // store callback for this request this.pendingRequests.set(request.seq, clb); const json = JSON.stringify(request); this.outputStream.write(`Content-Length: ${Buffer.byteLength(json, 'utf8')}\r\n\r\n${json}`, 'utf8'); } private handleData(data: Buffer): void { this.rawData = Buffer.concat([this.rawData, data]); while (true) { if (this.contentLength >= 0) { if (this.rawData.length >= this.contentLength) { const message = this.rawData.toString('utf8', 0, this.contentLength); this.rawData = this.rawData.slice(this.contentLength); this.contentLength = -1; if (message.length > 0) { this.dispatch(message); } continue; // there may be more complete messages to process } } else { const idx = this.rawData.indexOf(ProtocolClient.TWO_CRLF); if (idx !== -1) { const header = this.rawData.toString('utf8', 0, idx); const lines = header.split('\r\n'); for (let i = 0; i < lines.length; i++) { const pair = lines[i].split(/: +/); if (pair[0] === 'Content-Length') { this.contentLength = +pair[1]; } } this.rawData = this.rawData.slice(idx + ProtocolClient.TWO_CRLF.length); continue; } } break; } } private dispatch(body: string): void { const rawData = JSON.parse(body); if (typeof rawData.event !== 'undefined') { const event = rawData; this.emit(event.event, event); } else { const response = rawData; const clb = this.pendingRequests.get(response.request_seq); if (clb) { this.pendingRequests.delete(response.request_seq); clb(response); } } } }