Skip to content

Commit

Permalink
Rename Client to GetClient + some code clean + Add rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Jan 21, 2021
1 parent 584568b commit eaf05f4
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 22 deletions.
26 changes: 17 additions & 9 deletions source/Cosmos.HAL2/Network/MACAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ static MACAddress()
public MACAddress(byte[] address)
{
if (address == null || address.Length != 6)
{
throw new ArgumentException("MACAddress is null or has wrong length", "address");
}

bytes[0] = address[0];
bytes[1] = address[1];
Expand All @@ -52,7 +54,9 @@ public MACAddress(byte[] address)
public MACAddress(byte[] buffer, int offset)
{
if (buffer == null || buffer.Length < (offset + 6))
{
throw new ArgumentException("buffer does not contain enough data starting at offset", "buffer");
}

bytes[0] = buffer[offset];
bytes[1] = buffer[offset + 1];
Expand Down Expand Up @@ -95,7 +99,9 @@ public int CompareTo(object obj)
return 0;
}
else
{
throw new ArgumentException("obj is not a MACAddress", "obj");
}
}

public override bool Equals(object obj)
Expand All @@ -112,17 +118,19 @@ public override bool Equals(object obj)
bytes[5] == other.bytes[5];
}
else
{
throw new ArgumentException("obj is not a MACAddress", "obj");
}
}

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));
}

Expand All @@ -133,23 +141,23 @@ private static void PutByte(char[] aChars, int aIndex, byte aByte)
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));
}

private UInt32 hash;
private uint hash;
/// <summary>
/// Hash value for this mac. Used to uniquely identify each mac
/// </summary>
public UInt32 Hash
public uint Hash
{
get
{
if (hash == 0)
{
hash = to32BitNumber();
hash = To32BitNumber();
}

return hash;
Expand All @@ -171,7 +179,7 @@ public override string ToString()
PutByte(xChars, 12, bytes[4]);
xChars[14] = ':';
PutByte(xChars, 15, bytes[5]);
return new String(xChars);
return new string(xChars);
}
}
}
8 changes: 2 additions & 6 deletions source/Cosmos.System2/Network/ARP/ARPCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class ARPCache
/// <summary>
/// Cache.
/// </summary>
private static Dictionary<uint, MACAddress> cache;
public static Dictionary<uint, MACAddress> cache;

/// <summary>
/// Ensure cache exists.
Expand Down Expand Up @@ -55,11 +55,7 @@ internal static bool Contains(IPv4.Address ipAddress)
internal static void Update(IPv4.Address ipAddress, MACAddress macAddress)
{
ensureCacheExists();
if (ipAddress == null)
{
global::System.Console.Write("");
}
UInt32 ip_hash = ipAddress.Hash;
uint ip_hash = ipAddress.Hash;
if (ip_hash == 0)
{
return;
Expand Down
4 changes: 2 additions & 2 deletions source/Cosmos.System2/Network/Config/DNSConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class DNSConfig
/// <param name="config"></param>
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;
}
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/IPV4/ICMPClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static ICMPClient()
/// </summary>
/// <param name="iphash">IP Hash.</param>
/// <returns>ICMPClient</returns>
internal static ICMPClient Client(uint iphash)
internal static ICMPClient GetClient(uint iphash)
{
if (clients.ContainsKey(iphash) == true)
{
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/IPV4/UDP/DNS/DNSPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal static void DNSHandler(byte[] 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)
{
receiver.receiveData(dns_packet);
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/IPv4/ICMPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static void ICMPHandler(byte[] packetData)
switch (icmp_packet.ICMP_Type)
{
case 0:
ICMPClient receiver = ICMPClient.Client(icmp_packet.SourceIP.Hash);
ICMPClient receiver = ICMPClient.GetClient(icmp_packet.SourceIP.Hash);
if (receiver != null)
{
receiver.receiveData(icmp_packet);
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/IPv4/UDP/UDPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static void UDPHandler(byte[] packetData)
return;
}

UdpClient receiver = UdpClient.Client(udp_packet.DestinationPort);
UdpClient receiver = UdpClient.GetClient(udp_packet.DestinationPort);
if (receiver != null)
{
receiver.receiveData(udp_packet);
Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.System2/Network/IPv4/UDP/UdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static UdpClient()
/// </summary>
/// <param name="destPort">Destination port.</param>
/// <returns>UdpClient</returns>
internal static UdpClient Client(ushort destPort)
internal static UdpClient GetClient(ushort destPort)
{
if (clients.ContainsKey((uint)destPort) == true)
{
Expand Down

0 comments on commit eaf05f4

Please sign in to comment.