Skip to content

Commit

Permalink
resolve some more suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
artehe committed Jan 11, 2024
1 parent fbe18cd commit bf81219
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions Netimobiledevice/Backup/BackupResultEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class BackupResultEventArgs : EventArgs
public BackupResultEventArgs(IEnumerable<BackupFile> failedFiles, bool userCancelled, bool deviceDisconnected)
{
UserCancelled = userCancelled;
DeviceDisconnected = deviceDisconnected;
TransferErrors = new List<BackupFile>(failedFiles);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Netimobiledevice/EndianBitConversion/BigEndianBitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// </summary>
internal class BigEndianBitConverter : EndianBitConverter
{
public override bool IsLittleEndian { get; } = false;
public override bool IsLittleEndian { get; }

// Instance available from EndianBitConverter.BigEndian
internal BigEndianBitConverter() { }
Expand All @@ -31,21 +31,21 @@ public override byte[] GetBytes(long value)

public override short ToInt16(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, sizeof(short));
CheckArguments(value, startIndex, sizeof(short));

return (short) ((value[startIndex] << 8) | (value[startIndex + 1]));
}

public override int ToInt32(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, sizeof(int));
CheckArguments(value, startIndex, sizeof(int));

return (value[startIndex] << 24) | (value[startIndex + 1] << 16) | (value[startIndex + 2] << 8) | (value[startIndex + 3]);
}

public override long ToInt64(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, sizeof(long));
CheckArguments(value, startIndex, sizeof(long));

int highBytes = (value[startIndex] << 24) | (value[startIndex + 1] << 16) | (value[startIndex + 2] << 8) | (value[startIndex + 3]);
int lowBytes = (value[startIndex + 4] << 24) | (value[startIndex + 5] << 16) | (value[startIndex + 6] << 8) | (value[startIndex + 7]);
Expand Down
11 changes: 4 additions & 7 deletions Netimobiledevice/EndianBitConversion/EndianBitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal abstract class EndianBitConverter
/// <param name="value">A Boolean value.</param>
/// <returns>A byte array with length 1.</returns>
/// <remarks>You can convert a byte array back to a <see cref="Boolean"/> value by calling the <see cref="ToBoolean(byte[], int)"/> method.</remarks>
public byte[] GetBytes(bool value)
public virtual byte[] GetBytes(bool value)
{
return new byte[] { value ? (byte) 1 : (byte) 0 };
}
Expand Down Expand Up @@ -144,10 +144,9 @@ public byte[] GetBytes(ulong value)
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> is less than zero or greater than the length of <paramref name="value"/> minus 1.
/// </exception>
public bool ToBoolean(byte[] value, int startIndex)
public virtual bool ToBoolean(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, 1);

CheckArguments(value, startIndex, 1);
return value[startIndex] != 0;
}

Expand Down Expand Up @@ -315,10 +314,8 @@ public ulong ToUInt64(byte[] value, int startIndex)
}

// Testing showed that this method wasn't automatically being inlined, and doing so offers a significant performance improvement.
#if !NET40
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal void CheckArguments(byte[] value, int startIndex, int byteLength)
internal static void CheckArguments(byte[] value, int startIndex, int byteLength)
{
if (value == null) {
throw new ArgumentNullException(nameof(value));
Expand Down
11 changes: 4 additions & 7 deletions Netimobiledevice/EndianBitConversion/LittleEndianBitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,22 @@ public override byte[] GetBytes(long value)

public override short ToInt16(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, sizeof(short));

CheckArguments(value, startIndex, sizeof(short));
return (short) ((value[startIndex]) | (value[startIndex + 1] << 8));
}

public override int ToInt32(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, sizeof(int));

CheckArguments(value, startIndex, sizeof(int));
return (value[startIndex]) | (value[startIndex + 1] << 8) | (value[startIndex + 2] << 16) | (value[startIndex + 3] << 24);
}

public override long ToInt64(byte[] value, int startIndex)
{
this.CheckArguments(value, startIndex, sizeof(long));

CheckArguments(value, startIndex, sizeof(long));
int lowBytes = (value[startIndex]) | (value[startIndex + 1] << 8) | (value[startIndex + 2] << 16) | (value[startIndex + 3] << 24);
int highBytes = (value[startIndex + 4]) | (value[startIndex + 5] << 8) | (value[startIndex + 6] << 16) | (value[startIndex + 7] << 24);
return ((uint) lowBytes | ((long) highBytes << 32));
return (uint) lowBytes | ((long) highBytes << 32);
}
}
}
10 changes: 5 additions & 5 deletions Netimobiledevice/EndianBitConversion/SingleConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Netimobiledevice.EndianBitConversion
// Converts between Single (float) and Int32 (int), as System.BitConverter does not have a method to do this in all .NET versions.
// A union is used instead of an unsafe pointer cast so we don't have to worry about the trusted environment implications.
[StructLayout(LayoutKind.Explicit)]
internal struct SingleConverter
internal readonly struct SingleConverter
{
// map int value to offset zero
[FieldOffset(0)]
private int intValue;
private readonly int intValue;

// map float value to offset zero - intValue and floatValue now take the same position in memory
[FieldOffset(0)]
private float floatValue;
private readonly float floatValue;

internal SingleConverter(int intValue)
{
Expand All @@ -27,8 +27,8 @@ internal SingleConverter(float floatValue)
this.floatValue = floatValue;
}

internal int GetIntValue() => this.intValue;
internal readonly int GetIntValue() => this.intValue;

internal float GetFloatValue() => this.floatValue;
internal readonly float GetFloatValue() => this.floatValue;
}
}
2 changes: 1 addition & 1 deletion Netimobiledevice/Lockdown/ServiceConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public async Task<byte[]> ReceiveAsync(int length, CancellationToken cancellatio
bytesRead = await result;
}
else {
bytesRead = await networkStream.ReadAsync(receiveBuffer, 0, readSize, cancellationToken);
bytesRead = await networkStream.ReadAsync(receiveBuffer.AsMemory(0, readSize), cancellationToken);
}

totalBytesRead += bytesRead;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override void Dispose()

private async Task<string?> GetNotification(CancellationToken cancellationToken = default)
{
await serviceLockSemaphoreSlim.WaitAsync();
await serviceLockSemaphoreSlim.WaitAsync(cancellationToken);
try {
PropertyNode? plist = await Service.ReceivePlistAsync(cancellationToken);
if (plist != null) {
Expand Down Expand Up @@ -130,7 +130,7 @@ private async void NotificationListener_DoWork(object? sender, DoWorkEventArgs e
try {
string? notification = await GetNotification();
if (!string.IsNullOrEmpty(notification)) {
KeyValuePair<ReceivableNotification, string> receivableNotificationKeyPair = receivableNotifications.AsEnumerable().First(x => x.Value.Equals(notification));
KeyValuePair<ReceivableNotification, string> receivableNotificationKeyPair = receivableNotifications.AsEnumerable().First(x => x.Value.Equals(notification, StringComparison.Ordinal));
ReceivableNotification receivedNotification = receivableNotificationKeyPair.Key;
ReceivedNotification?.Invoke(this, new ReceivedNotificationEventArgs(receivedNotification, notification));
}
Expand Down
2 changes: 1 addition & 1 deletion Netimobiledevice/Usbmuxd/UsbmuxConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal abstract class UsbmuxConnection
/// residing inside the target device. when this happens, we can no longer send/receive control commands to
/// usbmux on same socket
/// </summary>
private bool connected = false;
private bool connected;
protected int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;

protected UsbmuxdSocket Sock { get; }
Expand Down

0 comments on commit bf81219

Please sign in to comment.