Rename Client to GetClient + some code clean + Add rewrite

This commit is contained in:
valentinbreiz 2021-01-21 16:37:33 +01:00
parent 584568bceb
commit eaf05f4c57
8 changed files with 26 additions and 22 deletions

View file

@ -33,7 +33,9 @@ namespace Cosmos.HAL.Network
public MACAddress(byte[] address) public MACAddress(byte[] address)
{ {
if (address == null || address.Length != 6) if (address == null || address.Length != 6)
{
throw new ArgumentException("MACAddress is null or has wrong length", "address"); throw new ArgumentException("MACAddress is null or has wrong length", "address");
}
bytes[0] = address[0]; bytes[0] = address[0];
bytes[1] = address[1]; bytes[1] = address[1];
@ -52,7 +54,9 @@ namespace Cosmos.HAL.Network
public MACAddress(byte[] buffer, int offset) public MACAddress(byte[] buffer, int offset)
{ {
if (buffer == null || buffer.Length < (offset + 6)) if (buffer == null || buffer.Length < (offset + 6))
{
throw new ArgumentException("buffer does not contain enough data starting at offset", "buffer"); throw new ArgumentException("buffer does not contain enough data starting at offset", "buffer");
}
bytes[0] = buffer[offset]; bytes[0] = buffer[offset];
bytes[1] = buffer[offset + 1]; bytes[1] = buffer[offset + 1];
@ -95,7 +99,9 @@ namespace Cosmos.HAL.Network
return 0; return 0;
} }
else else
{
throw new ArgumentException("obj is not a MACAddress", "obj"); throw new ArgumentException("obj is not a MACAddress", "obj");
}
} }
public override bool Equals(object obj) public override bool Equals(object obj)
@ -112,17 +118,19 @@ namespace Cosmos.HAL.Network
bytes[5] == other.bytes[5]; bytes[5] == other.bytes[5];
} }
else else
{
throw new ArgumentException("obj is not a MACAddress", "obj"); throw new ArgumentException("obj is not a MACAddress", "obj");
}
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return (GetType().AssemblyQualifiedName + "|" + this.ToString()).GetHashCode(); return (GetType().AssemblyQualifiedName + "|" + ToString()).GetHashCode();
} }
public UInt64 ToNumber() public ulong ToNumber()
{ {
return (UInt64)((bytes[0] << 40) | (bytes[1] << 32) | (bytes[2] << 24) | (bytes[3] << 16) | return (ulong)((bytes[0] << 40) | (bytes[1] << 32) | (bytes[2] << 24) | (bytes[3] << 16) |
(bytes[4] << 8) | (bytes[5] << 0)); (bytes[4] << 8) | (bytes[5] << 0));
} }
@ -133,23 +141,23 @@ namespace Cosmos.HAL.Network
aChars[aIndex + 1] = xChars[aByte & 0xF]; aChars[aIndex + 1] = xChars[aByte & 0xF];
} }
public UInt32 to32BitNumber() public uint To32BitNumber()
{ {
return (UInt32)((bytes[0] << 40) | (bytes[1] << 32) | (bytes[2] << 24) | (bytes[3] << 16) | return (uint)((bytes[0] << 40) | (bytes[1] << 32) | (bytes[2] << 24) | (bytes[3] << 16) |
(bytes[4] << 8) | (bytes[5] << 0)); (bytes[4] << 8) | (bytes[5] << 0));
} }
private UInt32 hash; private uint hash;
/// <summary> /// <summary>
/// Hash value for this mac. Used to uniquely identify each mac /// Hash value for this mac. Used to uniquely identify each mac
/// </summary> /// </summary>
public UInt32 Hash public uint Hash
{ {
get get
{ {
if (hash == 0) if (hash == 0)
{ {
hash = to32BitNumber(); hash = To32BitNumber();
} }
return hash; return hash;
@ -171,7 +179,7 @@ namespace Cosmos.HAL.Network
PutByte(xChars, 12, bytes[4]); PutByte(xChars, 12, bytes[4]);
xChars[14] = ':'; xChars[14] = ':';
PutByte(xChars, 15, bytes[5]); PutByte(xChars, 15, bytes[5]);
return new String(xChars); return new string(xChars);
} }
} }
} }

View file

@ -19,7 +19,7 @@ namespace Cosmos.System.Network.ARP
/// <summary> /// <summary>
/// Cache. /// Cache.
/// </summary> /// </summary>
private static Dictionary<uint, MACAddress> cache; public static Dictionary<uint, MACAddress> cache;
/// <summary> /// <summary>
/// Ensure cache exists. /// Ensure cache exists.
@ -55,11 +55,7 @@ namespace Cosmos.System.Network.ARP
internal static void Update(IPv4.Address ipAddress, MACAddress macAddress) internal static void Update(IPv4.Address ipAddress, MACAddress macAddress)
{ {
ensureCacheExists(); ensureCacheExists();
if (ipAddress == null) uint ip_hash = ipAddress.Hash;
{
global::System.Console.Write("");
}
UInt32 ip_hash = ipAddress.Hash;
if (ip_hash == 0) if (ip_hash == 0)
{ {
return; return;

View file

@ -21,9 +21,9 @@ namespace Cosmos.System.Network.Config
/// <param name="config"></param> /// <param name="config"></param>
public static void Add(Address nameserver) public static void Add(Address nameserver)
{ {
foreach (var ns in DNSNameservers) for (int i = 0; i < DNSNameservers.Count; i++)
{ {
if (ns.address.ToString() == nameserver.address.ToString()) if (DNSNameservers[i].address.Equals(nameserver))
{ {
return; return;
} }

View file

@ -47,7 +47,7 @@ namespace Cosmos.System.Network.IPv4
/// </summary> /// </summary>
/// <param name="iphash">IP Hash.</param> /// <param name="iphash">IP Hash.</param>
/// <returns>ICMPClient</returns> /// <returns>ICMPClient</returns>
internal static ICMPClient Client(uint iphash) internal static ICMPClient GetClient(uint iphash)
{ {
if (clients.ContainsKey(iphash) == true) if (clients.ContainsKey(iphash) == true)
{ {

View file

@ -61,7 +61,7 @@ namespace Cosmos.System.Network.IPv4.UDP.DNS
{ {
DNSPacket dns_packet = new DNSPacket(packetData); DNSPacket dns_packet = new DNSPacket(packetData);
DnsClient receiver = (DnsClient)UdpClient.Client(dns_packet.DestinationPort); DnsClient receiver = (DnsClient)UdpClient.GetClient(dns_packet.DestinationPort);
if (receiver != null) if (receiver != null)
{ {
receiver.receiveData(dns_packet); receiver.receiveData(dns_packet);

View file

@ -42,7 +42,7 @@ namespace Cosmos.System.Network.IPv4
switch (icmp_packet.ICMP_Type) switch (icmp_packet.ICMP_Type)
{ {
case 0: case 0:
ICMPClient receiver = ICMPClient.Client(icmp_packet.SourceIP.Hash); ICMPClient receiver = ICMPClient.GetClient(icmp_packet.SourceIP.Hash);
if (receiver != null) if (receiver != null)
{ {
receiver.receiveData(icmp_packet); receiver.receiveData(icmp_packet);

View file

@ -46,7 +46,7 @@ namespace Cosmos.System.Network.IPv4.UDP
return; return;
} }
UdpClient receiver = UdpClient.Client(udp_packet.DestinationPort); UdpClient receiver = UdpClient.GetClient(udp_packet.DestinationPort);
if (receiver != null) if (receiver != null)
{ {
receiver.receiveData(udp_packet); receiver.receiveData(udp_packet);

View file

@ -54,7 +54,7 @@ namespace Cosmos.System.Network.IPv4.UDP
/// </summary> /// </summary>
/// <param name="destPort">Destination port.</param> /// <param name="destPort">Destination port.</param>
/// <returns>UdpClient</returns> /// <returns>UdpClient</returns>
internal static UdpClient Client(ushort destPort) internal static UdpClient GetClient(ushort destPort)
{ {
if (clients.ContainsKey((uint)destPort) == true) if (clients.ContainsKey((uint)destPort) == true)
{ {