Skip to content

Commit

Permalink
Packet增加Slice方法
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 28, 2022
1 parent da1c6b2 commit b72aeac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
18 changes: 18 additions & 0 deletions WindivertDotnet.Test/WinDivertPacketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ public void GetSpanTest()
Assert.Throws<ArgumentOutOfRangeException>(() => packet.GetSpan(1, 10));
}


[Fact]
public void SliceTest()
{
using var packet = new WinDivertPacket(10);
var slicePacket = packet.Slice(2, 8);

var span = packet.GetSpan(0, packet.Capacity);
span[1] = 1;
span[2] = 2;
span[4] = 4;
span[8] = 8;

Assert.Equal(8, slicePacket.Capacity);
Assert.Equal(8, slicePacket.Length);
Assert.True(span.Slice(2, 8).SequenceEqual(slicePacket.Span));
}

[Fact]
public unsafe void CalcNetworkIfIdxTest()
{
Expand Down
51 changes: 47 additions & 4 deletions WindivertDotnet/WinDivertPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class WinDivertPacket : SafeHandleZeroOrMinusOneIsInvalid, IEquatable<Win
/// <summary>
/// 获取有效数据视图
/// </summary>
public Span<byte> Span => this.GetSpan(0, this.length);
public unsafe Span<byte> Span => new(this.handle.ToPointer(), this.length);

/// <summary>
/// 获取或设置有效数据的长度
Expand All @@ -60,12 +60,25 @@ public int Length
/// </summary>
/// <param name="capacity">最大容量</param>
public WinDivertPacket(int capacity = MTU_MAX)
: base(ownsHandle: true)
: this(MemoryNative.AllocZeroed(capacity), capacity, ownsHandle: true)
{
this.Capacity = capacity;
this.handle = MemoryNative.AllocZeroed(capacity);
}

/// <summary>
/// WinDivert的数据包
/// </summary>
/// <param name="handle"></param>
/// <param name="capacity"></param>
/// <param name="ownsHandle"></param>
private unsafe WinDivertPacket(IntPtr handle, int capacity, bool ownsHandle)
: base(ownsHandle)
{
this.handle = handle;
this.Capacity = capacity;
}

/// <summary>
/// 释放本机句柄
/// </summary>
Expand All @@ -89,8 +102,39 @@ public void Clear()
/// </summary>
/// <param name="offset">偏移量</param>
/// <param name="count">字节数</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <returns></returns>
public unsafe Span<byte> GetSpan(int offset, int count)
{
var pointer = this.GetPointer(offset, count);
return new Span<byte>(pointer, count);
}

/// <summary>
/// 切片
/// </summary>
/// <param name="offset">偏移量</param>
/// <param name="count">大小</param>
/// <returns></returns>
public unsafe WinDivertPacket Slice(int offset, int count)
{
var pointer = this.GetPointer(offset, count);
var handle = new IntPtr(pointer);
return new WinDivertPacket(handle, count, ownsHandle: false)
{
length = count
};
}


/// <summary>
/// 获取指针
/// </summary>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private unsafe void* GetPointer(int offset, int count)
{
if (offset < 0 || offset > this.Capacity)
{
Expand All @@ -102,8 +146,7 @@ public unsafe Span<byte> GetSpan(int offset, int count)
throw new ArgumentOutOfRangeException(nameof(count));
}

var pointer = (byte*)this.handle.ToPointer() + offset;
return new Span<byte>(pointer, count);
return (this.handle + offset).ToPointer();
}

/// <summary>
Expand Down

0 comments on commit b72aeac

Please sign in to comment.