diff --git a/codelab/TcpServerLab/FrameHeader.cs b/codelab/TcpServerLab/FrameHeader.cs index 9ae4877f..7106f4e0 100644 --- a/codelab/TcpServerLab/FrameHeader.cs +++ b/codelab/TcpServerLab/FrameHeader.cs @@ -71,5 +71,10 @@ public void WriteTo(Span buffer) // Write content checksum. BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(16, 4), this.ContentChecksum); } + + public String ToString() + { + throw new NotImplementedException(); + } } } diff --git a/codelab/TcpServerLab/Program.cs b/codelab/TcpServerLab/Program.cs index 89d0a447..2b67cd2d 100644 --- a/codelab/TcpServerLab/Program.cs +++ b/codelab/TcpServerLab/Program.cs @@ -6,13 +6,11 @@ using System; using System.Globalization; using System.IO; -using System.Net; using System.Net.Sockets; using System.Text; using GeothermalResearchInstitute.v2; using Google.Protobuf; using Google.Protobuf.WellKnownTypes; -using System.Buffers.Binary; namespace TcpServerLab { @@ -47,25 +45,36 @@ private static void Main() .AsMemory(dataStartingPosition, (int)(memoryStream.Position - dataStartingPosition)))); Console.WriteLine(); - using var client = listener.AcceptTcpClient(); - using var networkStream = client.GetStream(); + // using var client = listener.AcceptTcpClient(); + // using var networkStream = client.GetStream(); + // memoryStream.Seek(0, SeekOrigin.Begin); + // memoryStream.CopyTo(networkStream); + memoryStream.Seek(0, SeekOrigin.Begin); - memoryStream.CopyTo(networkStream); - - var buffer = new byte[8192]; - var headerFrameHeaderByteSpan = buffer.AsSpan(0, 20); - ReadBytes(networkStream, headerFrameHeaderByteSpan); - var headerFrameHeader = FrameHeader.Parse(headerFrameHeaderByteSpan); - var headerFrameContentByteSpan = buffer.AsSpan(20, headerFrameHeader.ContentLength); - ReadBytes(networkStream, headerFrameContentByteSpan); - var headerFrameByteSpan = buffer.AsSpan(0, headerFrameHeaderByteSpan.Length + headerFrameContentByteSpan.Length); + var networkStream = memoryStream; + + var bufferMemory = new Memory(new byte[8192]); + ReadHeaderFrame(networkStream, bufferMemory.Span, out var headerFrameSpan, out var headerFrameHeader, out var headerFrameContent); + Console.WriteLine("Receiving header frame, content size = {0}, frame size = {1}", headerFrameHeader.ContentLength, headerFrameSpan.Length); + Console.WriteLine(HexUtils.Dump(bufferMemory.Slice(0, headerFrameSpan.Length))); + Console.WriteLine(); + + Console.WriteLine(headerFrameContent.ToString()); } - private static void ReadHeaderFrame(Stream stream, Span buffer, out Span frameSpan, out FrameHeader frameHeader, out Header header) { + private static void ReadHeaderFrame(Stream stream, Span buffer, out Span frameSpan, out FrameHeader frameHeader, out Header header) + { ReadFrame(stream, buffer, out var frameHeaderSpan, out var frameContentSpan, out frameSpan, out frameHeader); + header = Header.Parser.ParseFrom(frameContentSpan.ToArray()); + } + + private static void ReadDataFrame(Stream stream, Span buffer, out Span frameSpan, out FrameHeader frameHeader, out Span contentSpan) + { + ReadFrame(stream, buffer, out var frameHeaderSpan, out contentSpan, out frameSpan, out frameHeader); } - private static void ReadFrame(Stream stream, Span buffer, out Span frameHeaderSpan, out Span frameContentSpan, out Span frameSpan, out FrameHeader frameHeader) { + private static void ReadFrame(Stream stream, Span buffer, out Span frameHeaderSpan, out Span frameContentSpan, out Span frameSpan, out FrameHeader frameHeader) + { frameHeaderSpan = buffer.Slice(0, 20); ReadBytes(stream, frameHeaderSpan); frameHeader = FrameHeader.Parse(frameHeaderSpan); @@ -74,8 +83,10 @@ private static void ReadFrame(Stream stream, Span buffer, out Span f frameSpan = buffer.Slice(0, frameHeaderSpan.Length + frameContentSpan.Length); } - private static void ReadBytes(Stream stream, Span buffer) { - while (buffer.Length != 0) { + private static void ReadBytes(Stream stream, Span buffer) + { + while (buffer.Length != 0) + { int count = stream.Read(buffer); buffer = buffer.Slice(count); } @@ -104,7 +115,8 @@ private static void WriteDataFrame(BinaryWriter writer, int streamId, ReadOnlySp private static void WriteFrame(BinaryWriter writer, byte type, byte seqNum, int streamId, ReadOnlySpan contents) { uint contentChecksum = 0; - if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) { + if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) + { contentChecksum = Crc32C.Crc32CAlgorithm.Compute(contents.ToArray()); }