Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PipeWriter和长度前缀消息_C#_System.io.pipelines - Fatal编程技术网

C# PipeWriter和长度前缀消息

C# PipeWriter和长度前缀消息,c#,system.io.pipelines,C#,System.io.pipelines,我正在尝试使用前缀为字节长度的PipeWriter编写二进制消息。 在整个消息完成之前,我不知道字节长度。 是否可以使用PipeWriter完成此操作 基本上,我正在尝试实现这三种方法: void StartMessage(PipeWriter writer) { // leave 4 empty bytes for length written at later time this.header_ = writer.GetMemory(4); writer.A

我正在尝试使用前缀为字节长度的
PipeWriter
编写二进制消息。 在整个消息完成之前,我不知道字节长度。 是否可以使用
PipeWriter
完成此操作

基本上,我正在尝试实现这三种方法:

void StartMessage(PipeWriter writer)
{
    // leave 4 empty bytes for length written at later time
    this.header_ = writer.GetMemory(4);
    
    writer.Advance(4);
}

void Write(PipeWriter writer, ReadOnlySpan<byte> span)
{
    var buffer = writer.GetSpan(span.Length);

    span.CopyTo(buffer);

    writer.Advance(span.Length);

    this.totalLength_ += span.Length;
}

ValueTask EndMessage(PipeWriter writer)
{
    // should prepend this.totalLength_ to the message
    BinaryPrimities.WriteUInt32BingEndian(this.header_.Span, this.totalLength_);

    // signal end of message
    return writer.FlushAsync();
}
void StartMessage(PipeWriter)
{
//为以后写入的长度保留4个空字节
this.header=writer.GetMemory(4);
作者:前进(4);
}
无效写入(PipeWriter-writer,ReadOnlySpan-span)
{
var buffer=writer.GetSpan(span.Length);
span.CopyTo(缓冲区);
作家进步(跨度、长度);
总长度=跨度长度;
}
ValueTask EndMessage(PipeWriter编写器)
{
//应该在消息前加上this.totalLength
BinaryPrimities.WriteUInt32BingEndian(this.header\u0.Span,this.totalLength);
//信号结束消息
返回writer.FlushAsync();
}
这给了我预期的结果,但是
PipeWrite.Advance
的文档说明:

您必须在调用System.IO.Pipelines.PipeWriter.Advance(System.Int32)后请求新的缓冲区,才能继续写入更多数据;无法写入以前获取的缓冲区


使用
PipeReader
AdvanceTo(已消费、已检查)
方法可以轻松读取此类消息,在这种方法中,我们仅在整个消息长度可用时使用输入,但是根据文档,我无法想出正确的
PipeWriter
用法。

我在这里找到了一些关于这种用法的讨论:。似乎may代码所基于的行为是未定义的。