Skip to content

Commit

Permalink
SpanWriter和SpanReader暴露内部Span,支持修改Position,以支持复杂的协议读写场景
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 12, 2024
1 parent 773014a commit 063270c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: publish-beta

on:
push:
branches: [ master, dev ]
branches: [ dev ]
paths:
- 'NewLife.Core/**'
pull_request:
branches: [ master, dev ]
branches: [ dev ]
paths:
- 'NewLife.Core/**'
workflow_dispatch:
Expand Down
9 changes: 8 additions & 1 deletion NewLife.Core/Buffers/SpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using NewLife.Data;

namespace NewLife.Buffers;

Expand All @@ -10,10 +11,12 @@ public ref struct SpanReader
{
#region 属性
private readonly ReadOnlySpan<Byte> _span;
/// <summary>数据片段</summary>
public ReadOnlySpan<Byte> Span => _span;

private Int32 _index;
/// <summary>已读取字节数</summary>
public Int32 Position => _index;
public Int32 Position { get => _index; set => _index = value; }

/// <summary>总容量</summary>
public Int32 Capacity => _span.Length;
Expand All @@ -33,6 +36,10 @@ public ref struct SpanReader
/// <summary>实例化。暂时兼容旧版,后面删除</summary>
/// <param name="span"></param>
public SpanReader(Span<Byte> span) => _span = span;

/// <summary>实例化Span读取器</summary>
/// <param name="data"></param>
public SpanReader(IPacket data) : this(data.GetSpan()) { }
#endregion

#region 基础方法
Expand Down
11 changes: 10 additions & 1 deletion NewLife.Core/Buffers/SpanWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.InteropServices;
using System.Text;
using NewLife.Collections;
using NewLife.Data;

namespace NewLife.Buffers;

Expand All @@ -13,10 +14,12 @@ public ref struct SpanWriter(Span<Byte> buffer)
{
#region 属性
private readonly Span<Byte> _span = buffer;
/// <summary>数据片段</summary>
public Span<Byte> Span => _span;

private Int32 _index;
/// <summary>已写入字节数</summary>
public readonly Int32 Position => _index;
public Int32 Position { get => _index; set => _index = value; }

/// <summary>总容量</summary>
public readonly Int32 Capacity => _span.Length;
Expand All @@ -28,6 +31,12 @@ public ref struct SpanWriter(Span<Byte> buffer)
public Boolean IsLittleEndian { get; set; } = true;
#endregion

#region 构造
/// <summary>实例化Span读取器</summary>
/// <param name="data"></param>
public SpanWriter(IPacket data) : this(data.GetSpan()) { }
#endregion

#region 基础方法
/// <summary>告知有多少数据已写入缓冲区</summary>
/// <param name="count"></param>
Expand Down

0 comments on commit 063270c

Please sign in to comment.