Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using System;
  2. using System.Buffers;
  3. using System.Collections.Generic;
  4. using System.IO.Pipelines;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ConsoleApp1
  12. {
  13. class Program
  14. {
  15. private static readonly IEnumerable<IPEndPoint> ipAddresses = new[]
  16. {
  17. new IPEndPoint(IPAddress.Loopback, 8087),
  18. // more here.
  19. };
  20.  
  21. internal static async Task Main()
  22. {
  23. await Task.WhenAll((await Task.WhenAll(
  24. ipAddresses.Select(OpenSocket)))
  25. .SelectMany(p => p));
  26.  
  27. // Handling code in ProcessLine.
  28. }
  29.  
  30. private static async Task<IEnumerable<Task>> OpenSocket(
  31. EndPoint iPEndPoint)
  32. {
  33. var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  34. await socket.ConnectAsync(iPEndPoint);
  35. var pipe = new Pipe();
  36.  
  37. var attendants = new[]
  38. {
  39. FillPipeAsync(socket, pipe.Writer),
  40. ReadPipeAsync(socket, pipe.Reader)
  41. };
  42.  
  43. return attendants;
  44. }
  45.  
  46. private static async Task FillPipeAsync(Socket socket, PipeWriter writer)
  47. {
  48. const int minimumBufferSize = 512;
  49.  
  50. while (true)
  51. {
  52. try
  53. {
  54. // Request a minimum of 512 bytes from the PipeWriter
  55. var memory = writer.GetMemory(minimumBufferSize);
  56.  
  57. var bytesRead = await socket.ReceiveAsync(
  58. memory,
  59. SocketFlags.None);
  60.  
  61. if (bytesRead == 0)
  62. {
  63. break;
  64. }
  65.  
  66. // Tell the PipeWriter how much was read
  67. writer.Advance(bytesRead);
  68. }
  69. catch
  70. {
  71. break;
  72. }
  73.  
  74. // Make the data available to the PipeReader
  75. var result = await writer.FlushAsync();
  76.  
  77. if (result.IsCompleted)
  78. {
  79. break;
  80. }
  81. }
  82.  
  83. // Signal to the reader that we're done writing
  84. writer.Complete();
  85. }
  86.  
  87. private static async Task ReadPipeAsync(Socket socket, PipeReader reader)
  88. {
  89. while (true)
  90. {
  91. var result = await reader.ReadAsync();
  92. var buffer = result.Buffer;
  93. SequencePosition? position;
  94.  
  95. do
  96. {
  97. // Find the EOL
  98. position = buffer.PositionOf((byte)'n');
  99.  
  100. if (position == null)
  101. {
  102. continue;
  103. }
  104.  
  105. var line = buffer.Slice(0, position.Value);
  106. ProcessLine(socket, line);
  107.  
  108. // This is equivalent to position + 1
  109. var next = buffer.GetPosition(1, position.Value);
  110.  
  111. // Skip what we've already processed including n
  112. buffer = buffer.Slice(next);
  113. } while (position != null);
  114.  
  115. // We sliced the buffer until no more data could be processed
  116. // Tell the PipeReader how much we consumed and how much we
  117. // left to process
  118.  
  119. reader.AdvanceTo(buffer.Start, buffer.End);
  120.  
  121. if (result.IsCompleted)
  122. {
  123. break;
  124. }
  125. }
  126.  
  127. reader.Complete();
  128. }
  129.  
  130. private static void ProcessLine(
  131. Socket socket,
  132. in ReadOnlySequence<byte> buffer)
  133. {
  134. Console.Write($"[{socket.RemoteEndPoint}]: ");
  135.  
  136. foreach (var segment in buffer)
  137. {
  138. Console.Write(Encoding.UTF8.GetString(segment.Span));
  139. }
  140.  
  141. Console.WriteLine();
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement