agn*_*aft 10 c# ssl .net-core tls1.2
我注意到了新的System.IO.Pipelines,并试图将现有的基于流的代码移植到它上面.流的问题很好理解,但同时它具有丰富的相关类的回声系统.
从这里提供的示例中,有一个小的tcp echo服务器. https://blogs.msdn.microsoft.com/dotnet/2018/07/09/system-io-pipelines-high-performance-io-in-net/
代码片段附在此处:
private static async Task ProcessLinesAsync(Socket socket)
{
Console.WriteLine($"[{socket.RemoteEndPoint}]: connected");
var pipe = new Pipe();
Task writing = FillPipeAsync(socket, pipe.Writer);
Task reading = ReadPipeAsync(socket, pipe.Reader);
await Task.WhenAll(reading, writing);
Console.WriteLine($"[{socket.RemoteEndPoint}]: disconnected");
}
private static async Task FillPipeAsync(Socket socket, PipeWriter writer)
{
const int minimumBufferSize = 512;
while (true)
{
try
{
// Request a minimum of 512 bytes from the PipeWriter
Memory<byte> memory = writer.GetMemory(minimumBufferSize);
int bytesRead = await socket.ReceiveAsync(memory, SocketFlags.None);
if (bytesRead == 0)
{
break;
}
// Tell the PipeWriter how much was read
writer.Advance(bytesRead);
}
catch
{
break;
}
// Make the data available to the PipeReader
FlushResult result = await writer.FlushAsync();
if (result.IsCompleted)
{
break;
}
}
// Signal to the reader that we're done writing
writer.Complete();
}
private static async Task ReadPipeAsync(Socket socket, PipeReader reader)
{
while (true)
{
ReadResult result = await reader.ReadAsync();
ReadOnlySequence<byte> buffer = result.Buffer;
SequencePosition? position = null;
do
{
// Find the EOL
position = buffer.PositionOf((byte)'\n');
if (position != null)
{
var line = buffer.Slice(0, position.Value);
ProcessLine(socket, line);
// This is equivalent to position + 1
var next = buffer.GetPosition(1, position.Value);
// Skip what we've already processed including \n
buffer = buffer.Slice(next);
}
}
while (position != null);
// We sliced the buffer until no more data could be processed
// Tell the PipeReader how much we consumed and how much we left to process
reader.AdvanceTo(buffer.Start, buffer.End);
if (result.IsCompleted)
{
break;
}
}
reader.Complete();
}
private static void ProcessLine(Socket socket, in ReadOnlySequence<byte> buffer)
{
if (_echo)
{
Console.Write($"[{socket.RemoteEndPoint}]: ");
foreach (var segment in buffer)
{
Console.Write(Encoding.UTF8.GetString(segment.Span));
}
Console.WriteLine();
}
}
Run Code Online (Sandbox Code Playgroud)
使用流时,只需将其包装在SslStream中,就可以轻松地将SSL/TLS添加到代码中.如何通过管道解决这个问题?
命名管道是一种网络协议,就像 HTTP、FTP 和 SMTP 一样。让我们看一下 .net Framework 的一些快速示例:
但是如果我们使用不同的网络协议,比如管道呢?我们马上就知道没有任何类似于“HTTPS”前缀的东西。此外,我们可以阅读 System.IO.Piplines 的文档,看到没有“EnableSsl”方法。但是,在 .NET Framework 和 .NET Core 中,都可以使用 SslStream 类。此类允许您从几乎所有可用的 Stream 中构建 SslStream。
.NET Framework 和 .NET Core 中还提供 System.IO.Pipes 命名空间。Pipes 命名空间中可用的类非常有用。
所有这些类都返回某种继承自 Stream 的对象,因此可以在 SslStream 的构造函数中使用。
这与 System.IO.Piplines 命名空间有何关联?嗯......它没有。System.IO.Pipelines 命名空间中定义的类、结构或接口均不继承自 Stream。所以我们不能直接使用 SslStream 类。
相反,我们可以访问 PipeReaders 和 PipeWriters。有时我们只有其中一个可供我们使用,但让我们考虑一个双向管道,以便我们可以同时访问两者。
System.IO.Piplines 命名空间有助于提供 IDuplexPipe 接口。如果我们想将 PipeReader 和 PipeWriters 包装在 SSL 流中,我们需要定义一个实现 IDuplexPipe 的新类型。
在这种新类型中:
下面是一个伪代码示例:
SslStreamDuplexPipe : IDuplexPipe
{
SslStream sslStream;
Pipe inputBuffer;
Pipe outputBuffer;
public PipeReader Input = inputBuffer.Reader;
public PipeWriter Output = outputBuffer.Writer;
ReadDataFromSslStream()
{
int bytes = sslStream.Read(new byte[2048], 0, 2048);
inputBuffer.Writer.Advance(bytes)
inputBuffer.Writer.Flush();
}
//and the reverse to write to the SslStream
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我们仍在使用 System.Net.Security 命名空间中的 SslStream 类,只是多走了几步。
这是否意味着您基本上仍在使用流?是的!但是,一旦您完全实现了 SslStreamDuplexPipe 类,您就只能使用管道。无需围绕所有内容包装 SslStream。
Marc Gravell 对此写了一个非常非常详细的解释。3 部分中的第一部分可以在这里找到:https : //blog.marcgravell.com/2018/07/pipe-dreams-part-1.html
此外,您可以阅读提到的各种 .NET 类: