This repository was archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathAdbSocket.cs
More file actions
513 lines (443 loc) · 15.9 KB
/
AdbSocket.cs
File metadata and controls
513 lines (443 loc) · 15.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// <copyright file="AdbSocket.cs" company="The Android Open Source Project, Ryan Conrad, Quamotion">
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion. All rights reserved.
// </copyright>
namespace SharpAdbClient
{
using Exceptions;
using Logs;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// <para>
/// Implements a client for the Android Debug Bridge client-server protocol. Using the client, you
/// can send messages to and receive messages from the Android Debug Bridge.
/// </para>
/// <para>
/// The <see cref="AdbSocket"/> class implements the raw messaging protocol; that is,
/// sending and receiving messages. For interacting with the services the Android Debug
/// Bridge exposes, use the <see cref="AdbClient"/>.
/// </para>
/// <para>
/// For more information about the protocol that is implemented here, see chapter
/// II Protocol Details, section 1. Client <->Server protocol at
/// <see href="https://android.googlesource.com/platform/system/core/+/master/adb/OVERVIEW.TXT"/>.
/// </para>
/// </summary>
public class AdbSocket : IAdbSocket, IDisposable
{
/// <summary>
/// The underlying TCP socket that manages the connection with the ADB server.
/// </summary>
private readonly ITcpSocket socket;
/// <summary>
/// The logger to use when logging messages.
/// </summary>
private readonly ILogger<AdbSocket> logger;
/// <summary>
/// Initializes a new instance of the <see cref="AdbSocket"/> class.
/// </summary>
/// <param name="endPoint">
/// The <see cref="EndPoint"/> at which the Android Debug Bridge is listening
/// for clients.
/// </param>
/// <param name="logger">
/// The logger to use when logging.
/// </param>
public AdbSocket(EndPoint endPoint, ILogger<AdbSocket> logger = null)
{
this.socket = new TcpSocket();
this.socket.Connect(endPoint);
this.socket.ReceiveBufferSize = ReceiveBufferSize;
this.logger = logger ?? NullLogger<AdbSocket>.Instance;
}
/// <summary>
/// Initializes a new instance of the <see cref="AdbSocket"/> class.
/// </summary>
/// <param name="socket">
/// The <see cref="ITcpSocket"/> at which the Android Debug Bridge is listening
/// for clients.
/// </param>
public AdbSocket(ITcpSocket socket)
{
this.socket = socket;
this.logger = NullLogger<AdbSocket>.Instance;
}
/// <summary>
/// Gets or sets the size of the receive buffer
/// </summary>
public static int ReceiveBufferSize { get; set; } = 40960;
/// <summary>
/// Gets or sets the size of the write buffer.
/// </summary>
public static int WriteBufferSize { get; set; } = 1024;
/// <inheritdoc/>
public bool Connected
{
get { return this.socket.Connected; }
}
/// <summary>
/// Determines whether the specified reply is okay.
/// </summary>
/// <param name="reply">The reply.</param>
/// <returns>
/// <see langword="true"/> if the specified reply is okay; otherwise, <see langword="false"/>.
/// </returns>
public static bool IsOkay(byte[] reply)
{
return AdbClient.Encoding.GetString(reply).Equals("OKAY");
}
/// <inheritdoc/>
public virtual void Reconnect()
{
this.socket.Reconnect();
}
/// <summary>
/// Releases all resources used by the current instance of the <see cref="AdbSocket"/>
/// class.
/// </summary>
public virtual void Dispose()
{
this.socket.Dispose();
}
/// <inheritdoc/>
public virtual int Read(byte[] data)
{
return this.Read(data, data.Length);
}
/// <inheritdoc/>
public virtual Task ReadAsync(byte[] data, CancellationToken cancellationToken)
{
return this.ReadAsync(data, data.Length, cancellationToken);
}
/// <inheritdoc/>
public virtual void SendSyncRequest(SyncCommand command, string path, int permissions)
{
this.SendSyncRequest(command, $"{path},{permissions}");
}
/// <inheritdoc/>
public virtual void SendSyncRequest(SyncCommand command, string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
byte[] pathBytes = AdbClient.Encoding.GetBytes(path);
this.SendSyncRequest(command, pathBytes.Length);
this.Write(pathBytes);
}
/// <inheritdoc/>
public virtual void SendSyncRequest(SyncCommand command, int length)
{
// The message structure is:
// First four bytes: command
// Next four bytes: length of the path
// Final bytes: path
byte[] commandBytes = SyncCommandConverter.GetBytes(command);
byte[] lengthBytes = BitConverter.GetBytes(length);
if (!BitConverter.IsLittleEndian)
{
// Convert from big endian to little endian
Array.Reverse(lengthBytes);
}
this.Write(commandBytes);
this.Write(lengthBytes);
}
/// <inheritdoc/>
public virtual SyncCommand ReadSyncResponse()
{
byte[] data = new byte[4];
this.Read(data);
return SyncCommandConverter.GetCommand(data);
}
/// <inheritdoc/>
public virtual string ReadString()
{
// The first 4 bytes contain the length of the string
var reply = new byte[4];
int read = this.Read(reply);
if (read == 0)
{
// There is no data to read
return null;
}
// Convert the bytes to a hex string
string lenHex = AdbClient.Encoding.GetString(reply);
int len = int.Parse(lenHex, NumberStyles.HexNumber);
// And get the string
reply = new byte[len];
this.Read(reply);
string value = AdbClient.Encoding.GetString(reply);
return value;
}
/// <inheritdoc/>
public virtual string ReadSyncString()
{
// The first 4 bytes contain the length of the string
var reply = new byte[4];
this.Read(reply);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(reply);
}
int len = BitConverter.ToInt32(reply, 0);
// And get the string
reply = new byte[len];
this.Read(reply);
string value = AdbClient.Encoding.GetString(reply);
return value;
}
/// <inheritdoc/>
public virtual async Task<string> ReadStringAsync(CancellationToken cancellationToken)
{
// The first 4 bytes contain the length of the string
var reply = new byte[4];
await this.ReadAsync(reply, cancellationToken).ConfigureAwait(false);
// Convert the bytes to a hex string
string lenHex = AdbClient.Encoding.GetString(reply);
int len = int.Parse(lenHex, NumberStyles.HexNumber);
// And get the string
reply = new byte[len];
await this.ReadAsync(reply, cancellationToken).ConfigureAwait(false);
string value = AdbClient.Encoding.GetString(reply);
return value;
}
/// <inheritdoc/>
public virtual AdbResponse ReadAdbResponse()
{
var response = this.ReadAdbResponseInner();
if (!response.IOSuccess || !response.Okay)
{
this.socket.Dispose();
throw new AdbException($"An error occurred while reading a response from ADB: {response.Message}", response);
}
return response;
}
/// <inheritdoc/>
public virtual void SendAdbRequest(string request)
{
byte[] data = AdbClient.FormAdbRequest(request);
if (!this.Write(data))
{
throw new IOException($"Failed sending the request '{request}' to ADB");
}
}
/// <inheritdoc/>
public virtual void Send(byte[] data, int length)
{
this.Send(data, 0, length);
}
/// <inheritdoc/>
public virtual void Send(byte[] data, int offset, int length)
{
try
{
int count = this.socket.Send(data, 0, length != -1 ? length : data.Length, SocketFlags.None);
if (count < 0)
{
throw new AdbException("channel EOF");
}
}
catch (SocketException sex)
{
this.logger.LogError(sex, sex.Message);
throw;
}
}
/// <inheritdoc/>
public virtual async Task<int> ReadAsync(byte[] data, int length, CancellationToken cancellationToken)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (data.Length < length)
{
throw new ArgumentOutOfRangeException(nameof(data));
}
int count = -1;
int totalRead = 0;
while (count != 0 && totalRead < length)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
int left = length - totalRead;
int buflen = left < ReceiveBufferSize ? left : ReceiveBufferSize;
count = await this.socket.ReceiveAsync(data, totalRead, buflen, SocketFlags.None, cancellationToken).ConfigureAwait(false);
if (count < 0)
{
this.logger.LogError("read: channel EOF");
throw new AdbException("EOF");
}
else if (count == 0)
{
this.logger.LogInformation("DONE with Read");
}
else
{
totalRead += count;
}
}
catch (SocketException ex)
{
throw new AdbException($"An error occurred while receiving data from the adb server: {ex.Message}.", ex);
}
}
return totalRead;
}
/// <inheritdoc/>
public virtual int Read(byte[] data, int length)
{
int expLen = length != -1 ? length : data.Length;
int count = -1;
int totalRead = 0;
while (count != 0 && totalRead < expLen)
{
try
{
int left = expLen - totalRead;
int buflen = left < ReceiveBufferSize ? left : ReceiveBufferSize;
byte[] buffer = new byte[buflen];
count = this.socket.Receive(buffer, buflen, SocketFlags.None);
if (count < 0)
{
this.logger.LogError("read: channel EOF");
throw new AdbException("EOF");
}
else if (count == 0)
{
this.logger.LogInformation("DONE with Read");
}
else
{
Array.Copy(buffer, 0, data, totalRead, count);
totalRead += count;
}
}
catch (SocketException sex)
{
throw new AdbException(string.Format("No Data to read: {0}", sex.Message));
}
}
return totalRead;
}
/// <inheritdoc/>
public void SetDevice(DeviceData device)
{
// if the device is not null, then we first tell adb we're looking to talk
// to a specific device
if (device != null)
{
this.SendAdbRequest($"host:transport:{device.Serial}");
try
{
var response = this.ReadAdbResponse();
}
catch (AdbException e)
{
if (string.Equals("device not found", e.AdbError, StringComparison.OrdinalIgnoreCase))
{
throw new DeviceNotFoundException(device.Serial);
}
else
{
throw;
}
}
}
}
/// <inheritdoc/>
public Stream GetShellStream()
{
var stream = this.socket.GetStream();
return new ShellStream(stream, closeStream: true);
}
/// <summary>
/// Write until all data in "data" is written or the connection fails or times out.
/// </summary>
/// <param name="data">The data to send.</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 bool Write(byte[] data)
{
try
{
this.Send(data, -1);
}
catch (IOException e)
{
this.logger.LogError(e, e.Message);
return false;
}
return true;
}
/// <summary>
/// Reads the response from ADB after a command.
/// </summary>
/// <returns>
/// A <see cref="AdbResponse"/> that represents the response received from ADB.
/// </returns>
protected AdbResponse ReadAdbResponseInner()
{
AdbResponse resp = new AdbResponse();
byte[] reply = new byte[4];
this.Read(reply);
resp.IOSuccess = true;
if (IsOkay(reply))
{
resp.Okay = true;
}
else
{
resp.Okay = false;
}
if (!resp.Okay)
{
var message = this.ReadString();
resp.Message = message;
this.logger.LogError("Got reply '{0}', diag='{1}'", this.ReplyToString(reply), resp.Message);
}
return resp;
}
/// <summary>
/// Converts an ADB reply to a string.
/// </summary>
/// <param name="reply">
/// A <see cref="byte"/> array that represents the ADB reply.
/// </param>
/// <returns>
/// A <see cref="string"/> that represents the ADB reply.
/// </returns>
protected string ReplyToString(byte[] reply)
{
string result;
try
{
result = Encoding.ASCII.GetString(reply);
}
catch (DecoderFallbackException uee)
{
this.logger.LogError(uee, uee.Message);
result = string.Empty;
}
return result;
}
}
}