title: TLS/SSL with System.IO.Pipelines
body: <p>I have noticed the new System.IO.Pipelines and are trying to port existing, stream based, code over to it. The problems with streams are well understood, but at the same time it features a rich echosystem of related classes.</p>
  
  <p>From the example provided here, there is a small tcp echo server.
  <a href="https://blogs.msdn.microsoft.com/dotnet/2018/07/09/system-io-pipelines-high-performance-io-in-net/" rel="noreferrer">https://blogs.msdn.microsoft.com/dotnet/2018/07/09/system-io-pipelines-high-performance-io-in-net/</a></p>
  
  <p>A snippet of the code is attached here:</p>
  
  <pre><code>    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&lt;byte&gt; 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&lt;byte&gt; 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&lt;byte&gt; buffer)
      {
          if (_echo)
          {
              Console.Write($"[{socket.RemoteEndPoint}]: ");
              foreach (var segment in buffer)
              {
                  Console.Write(Encoding.UTF8.GetString(segment.Span));
              }
              Console.WriteLine();
          }
      }
  </code></pre>
  
  <p>When using streams, you could easily add SSL/TLS to your code just by wrapping it in SslStream. How is this intended to be solved with Pipelines?</p>