Repository navigation
Expand file tree
/
Copy pathAdbServer.cs
More file actions
294 lines (258 loc) · 14.5 KB
/
Copy pathAdbServer.cs
File metadata and controls
294 lines (258 loc) · 14.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// <copyright file="AdbServer.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.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace AdvancedSharpAdbClient
{
/// <summary>
/// <para>Provides methods for interacting with the adb server. The adb server must be running for
/// the rest of the <c>Managed.Adb</c> library to work.</para>
/// <para>The adb server is a background process that runs on the host machine.
/// Its purpose if to sense the USB ports to know when devices are attached/removed,
/// as well as when emulator instances start/stop. The ADB server is really one
/// giant multiplexing loop whose purpose is to orchestrate the exchange of data
/// between clients and devices.</para>
/// </summary>
[DebuggerDisplay($"{{{nameof(GetType)}().{nameof(Type.ToString)}(),nq}} \\{{ {nameof(EndPoint)} = {{{nameof(EndPoint)}}}, {nameof(CachedAdbPath)} = {{{nameof(CachedAdbPath)}}} }}")]
public partial class AdbServer : IAdbServer, IInternalCloneable<AdbServer>
{
/// <summary>
/// The minimum version of <c>adb.exe</c> that is supported by this library.
/// </summary>
public static readonly Version RequiredAdbVersion = new(1, 0, 20);
/// <summary>
/// The error code that is returned by the <see cref="SocketException"/> when the connection is refused.
/// </summary>
/// <remarks>No connection could be made because the target computer actively refused it.This usually
/// results from trying to connect to a service that is inactive on the foreign host—that is,
/// one with no server application running. <seealso href="https://msdn.microsoft.com/en-us/library/ms740668.aspx"/></remarks>
public const int ConnectionRefused = 10061;
/// <summary>
/// The error code that is returned by the <see cref="SocketException"/> when the connection was reset by the peer.
/// </summary>
/// <remarks>An existing connection was forcibly closed by the remote host. This normally results if the peer application on the
/// remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote
/// host uses a hard close. This error may also result if a connection was broken due to keep-alive activity detecting
/// a failure while one or more operations are in progress. <seealso href="https://msdn.microsoft.com/en-us/library/ms740668.aspx"/></remarks>
public const int ConnectionReset = 10054;
/// <summary>
/// The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.
/// </summary>
protected readonly Func<EndPoint, IAdbSocket> AdbSocketFactory;
/// <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>
protected readonly Func<string, IAdbCommandLineClient> AdbCommandLineClientFactory;
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
public AdbServer()
: this(AdbClient.AdbServerEndPoint, Factories.AdbSocketFactory, Factories.AdbCommandLineClientFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="adbClient">A connection to an adb server.</param>
public AdbServer(IAdbClient adbClient)
: this(adbClient.EndPoint, Factories.AdbSocketFactory, Factories.AdbCommandLineClientFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="endPoint">The <see cref="System.Net.EndPoint"/> at which the adb server is listening.</param>
public AdbServer(EndPoint endPoint)
: this(endPoint, Factories.AdbSocketFactory, Factories.AdbCommandLineClientFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="host">The host address at which the adb server is listening.</param>
/// <param name="port">The port at which the adb server is listening.</param>
public AdbServer(string host, int port)
: this(Extensions.CreateDnsEndPoint(host, port), Factories.AdbSocketFactory, Factories.AdbCommandLineClientFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="adbClient">A connection to an adb server.</param>
/// <param name="adbSocketFactory">The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.</param>
/// <param name="adbCommandLineClientFactory">The <see cref="Func{String, IAdbCommandLineClient}"/> to create <see cref="IAdbCommandLineClient"/>.</param>
public AdbServer(IAdbClient adbClient, Func<EndPoint, IAdbSocket> adbSocketFactory, Func<string, IAdbCommandLineClient> adbCommandLineClientFactory)
: this(adbClient.EndPoint, adbSocketFactory, adbCommandLineClientFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="endPoint">The <see cref="System.Net.EndPoint"/> at which the adb server is listening.</param>
/// <param name="adbSocketFactory">The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.</param>
/// <param name="adbCommandLineClientFactory">The <see cref="Func{String, IAdbCommandLineClient}"/> to create <see cref="IAdbCommandLineClient"/>.</param>
public AdbServer(EndPoint endPoint, Func<EndPoint, IAdbSocket> adbSocketFactory, Func<string, IAdbCommandLineClient> adbCommandLineClientFactory)
{
ArgumentNullException.ThrowIfNull(endPoint);
if (endPoint is not (IPEndPoint or DnsEndPoint))
{
throw new NotSupportedException("Only TCP endpoints are supported");
}
EndPoint = endPoint;
AdbSocketFactory = adbSocketFactory ?? throw new ArgumentNullException(nameof(adbSocketFactory));
AdbCommandLineClientFactory = adbCommandLineClientFactory ?? throw new ArgumentNullException(nameof(adbCommandLineClientFactory));
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="host">The host address at which the adb server is listening.</param>
/// <param name="port">The port at which the adb server is listening.</param>
/// <param name="adbSocketFactory">The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.</param>
/// <param name="adbCommandLineClientFactory">The <see cref="Func{String, IAdbCommandLineClient}"/> to create <see cref="IAdbCommandLineClient"/>.</param>
public AdbServer(string host, int port, Func<EndPoint, IAdbSocket> adbSocketFactory, Func<string, IAdbCommandLineClient> adbCommandLineClientFactory)
: this(Extensions.CreateDnsEndPoint(host, port), adbSocketFactory, adbCommandLineClientFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbServer"/> class.
/// </summary>
/// <param name="adbSocketFactory">The <see cref="Func{EndPoint, IAdbSocket}"/> to create <see cref="IAdbSocket"/>.</param>
/// <param name="adbCommandLineClientFactory">The <see cref="Func{String, IAdbCommandLineClient}"/> to create <see cref="IAdbCommandLineClient"/>.</param>
public AdbServer(Func<EndPoint, IAdbSocket> adbSocketFactory, Func<string, IAdbCommandLineClient> adbCommandLineClientFactory)
: this(AdbClient.AdbServerEndPoint, adbSocketFactory, adbCommandLineClientFactory)
{
}
/// <summary>
/// Gets or sets the default instance of the <see cref="IAdbServer"/> interface.
/// </summary>
public static IAdbServer Instance
{
get => field ??= new AdbServer();
set;
}
/// <summary>
/// <see langword="true"/> if is starting adb server; otherwise, <see langword="false"/>.
/// </summary>
protected static bool IsStarting { get; set; }
/// <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>
protected string? CachedAdbPath { get; set; }
/// <summary>
/// Gets the <see cref="System.Net.EndPoint"/> at which the adb server is listening.
/// </summary>
public EndPoint EndPoint { get; init; }
/// <inheritdoc/>
public StartServerResult StartServer(string adbPath, bool restartServerIfNewer = false)
{
if (IsStarting) { return StartServerResult.Starting; }
try
{
AdbServerStatus serverStatus = GetStatus();
Version? commandLineVersion = null;
IAdbCommandLineClient commandLineClient = AdbCommandLineClientFactory(adbPath);
if (commandLineClient.CheckAdbFileExists(adbPath))
{
CachedAdbPath = adbPath;
commandLineVersion = commandLineClient.GetVersion().AdbVersion;
}
// 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))
{
StopServer();
commandLineClient.StartServer(Timeout.Infinite);
return StartServerResult.RestartedOutdatedDaemon;
}
else
{
return StartServerResult.AlreadyRunning;
}
}
else
{
commandLineClient.StartServer(Timeout.Infinite);
return StartServerResult.Started;
}
}
finally
{
IsStarting = false;
}
}
/// <inheritdoc/>
public StartServerResult RestartServer() => StartServer(CachedAdbPath!, true);
/// <inheritdoc/>
public StartServerResult RestartServer(string adbPath) =>
string.IsNullOrWhiteSpace(adbPath) ? RestartServer() : StartServer(adbPath, true);
/// <inheritdoc/>
public void StopServer()
{
using IAdbSocket socket = CreateAdbSocket();
socket.SendAdbRequest("host:kill");
// The host will immediately close the connection after the kill
// command has been sent; no need to read the response.
}
/// <inheritdoc/>
public AdbServerStatus GetStatus()
{
// Try to connect to a running instance of the adb server
try
{
using IAdbSocket socket = CreateAdbSocket();
socket.SendAdbRequest("host:version");
AdbResponse response = socket.ReadAdbResponse();
string version = socket.ReadString();
int versionCode = int.Parse(version, NumberStyles.HexNumber);
return new AdbServerStatus(true, new Version(1, 0, versionCode));
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.ConnectionRefused)
{
return new AdbServerStatus(false, null);
}
else
{
// An unexpected exception occurred; re-throw the exception
throw;
}
}
}
/// <summary>
/// Creates a new <see cref="AdbClient"/> object with the specified <see cref="EndPoint"/>.
/// </summary>
/// <returns>A new <see cref="AdbClient"/> object with the specified <see cref="EndPoint"/>.</returns>
public IAdbSocket CreateAdbSocket() => AdbSocketFactory(EndPoint);
/// <inheritdoc/>
public override string ToString() => $"The {GetType()} communicate with adb at '{EndPoint}'.";
/// <summary>
/// Creates a new <see cref="AdbServer"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
/// </summary>
/// <param name="endPoint">The new <see cref="EndPoint"/> to use.</param>
/// <returns>A new <see cref="AdbServer"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
public virtual AdbServer Clone(EndPoint endPoint) => new(endPoint, AdbSocketFactory, AdbCommandLineClientFactory);
/// <inheritdoc/>
public AdbServer Clone() => Clone(EndPoint);
/// <inheritdoc/>
object ICloneable.Clone() => Clone(EndPoint);
}
}