From d780c90bd3ca9d1b1fb3c24d841549f1d7a85a04 Mon Sep 17 00:00:00 2001 From: Marco Giovanni Date: Thu, 26 Apr 2018 17:18:44 -0300 Subject: [PATCH 1/3] Created interactive shell feature --- SharpAdbClient/AdbSocket.cs | 2 +- SharpAdbClient/InteractiveShell/Shell.cs | 100 +++++++++++++++++++++++ SharpAdbClient/SharpAdbClient.csproj | 4 + 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 SharpAdbClient/InteractiveShell/Shell.cs diff --git a/SharpAdbClient/AdbSocket.cs b/SharpAdbClient/AdbSocket.cs index 73403e92..0765dd8b 100644 --- a/SharpAdbClient/AdbSocket.cs +++ b/SharpAdbClient/AdbSocket.cs @@ -402,7 +402,7 @@ public Stream GetShellStream() /// /// This uses the default time out value. /// - protected bool Write(byte[] data) + internal bool Write(byte[] data) { try { diff --git a/SharpAdbClient/InteractiveShell/Shell.cs b/SharpAdbClient/InteractiveShell/Shell.cs new file mode 100644 index 00000000..a1f0701b --- /dev/null +++ b/SharpAdbClient/InteractiveShell/Shell.cs @@ -0,0 +1,100 @@ +using SharpAdbClient.Exceptions; +using SharpAdbClient.Logs; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace SharpAdbClient.InteractiveShell +{ + public delegate Boolean ShellResponseEventHandler(String adbResponse, ShellResponseEventArgs args); + + public class Shell + { + public event ShellResponseEventHandler ResponseAdb = null; + + public AdbClient AdbClient { get; set;} + + public Shell(AdbClient adbClient) + { + this.AdbClient = adbClient; + } + + public async Task ExecuteRemoteCommandAsync(string command, DeviceData device, CancellationToken cancellationToken, int maxTimeToOutputResponse) + { + using (AdbSocket adbSocket = new AdbSocket(this.AdbClient.EndPoint)) + { + cancellationToken.Register(() => adbSocket.Dispose()); + this.AdbClient.SetDevice(adbSocket, device); + adbSocket.SendAdbRequest($"shell:{command}"); + var response = adbSocket.ReadAdbResponse(); + + ShellResponseEventArgs shellResponseEventArgs = new ShellResponseEventArgs(this); + shellResponseEventArgs.LastCommand = command; + + try + { + var shellStream = (adbSocket.GetShellStream() as ShellStream); + + using (StreamReader reader = new StreamReader(shellStream.Inner, AdbClient.Encoding)) + { + while (!cancellationToken.IsCancellationRequested && shellResponseEventArgs.CloseShell == false) + { + var line = await reader.ReadLineAsync().ConfigureAwait(false); + + if (ResponseAdb(line, shellResponseEventArgs)) + { + if (String.IsNullOrEmpty(shellResponseEventArgs.NextCommand) == false) + { + byte[] bytes = FormAdbNextRequest(shellResponseEventArgs.NextCommand); + + (shellStream.Inner as System.Net.Sockets.NetworkStream).Write(bytes, 0, bytes.Length); + (shellStream.Inner as System.Net.Sockets.NetworkStream).Flush(); + shellResponseEventArgs.LastCommand = shellResponseEventArgs.NextCommand; + shellResponseEventArgs.NextCommand = null; + } + } + + + } + } + } + catch (Exception e) + { + if (!cancellationToken.IsCancellationRequested) + { + throw new ShellCommandUnresponsiveException(e); + } + } + finally + { + + } + } + + } + + public static byte[] FormAdbNextRequest(string req) + { + byte[] result = AdbClient.Encoding.GetBytes(req + "\n"); + return result; + } + } + + + public class ShellResponseEventArgs : EventArgs + { + public Shell Shell { get; private set; } + public String LastCommand { get; internal set; } + public String NextCommand { get; set; } + public Boolean CloseShell { get; set; } = false; + + public ShellResponseEventArgs(Shell shell) + { + this.Shell = shell; + } + } +} + diff --git a/SharpAdbClient/SharpAdbClient.csproj b/SharpAdbClient/SharpAdbClient.csproj index a956c5c1..6145c509 100644 --- a/SharpAdbClient/SharpAdbClient.csproj +++ b/SharpAdbClient/SharpAdbClient.csproj @@ -44,6 +44,10 @@ + + + + From 20db2bc65849c13d0e682937eda78ceaedcb4ae7 Mon Sep 17 00:00:00 2001 From: Marco Giovanni Date: Thu, 26 Apr 2018 17:45:06 -0300 Subject: [PATCH 2/3] Fixxed --- SharpAdbClient/SharpAdbClient.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/SharpAdbClient/SharpAdbClient.csproj b/SharpAdbClient/SharpAdbClient.csproj index 6145c509..a956c5c1 100644 --- a/SharpAdbClient/SharpAdbClient.csproj +++ b/SharpAdbClient/SharpAdbClient.csproj @@ -44,10 +44,6 @@ - - - - From a9fc9318c2e57d3000284707dbeb21aa199aa22a Mon Sep 17 00:00:00 2001 From: Marco Giovanni Date: Mon, 30 Apr 2018 17:17:53 -0300 Subject: [PATCH 3/3] Temp --- SharpAdbClient/InteractiveShell/Shell.cs | 57 ++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/SharpAdbClient/InteractiveShell/Shell.cs b/SharpAdbClient/InteractiveShell/Shell.cs index a1f0701b..8fddf30b 100644 --- a/SharpAdbClient/InteractiveShell/Shell.cs +++ b/SharpAdbClient/InteractiveShell/Shell.cs @@ -22,6 +22,7 @@ public Shell(AdbClient adbClient) this.AdbClient = adbClient; } + //alterar depois public async Task ExecuteRemoteCommandAsync(string command, DeviceData device, CancellationToken cancellationToken, int maxTimeToOutputResponse) { using (AdbSocket adbSocket = new AdbSocket(this.AdbClient.EndPoint)) @@ -33,7 +34,6 @@ public async Task ExecuteRemoteCommandAsync(string command, DeviceData device, C ShellResponseEventArgs shellResponseEventArgs = new ShellResponseEventArgs(this); shellResponseEventArgs.LastCommand = command; - try { var shellStream = (adbSocket.GetShellStream() as ShellStream); @@ -56,8 +56,6 @@ public async Task ExecuteRemoteCommandAsync(string command, DeviceData device, C shellResponseEventArgs.NextCommand = null; } } - - } } } @@ -76,6 +74,59 @@ public async Task ExecuteRemoteCommandAsync(string command, DeviceData device, C } + + public void ExecuteRemoteCommand(string command, DeviceData device, CancellationToken cancellationToken, int maxTimeToOutputResponse) + { + //problems invoke event in control + //ExecuteRemoteCommandAsync(command, device, cancellationToken, maxTimeToOutputResponse).Wait(); + using (AdbSocket adbSocket = new AdbSocket(this.AdbClient.EndPoint)) + { + cancellationToken.Register(() => adbSocket.Dispose()); + this.AdbClient.SetDevice(adbSocket, device); + adbSocket.SendAdbRequest($"shell:{command}"); + var response = adbSocket.ReadAdbResponse(); + + ShellResponseEventArgs shellResponseEventArgs = new ShellResponseEventArgs(this); + shellResponseEventArgs.LastCommand = command; + try + { + var shellStream = (adbSocket.GetShellStream() as ShellStream); + + using (StreamReader reader = new StreamReader(shellStream.Inner, AdbClient.Encoding)) + { + while (!cancellationToken.IsCancellationRequested && shellResponseEventArgs.CloseShell == false) + { + var line = reader.ReadLine(); + + if (ResponseAdb(line, shellResponseEventArgs)) + { + if (String.IsNullOrEmpty(shellResponseEventArgs.NextCommand) == false) + { + byte[] bytes = FormAdbNextRequest(shellResponseEventArgs.NextCommand); + + (shellStream.Inner as System.Net.Sockets.NetworkStream).Write(bytes, 0, bytes.Length); + (shellStream.Inner as System.Net.Sockets.NetworkStream).Flush(); + shellResponseEventArgs.LastCommand = shellResponseEventArgs.NextCommand; + shellResponseEventArgs.NextCommand = null; + } + } + } + } + } + catch (Exception e) + { + if (!cancellationToken.IsCancellationRequested) + { + throw new ShellCommandUnresponsiveException(e); + } + } + finally + { + + } + } + } + public static byte[] FormAdbNextRequest(string req) { byte[] result = AdbClient.Encoding.GetBytes(req + "\n");