Skip to content

Commit a2c39e9

Browse files
authored
[DhcpServer] Fixes and major improvements (#1424)
1 parent bf88de6 commit a2c39e9

23 files changed

+1501
-419
lines changed

devices/DhcpServer/Converter.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Net;
6+
using System.Text;
7+
8+
namespace Iot.Device.DhcpServer
9+
{
10+
internal class Converter
11+
{
12+
/// <summary>
13+
/// Copies <paramref name="source"/> to <paramref name="destination"/> starting at <paramref name="destinationIndex"/>.
14+
/// </summary>
15+
/// <param name="source">The source data.</param>
16+
/// <param name="destination">The destination array.</param>
17+
/// <param name="destinationIndex">The destinations index.</param>
18+
/// <returns>The number of bytes copied.</returns>
19+
public static int CopyTo(byte source, byte[] destination, int destinationIndex)
20+
{
21+
destination[destinationIndex] = source;
22+
return 1;
23+
}
24+
25+
/// <inheritdoc cref="CopyTo(byte,byte[],int)"/>
26+
public static int CopyTo(byte[] source, byte[] destination, int destinationIndex)
27+
{
28+
if (destination.Length - destinationIndex < source.Length)
29+
{
30+
throw new ArgumentException();
31+
}
32+
33+
return CopyTo(source, 0, destination, destinationIndex, source.Length);
34+
}
35+
36+
/// <inheritdoc cref="CopyTo(byte,byte[],int)"/>
37+
public static int CopyTo(IPAddress source, byte[] destination, int destinationIndex) => CopyTo(GetBytes(source), destination, destinationIndex);
38+
39+
/// <inheritdoc cref="CopyTo(byte,byte[],int)"/>
40+
public static int CopyTo(uint source, byte[] destination, int destinationIndex) => CopyTo(GetBytes(source), destination, destinationIndex);
41+
42+
/// <inheritdoc cref="CopyTo(byte,byte[],int)"/>
43+
public static int CopyTo(ushort source, byte[] destination, int destinationIndex) => CopyTo(GetBytes(source), destination, destinationIndex);
44+
45+
public static int CopyTo(byte[] source, int sourceIndex, byte[] destination, int destinationIndex, int length)
46+
{
47+
Array.Copy(source, sourceIndex, destination, destinationIndex, length);
48+
return length;
49+
}
50+
51+
public static byte[] GetBytes(IPAddress value) => value.GetAddressBytes();
52+
53+
public static byte[] GetBytes(string value) => Encoding.UTF8.GetBytes(value);
54+
55+
public static byte[] GetBytes(TimeSpan value) => GetBytes(value > TimeSpan.Zero ? (uint)value.TotalSeconds : 0);
56+
57+
public static byte[] GetBytes(uint value) => BitConverter.GetBytes(value);
58+
59+
public static byte[] GetBytes(ushort value) => BitConverter.GetBytes(value);
60+
61+
public static IPAddress GetIPAddress(byte[] data, int index = 0) => new (GetUInt32(data, index));
62+
63+
public static string GetString(byte[] data, int index = 0, int length = -1) => Encoding.UTF8.GetString(data, index, length > -1 ? length : data.Length);
64+
65+
public static TimeSpan GetTimeSpan(byte[] data, int index = 0) => TimeSpan.FromSeconds(GetUInt32(data, index));
66+
67+
public static ushort GetUInt16(byte[] data, int index = 0) => BitConverter.ToUInt16(data, index);
68+
69+
public static uint GetUInt32(byte[] data, int index = 0) => BitConverter.ToUInt32(data, index);
70+
}
71+
}

0 commit comments

Comments
 (0)