Skip to content

Commit

Permalink
公开GetSpan方法
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 14, 2022
1 parent 8203318 commit ba79373
Showing 1 changed file with 15 additions and 65 deletions.
80 changes: 15 additions & 65 deletions WindivertDotnet/WinDivertPacket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.Buffers;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Sockets;
Expand Down Expand Up @@ -56,58 +55,47 @@ public WinDivertPacket(int capacity = 0xFFFF + 40)
{
this.Capacity = capacity;
this.handle = Marshal.AllocHGlobal(capacity);
this.Clear();
}

/// <summary>
/// 清除数据
/// </summary>
public void Clear()
{
this.GetSpan(0, this.Capacity).Clear();
}

/// <summary>
/// 创建缓冲区写入对象
/// 释放本机句柄
/// </summary>
/// <param name="offset">缓冲区偏移量</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <returns></returns>
public IBufferWriter<byte> CreateBufferWriter(int offset = 0)
protected override bool ReleaseHandle()
{
return new BufferWriter(this, offset);
Marshal.FreeHGlobal(this.handle);
return true;
}

/// <summary>
/// 释放本机句柄
/// 将有效数据清0
/// </summary>
/// <returns></returns>
protected override bool ReleaseHandle()
public void Clear()
{
Marshal.FreeHGlobal(this.handle);
return true;
this.Span.Clear();
}

/// <summary>
/// 获取span
/// 获取缓冲区的Span
/// </summary>
/// <param name="offset"></param>
/// <param name="sizeHint"></param>
/// <param name="offset">偏移量</param>
/// <param name="count">字节数</param>
/// <returns></returns>
private unsafe Span<byte> GetSpan(int offset, int sizeHint)
public unsafe Span<byte> GetSpan(int offset, int count)
{
if (offset > this.Capacity)
if (offset < 0 || offset > this.Capacity)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}

if (this.Capacity - offset < sizeHint)
if (count < 0 || this.Capacity - offset < count)
{
throw new ArgumentOutOfRangeException(nameof(sizeHint));
throw new ArgumentOutOfRangeException(nameof(count));
}

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

/// <summary>
Expand Down Expand Up @@ -203,43 +191,5 @@ public unsafe WinDivertParseResult GetParseResult()
NextLength = nextLength
};
}


private class BufferWriter : IBufferWriter<byte>
{
private int index;
private readonly WinDivertPacket packet;

public BufferWriter(WinDivertPacket packet, int offset)
{
if (offset >= packet.Capacity)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}

this.index = offset;
this.packet = packet;
}

public void Advance(int count)
{
this.index += count;
this.packet.Length = this.index;
}

public Span<byte> GetSpan(int sizeHint = 0)
{
if (sizeHint == 0)
{
sizeHint = this.packet.Capacity - this.index;
}
return this.packet.GetSpan(this.index, sizeHint);
}

public Memory<byte> GetMemory(int sizeHint = 0)
{
throw new NotSupportedException();
}
}
}
}

0 comments on commit ba79373

Please sign in to comment.