Repository navigation
Expand file tree
/
Copy pathAdbServer.Async.cs
More file actions
134 lines (122 loc) · 5.78 KB
/
Copy pathAdbServer.Async.cs
File metadata and controls
134 lines (122 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#if HAS_TASK
// <copyright file="AdbServer.Async.cs" company="The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere">
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved.
// </copyright>
using System;
using System.Globalization;
using System.Net.Sockets;
using System.Threading;
namespace AdvancedSharpAdbClient
{
public partial class AdbServer
{
/// <inheritdoc/>
public async Task<StartServerResult> StartServerAsync(string adbPath, bool restartServerIfNewer = false, CancellationToken cancellationToken = default)
{
if (IsStarting) { return StartServerResult.Starting; }
try
{
AdbServerStatus serverStatus = await GetStatusAsync(cancellationToken).ConfigureAwait(false);
Version? commandLineVersion = null;
IAdbCommandLineClient commandLineClient = AdbCommandLineClientFactory(adbPath);
if (await commandLineClient.CheckAdbFileExistsAsync(adbPath, cancellationToken).ConfigureAwait(false))
{
CachedAdbPath = adbPath;
commandLineVersion = await commandLineClient.GetVersionAsync(cancellationToken).ContinueWith(x => x.Result.AdbVersion).ConfigureAwait(false);
}
// If the server is running, and no adb path is provided, check if we have the minimum version
if (adbPath == null)
{
return !serverStatus.IsRunning
? throw new AdbException("The adb server is not running, but no valid path to the adb.exe executable was provided. The adb server cannot be started.")
: serverStatus.Version >= RequiredAdbVersion
? StartServerResult.AlreadyRunning
: throw new AdbException($"The adb daemon is running an outdated version ${commandLineVersion}, but not valid path to the adb.exe executable was provided. A more recent version of the adb server cannot be started.");
}
if (serverStatus.IsRunning)
{
if (serverStatus.Version < RequiredAdbVersion
|| (restartServerIfNewer && serverStatus.Version < commandLineVersion))
{
await StopServerAsync(cancellationToken);
await commandLineClient.StartServerAsync(cancellationToken);
return StartServerResult.RestartedOutdatedDaemon;
}
else
{
return StartServerResult.AlreadyRunning;
}
}
else
{
await commandLineClient.StartServerAsync(cancellationToken);
return StartServerResult.Started;
}
}
finally
{
IsStarting = false;
}
}
/// <inheritdoc/>
public Task<StartServerResult> RestartServerAsync(CancellationToken cancellationToken = default) =>
StartServerAsync(CachedAdbPath!, true, cancellationToken);
/// <inheritdoc/>
public Task<StartServerResult> RestartServerAsync(string adbPath, CancellationToken cancellationToken = default) =>
string.IsNullOrWhiteSpace(adbPath) ? RestartServerAsync(cancellationToken) : StartServerAsync(adbPath, true, cancellationToken);
/// <inheritdoc/>
public async Task StopServerAsync(CancellationToken cancellationToken = default)
{
using IAdbSocket socket = CreateAdbSocket();
await socket.SendAdbRequestAsync("host:kill", cancellationToken).ConfigureAwait(false);
// The host will immediately close the connection after the kill
// command has been sent; no need to read the response.
}
/// <inheritdoc/>
public async Task<AdbServerStatus> GetStatusAsync(CancellationToken cancellationToken = default)
{
// Try to connect to a running instance of the adb server
try
{
using IAdbSocket socket = CreateAdbSocket();
await socket.SendAdbRequestAsync("host:version", cancellationToken).ConfigureAwait(false);
AdbResponse response = await socket.ReadAdbResponseAsync(cancellationToken).ConfigureAwait(false);
string version = await socket.ReadStringAsync(cancellationToken).ConfigureAwait(false);
int versionCode = int.Parse(version, NumberStyles.HexNumber);
return new AdbServerStatus(true, new Version(1, 0, versionCode));
}
catch (AggregateException ex)
{
if (ex.InnerException is SocketException exception)
{
if (exception.SocketErrorCode == SocketError.ConnectionRefused)
{
return new AdbServerStatus(false, null);
}
else
{
// An unexpected exception occurred; re-throw the exception
throw exception;
}
}
else
{
throw;
}
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.ConnectionRefused)
{
return new AdbServerStatus(false, null);
}
else
{
// An unexpected exception occurred; re-throw the exception
throw;
}
}
}
}
}
#endif