Skip to content

Commit

Permalink
Merge pull request #67 from yungd1plomat/improve/virtualmember
Browse files Browse the repository at this point in the history
Add more virtual members to be override-able
  • Loading branch information
wherewhere authored Aug 14, 2023
2 parents 6ca02c3 + b51b876 commit 258564b
Show file tree
Hide file tree
Showing 21 changed files with 303 additions and 188 deletions.
30 changes: 2 additions & 28 deletions AdvancedSharpAdbClient.Tests/Dummys/TracingAdbSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,14 @@ public TracingAdbSocket(EndPoint endPoint) : base(endPoint)

public bool WaitForNewData { get; set; }

public override void Dispose()
protected override void Dispose(bool disposing)
{
if (DoDispose)
{
base.Dispose();
base.Dispose(disposing);
}
}

public override int Read(byte[] data)
{
StackTrace trace = new();

int read = base.Read(data);

if (trace != null && trace.GetFrames()[1].GetMethod().DeclaringType != typeof(AdbSocket))
{
SyncDataReceived.Enqueue(data);
}

return read;
}

public override int Read(byte[] data, int length)
{
StackTrace trace = new();
Expand Down Expand Up @@ -146,18 +132,6 @@ public override SyncCommand ReadSyncResponse()
return response;
}

public override void Send(byte[] data, int length)
{
StackTrace trace = new();

base.Send(data, length);

if (trace != null && trace.GetFrames()[1].GetMethod().DeclaringType != typeof(AdbSocket))
{
SyncDataSent.Enqueue(data.Take(length).ToArray());
}
}

public override void Reconnect()
{
base.Reconnect();
Expand Down
7 changes: 5 additions & 2 deletions AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public partial class AdbClient : IAdbClient
[Obsolete("This function has been removed since SharpAdbClient. Here is a placeholder which function is gets a new instance instead of gets or sets the default instance.")]
public static IAdbClient Instance => new AdbClient();

private readonly Func<EndPoint, IAdbSocket> adbSocketFactory;
/// <summary>
/// The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.
/// </summary>
protected readonly Func<EndPoint, IAdbSocket> adbSocketFactory;

/// <summary>
/// Initializes a new instance of the <see cref="AdbClient"/> class.
Expand Down Expand Up @@ -131,7 +134,7 @@ public AdbClient(EndPoint endPoint, Func<EndPoint, IAdbSocket> adbSocketFactory)
/// <summary>
/// Gets the <see cref="System.Net.EndPoint"/> at which the adb server is listening.
/// </summary>
public EndPoint EndPoint { get; private set; }
public EndPoint EndPoint { get; protected set; }

/// <summary>
/// Create an ASCII string preceded by four hex digits. The opening "####"
Expand Down
4 changes: 2 additions & 2 deletions AdvancedSharpAdbClient/AdbCommandLineClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class AdbCommandLineClient
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
/// <returns>A <see cref="Task"/> which return a <see cref="Version"/> object that contains the version number of the Android Command Line client.</returns>
public async Task<Version> GetVersionAsync(CancellationToken cancellationToken = default)
public virtual async Task<Version> GetVersionAsync(CancellationToken cancellationToken = default)
{
// Run the adb.exe version command and capture the output.
List<string> standardOutput = new();
Expand All @@ -46,7 +46,7 @@ public async Task<Version> GetVersionAsync(CancellationToken cancellationToken =
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
public async Task StartServerAsync(CancellationToken cancellationToken = default)
public virtual async Task StartServerAsync(CancellationToken cancellationToken = default)
{
int status = await RunAdbProcessInnerAsync("start-server", null, null, cancellationToken);

Expand Down
8 changes: 4 additions & 4 deletions AdvancedSharpAdbClient/AdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public partial class AdbCommandLineClient : IAdbCommandLineClient
/// <summary>
/// The regex pattern for getting the adb version from the <c>adb version</c> command.
/// </summary>
private const string AdbVersionPattern = "^.*(\\d+)\\.(\\d+)\\.(\\d+)$";
protected const string AdbVersionPattern = "^.*(\\d+)\\.(\\d+)\\.(\\d+)$";

#if HAS_LOGGER
/// <summary>
/// The logger to use when logging messages.
/// </summary>
private readonly ILogger<AdbCommandLineClient> logger;
protected readonly ILogger<AdbCommandLineClient> logger;
#endif

#if !HAS_LOGGER
Expand Down Expand Up @@ -94,7 +94,7 @@ public AdbCommandLineClient(string adbPath, bool isForce = false
/// Queries adb for its version number and checks it against <see cref="AdbServer.RequiredAdbVersion"/>.
/// </summary>
/// <returns>A <see cref="Version"/> object that contains the version number of the Android Command Line client.</returns>
public Version GetVersion()
public virtual Version GetVersion()
{
// Run the adb.exe version command and capture the output.
List<string> standardOutput = new();
Expand All @@ -119,7 +119,7 @@ public Version GetVersion()
/// <summary>
/// Starts the adb server by running the <c>adb start-server</c> command.
/// </summary>
public void StartServer()
public virtual void StartServer()
{
int status = RunAdbProcessInner("start-server", null, null);

Expand Down
6 changes: 3 additions & 3 deletions AdvancedSharpAdbClient/AdbServer.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AdvancedSharpAdbClient
public partial class AdbServer
{
/// <inheritdoc/>
public async Task<StartServerResult> StartServerAsync(string adbPath, bool restartServerIfNewer, CancellationToken cancellationToken = default)
public virtual async Task<StartServerResult> StartServerAsync(string adbPath, bool restartServerIfNewer, CancellationToken cancellationToken = default)
{
AdbServerStatus serverStatus = await GetStatusAsync(cancellationToken);
Version commandLineVersion = null;
Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task<StartServerResult> StartServerAsync(string adbPath, bool resta
public Task<StartServerResult> RestartServerAsync(CancellationToken cancellationToken = default) => RestartServerAsync(null, cancellationToken);

/// <inheritdoc/>
public async Task<StartServerResult> RestartServerAsync(string adbPath, CancellationToken cancellationToken = default)
public virtual async Task<StartServerResult> RestartServerAsync(string adbPath, CancellationToken cancellationToken = default)
{
adbPath ??= cachedAdbPath;

Expand Down Expand Up @@ -99,7 +99,7 @@ await Utilities.Run(() =>
}

/// <inheritdoc/>
public async Task<AdbServerStatus> GetStatusAsync(CancellationToken cancellationToken = default)
public virtual async Task<AdbServerStatus> GetStatusAsync(CancellationToken cancellationToken = default)
{
// Try to connect to a running instance of the adb server
try
Expand Down
14 changes: 7 additions & 7 deletions AdvancedSharpAdbClient/AdbServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ public partial class AdbServer : IAdbServer
/// <summary>
/// A lock used to ensure only one caller at a time can attempt to restart adb.
/// </summary>
private static readonly object RestartLock = new();
protected static readonly object RestartLock = new();

/// <summary>
/// The path to the adb server. Cached from calls to <see cref="StartServer(string, bool)"/>. Used when restarting
/// the server to figure out where adb is located.
/// </summary>
private static string cachedAdbPath;
protected static string cachedAdbPath;

/// <summary>
/// The current ADB client that manages the connection.
/// </summary>
private readonly IAdbClient adbClient;
protected readonly IAdbClient adbClient;

/// <summary>
/// Gets or sets a function that returns a new instance of a class that implements the
/// <see cref="IAdbCommandLineClient"/> interface, that can be used to interact with the
/// <c>adb.exe</c> command line client.
/// </summary>
private readonly Func<string, IAdbCommandLineClient> adbCommandLineClientFactory;
protected readonly Func<string, IAdbCommandLineClient> adbCommandLineClientFactory;

/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
Expand Down Expand Up @@ -109,7 +109,7 @@ public AdbServer(IAdbClient adbClient, Func<string, IAdbCommandLineClient> adbCo
public static IAdbServer Instance { get; set; } = new AdbServer();

/// <inheritdoc/>
public StartServerResult StartServer(string adbPath, bool restartServerIfNewer)
public virtual StartServerResult StartServer(string adbPath, bool restartServerIfNewer)
{
AdbServerStatus serverStatus = GetStatus();
Version commandLineVersion = null;
Expand Down Expand Up @@ -159,7 +159,7 @@ public StartServerResult StartServer(string adbPath, bool restartServerIfNewer)
}

/// <inheritdoc/>
public StartServerResult RestartServer(string adbPath = null)
public virtual StartServerResult RestartServer(string adbPath = null)
{
adbPath ??= cachedAdbPath;

Expand All @@ -175,7 +175,7 @@ public StartServerResult RestartServer(string adbPath = null)
}

/// <inheritdoc/>
public AdbServerStatus GetStatus()
public virtual AdbServerStatus GetStatus()
{
// Try to connect to a running instance of the adb server
try
Expand Down
26 changes: 13 additions & 13 deletions AdvancedSharpAdbClient/AdbSocket.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace AdvancedSharpAdbClient
public partial class AdbSocket
{
/// <inheritdoc/>
public virtual Task SendAsync(byte[] data, int length, CancellationToken cancellationToken = default) => SendAsync(data, 0, length, cancellationToken);
public Task SendAsync(byte[] data, int length, CancellationToken cancellationToken = default) => SendAsync(data, 0, length, cancellationToken);

/// <inheritdoc/>
public virtual async Task SendAsync(byte[] data, int offset, int length, CancellationToken cancellationToken = default)
Expand All @@ -41,7 +41,7 @@ public virtual async Task SendAsync(byte[] data, int offset, int length, Cancell
}

/// <inheritdoc/>
public virtual Task SendSyncRequestAsync(SyncCommand command, string path, int permissions, CancellationToken cancellationToken = default) =>
public Task SendSyncRequestAsync(SyncCommand command, string path, int permissions, CancellationToken cancellationToken = default) =>
SendSyncRequestAsync(command, $"{path},{permissions}", cancellationToken);

/// <inheritdoc/>
Expand Down Expand Up @@ -87,7 +87,7 @@ public virtual async Task SendAdbRequestAsync(string request, CancellationToken
}

/// <inheritdoc/>
public virtual Task<int> ReadAsync(byte[] data, CancellationToken cancellationToken = default) =>
public Task<int> ReadAsync(byte[] data, CancellationToken cancellationToken = default) =>
ReadAsync(data, data.Length, cancellationToken);

/// <inheritdoc/>
Expand Down Expand Up @@ -205,7 +205,7 @@ public virtual async Task<AdbResponse> ReadAdbResponseAsync(CancellationToken ca
}

/// <inheritdoc/>
public async Task SetDeviceAsync(DeviceData device, CancellationToken cancellationToken = default)
public virtual async Task SetDeviceAsync(DeviceData device, CancellationToken cancellationToken = default)
{
// if the device is not null, then we first tell adb we're looking to talk
// to a specific device
Expand Down Expand Up @@ -238,7 +238,7 @@ public async Task SetDeviceAsync(DeviceData device, CancellationToken cancellati
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous task.</param>
/// <returns>Returns <see langword="true"/> if all data was written; otherwise, <see langword="false"/>.</returns>
/// <remarks>This uses the default time out value.</remarks>
protected async Task<bool> WriteAsync(byte[] data, CancellationToken cancellationToken = default)
protected virtual async Task<bool> WriteAsync(byte[] data, CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -263,27 +263,27 @@ protected async Task<bool> WriteAsync(byte[] data, CancellationToken cancellatio
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous task.</param>
/// <returns>A <see cref="AdbResponse"/> that represents the response received from ADB.</returns>
protected async Task<AdbResponse> ReadAdbResponseInnerAsync(CancellationToken cancellationToken = default)
protected virtual async Task<AdbResponse> ReadAdbResponseInnerAsync(CancellationToken cancellationToken = default)
{
AdbResponse resp = new();
AdbResponse rasps = new();

byte[] reply = new byte[4];
_ = await ReadAsync(reply, cancellationToken);

resp.IOSuccess = true;
rasps.IOSuccess = true;

resp.Okay = IsOkay(reply);
rasps.Okay = IsOkay(reply);

if (!resp.Okay)
if (!rasps.Okay)
{
string message = await ReadStringAsync(cancellationToken);
resp.Message = message;
rasps.Message = message;
#if HAS_LOGGER
logger.LogError($"Got reply '{ReplyToString(reply)}', diag='{resp.Message}'");
logger.LogError($"Got reply '{ReplyToString(reply)}', diag='{rasps.Message}'");
#endif
}

return resp;
return rasps;
}
}
}
Expand Down
Loading

0 comments on commit 258564b

Please sign in to comment.