/**
 * Copyright (c) 2021 Expo, Inc.
 * Copyright (c) 2018 Drifty Co.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import Debug from 'debug' ; import type * as net from 'net' ; import * as path from 'path' ; import { GDBProtocolClient } from '../protocol/gdb' ; import { ServiceClient } from './client' ; const debug = Debug ( 'expo:xdl:ios:lib:client:debugserver' ) ; export class DebugserverClient extends ServiceClient < GDBProtocolClient > { constructor ( public socket ) { super ( socket , new GDBProtocolClient ( socket ) ) ; } async setMaxPacketSize ( size ) { return this . sendCommand ( 'QSetMaxPacketSize:' , [ size . toString ( ) ] ) ; } async setWorkingDir ( workingDir ) { return this . sendCommand ( 'QSetWorkingDir:' , [ workingDir ] ) ; } async checkLaunchSuccess ( ) { return this . sendCommand ( 'qLaunchSuccess' , [ ] ) ; } async attachByName ( name ) { const hexName = Buffer . from ( name ) . toString ( 'hex' ) ; return this . sendCommand ( `vAttachName;${ hexName }` , [ ] ) ; } async continue ( ) { return this . sendCommand ( 'c' , [ ] ) ; } halt ( ) { debug ( 'Sending ^C to debugserver' ) ; return this . protocolClient . socket . write ( '\u0003' ) ; } async kill ( ) { const msg = { cmd : 'k' , args : [ ] } ; return this . protocolClient . sendMessage ( msg , ( resp , resolve , reject ) => { this . protocolClient . socket . write ( '+' ) ; const parts = resp . split ( ';' ) ; for ( const part of parts ) { if ( part . includes ( 'description' ) ) { resolve ( Buffer . from ( part . split ( ':' ) [ 1 ] , 'hex' ) . toString ( 'ascii' ) ) ; } } } ) ; } async launchApp ( appPath , executableName ) { const fullPath = path . join ( appPath , executableName ) ; const hexAppPath = Buffer . from ( fullPath ) . toString ( 'hex' ) ; const appCommand = `A${ hexAppPath . length },0,${ hexAppPath }` ; return this . sendCommand ( appCommand , [ ] ) ; } async sendCommand ( cmd , args ) { const msg = { cmd , args } ; debug ( `Sending command: ${ cmd }, args: ${ args }` ) ; const resp = await this . protocolClient . sendMessage ( msg ) ; this . protocolClient . socket . write ( '+' ) ; return resp ; } }