Skip to content

Commit

Permalink
IdSeqNum设为静态类
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 20, 2022
1 parent e416f7e commit 7b5349a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
15 changes: 8 additions & 7 deletions App/FastPinger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace App
class FastPinger : IDisposable
{
private readonly WinDivert divert;
private readonly IdSeqNum idSeqNum = new();

public FastPinger()
{
Expand Down Expand Up @@ -166,7 +167,7 @@ private async Task SendEchoRequestAsync(IEnumerable<IPAddress> dstAddrs)
/// <param name="srcAddr"></param>
/// <param name="dstAddr"></param>
/// <returns></returns>
private static unsafe WinDivertPacket CreateIPV4EchoPacket(IPAddress srcAddr, IPAddress dstAddr)
private unsafe WinDivertPacket CreateIPV4EchoPacket(IPAddress srcAddr, IPAddress dstAddr)
{
// ipv4头
var ipHeader = new IPV4Header
Expand All @@ -177,7 +178,7 @@ private static unsafe WinDivertPacket CreateIPV4EchoPacket(IPAddress srcAddr, IP
SrcAddr = srcAddr,
Protocol = ProtocolType.Icmp,
HdrLength = (byte)(sizeof(IPV4Header) / 4),
Id = IdSeqNum.IdUInt16(),
Id = IdSeqNum.Shared.NextUInt16(),
Length = (ushort)(sizeof(IPV4Header) + sizeof(IcmpV4Header))
};

Expand All @@ -186,8 +187,8 @@ private static unsafe WinDivertPacket CreateIPV4EchoPacket(IPAddress srcAddr, IP
{
Type = IcmpV4MessageType.EchoRequest,
Code = default,
Identifier = ipHeader.Id,
SequenceNumber = IdSeqNum.SeqNumUInt16(),
Identifier = this.idSeqNum.NextUInt16(),
SequenceNumber = this.idSeqNum.NextUInt16(),
};

// 将数据写到packet缓冲区
Expand All @@ -206,7 +207,7 @@ private static unsafe WinDivertPacket CreateIPV4EchoPacket(IPAddress srcAddr, IP
/// <param name="srcAddr"></param>
/// <param name="dstAddr"></param>
/// <returns></returns>
private static unsafe WinDivertPacket CreateIPV6EchoPacket(IPAddress srcAddr, IPAddress dstAddr)
private unsafe WinDivertPacket CreateIPV6EchoPacket(IPAddress srcAddr, IPAddress dstAddr)
{
// ipv6头
var ipHeader = new IPV6Header
Expand All @@ -224,8 +225,8 @@ private static unsafe WinDivertPacket CreateIPV6EchoPacket(IPAddress srcAddr, IP
{
Type = IcmpV6MessageType.EchoRequest,
Code = default,
Identifier = IdSeqNum.IdUInt16(),
SequenceNumber = IdSeqNum.SeqNumUInt16(),
Identifier = this.idSeqNum.NextUInt16(),
SequenceNumber = this.idSeqNum.NextUInt16(),
};

// 将数据写到packet缓冲区
Expand Down
26 changes: 11 additions & 15 deletions WindivertDotnet/IdSeqNum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,28 @@
namespace WindivertDotnet
{
/// <summary>
/// 提供Id和序列号生成
/// id或序列号生成器
/// </summary>
public static class IdSeqNum
public sealed class IdSeqNum
{
private static readonly Random random = new();
private static int id = random.Next();
private static int seqNum = random.Next();
private int value = random.Next();

/// <summary>
/// 获取新的16位Id
/// 获取公共实例
/// </summary>
public static ushort IdUInt16() => (ushort)Interlocked.Increment(ref id);
public static IdSeqNum Shared { get; } = new IdSeqNum();

/// <summary>
/// 获取新的32位id
/// 生成下一个Id
/// </summary>
public static uint IdUInt32() => (uint)Interlocked.Increment(ref id);
/// <returns></returns>
public ushort NextUInt16() => (ushort)Interlocked.Increment(ref value);

/// <summary>
/// 获取新的16位SeqNum
/// 生成下一个Id
/// </summary>
public static ushort SeqNumUInt16() => (ushort)Interlocked.Increment(ref seqNum);

/// <summary>
/// 获取新的32位SeqNum
/// </summary>
public static uint SeqNumUInt32() => (uint)Interlocked.Increment(ref seqNum);
/// <returns></returns>
public ushort NextUInt32() => (ushort)Interlocked.Increment(ref value);
}
}

0 comments on commit 7b5349a

Please sign in to comment.