static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); var formatter = new ArrayFormatter(512, EncodingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (data) => { if (log) { unsafe { var requestString = new Utf8String(data); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var segment = formatter.Formatted; unsafe { fixed(byte *p = segment.Array) { var response = new Memory <byte>(segment.Array, segment.Offset, segment.Count, pointer: p); connection.TryWrite(response); } } connection.Dispose(); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public void BasicArrayFormatter() { using (var sb = new ArrayFormatter(256, SymbolTable.InvariantUtf16)) { sb.Append("hi"); sb.Append(1); sb.Append("hello"); sb.Append((sbyte)-20); Assert.Equal("hi1hello-20", Encoding.Unicode.GetString(sb.Formatted.Array, 0, sb.CommitedByteCount)); } }
public void The_http_extension_methods_can_be_composed_to_generate_the_http_message() { _formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); _formatter.Append(new Utf8String("Connection : open")); _formatter.AppendHttpNewLine(); _formatter.AppendHttpNewLine(); _formatter.Append(HttpBody); _formatter.AppendHttpNewLine(); _formatter.Append(HttpBody); var result = _formatter.Formatted; result.AsSpan().SequenceEqual(_httpMessageInBytes); _formatter.Clear(); }
public ArraySegment <byte> EncodeStringToUtf8() { const string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; var formatter = new ArrayFormatter(size, SymbolTable.InvariantUtf8, pool); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } return(formatter.Formatted); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (data) => { if (log) { unsafe { var requestString = new Utf8Span(data.Span); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } var formatter = new ArrayFormatter(512, SymbolTable.InvariantUtf8); formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var segment = formatter.Formatted; using (var memory = new OwnedPinnedBuffer <byte>(segment.Array)) { connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count)); connection.Dispose(); } }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public void FormatXUtf8() { var x = StandardFormat.Parse("x"); var X = StandardFormat.Parse("X"); var sb = new ArrayFormatter(256, SymbolTable.InvariantUtf8); sb.Append((ulong)255, x); sb.Append((uint)255, X); Assert.Equal("ffFF", new Utf8Span(sb.Formatted.AsSpan()).ToString()); sb.Clear(); sb.Append((int)-1, X); Assert.Equal("FFFFFFFF", new Utf8Span(sb.Formatted.AsSpan()).ToString()); sb.Clear(); sb.Append((int)-2, X); Assert.Equal("FFFFFFFE", new Utf8Span(sb.Formatted.AsSpan()).ToString()); }
public void FormatXUtf8() { var x = TextFormat.Parse("x"); var X = TextFormat.Parse("X"); var sb = new ArrayFormatter(256, EncodingData.InvariantUtf8); sb.Append((ulong)255, x); sb.Append((uint)255, X); Assert.Equal("ffFF", new Utf8String(sb.Formatted.Slice()).ToString()); sb.Clear(); sb.Append((int)-1, X); Assert.Equal("FFFFFFFF", new Utf8String(sb.Formatted.Slice()).ToString()); sb.Clear(); sb.Append((int)-2, X); Assert.Equal("FFFFFFFE", new Utf8String(sb.Formatted.Slice()).ToString()); }
//[Fact(Skip = "System.TypeLoadException : The generic type 'System.IEquatable`1' was used with an invalid instantiation in assembly 'System.Private.CoreLib")] public void EagerWrite() { dynamic json = new JsonDynamicObject(); json.First = "John"; var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); formatter.Append((JsonDynamicObject)json); Assert.Equal("{\"First\":\"John\"}", formatter.Formatted.ToString()); }
private void EncodeStringToUtf8() { string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; ArrayFormatter formatter = new ArrayFormatter(size, SymbolTable.InvariantUtf8, pool); timer.Restart(); for (int itteration = 0; itteration < itterationsInvariant; itteration++) { formatter.Clear(); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } Assert.Equal(size, formatter.CommitedByteCount); } PrintTime(); }
//[Fact(Skip = "System.TypeLoadException : The generic type 'System.IEquatable`1' was used with an invalid instantiation in assembly 'System.Private.CoreLib")] public void NestedEagerWrite() { var jsonText = new Utf8Span("{\"FirstName\":\"John\",\"LastName\":\"Smith\",\"Address\":{\"Street\":\"21 2nd Street\",\"City\":\"New York\",\"State\":\"NY\",\"Zip\":\"10021-3100\"},\"IsAlive\":true,\"Age\":25,\"Spouse\":null}"); JsonDynamicObject json = JsonDynamicObject.Parse(jsonText, 100); var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); formatter.Append(json); var formattedText = new Utf8Span(formatter.Formatted); // The follwoing check only works given the current implmentation of Dictionary. // If the implementation changes, the properties might round trip to different places in the JSON text. Assert.Equal(jsonText.ToString(), formattedText.ToString()); }
public void EagerWrite() { dynamic json = new JsonDynamicObject(); json.First = "John"; var formatter = new ArrayFormatter(1024, TextEncoder.Utf8); formatter.Append((JsonDynamicObject)json); var formattedText = new Utf8String(formatter.Formatted); Assert.Equal(new Utf8String("{\"First\":\"John\"}"), formattedText); }
public void FormatLongStringToUtf8() { int length = 260; { var formatter = new ArrayFormatter(length, SymbolTable.InvariantUtf8); string data = new string('#', length); formatter.Append(data); Assert.Equal(length, formatter.CommitedByteCount); for (int i = 0; i < formatter.CommitedByteCount; i++) { Assert.Equal((byte)'#', formatter.Formatted.Array[i]); } } }
public void FormatDateTimeRUtf8() { var time = DateTime.UtcNow; var expected = time.ToString("R"); var sb = new ArrayFormatter(100, TextEncoder.Utf8); sb.Append(time, 'R'); var result = sb.Formatted.AsSpan().ToArray(); var resultString = Encoding.UTF8.GetString(result); Assert.Equal(expected, resultString); sb.Clear(); }
public static Utf8String ToUtf8String(this ReadOnlyBytes bytes, TextEncoder encoder) { var sb = new ArrayFormatter(bytes.ComputeLength(), TextEncoder.Utf8); if (encoder.Encoding == TextEncoder.EncodingName.Utf8) { var position = Position.First; ReadOnlyBuffer <byte> segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8String(segment.Span)); } } else { throw new NotImplementedException(); } return(new Utf8String(sb.Formatted.AsSpan())); }
public static Utf8String ToUtf8String(this ReadOnlyBytes bytes, SymbolTable symbolTable) { var sb = new ArrayFormatter(bytes.ComputeLength(), SymbolTable.InvariantUtf8); if (symbolTable == SymbolTable.InvariantUtf8) { var position = Position.First; ReadOnlyBuffer <byte> segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8String(segment.Span)); } } else { throw new NotImplementedException(); } return(new Utf8String(sb.Formatted.AsSpan())); }
public static Utf8Span ToUtf8Span(this ReadOnlyBytes bytes, SymbolTable symbolTable) { if (bytes.Length > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(bytes)); } var sb = new ArrayFormatter((int)bytes.Length, SymbolTable.InvariantUtf8); if (symbolTable == SymbolTable.InvariantUtf8) { var position = Position.First; ReadOnlyMemory <byte> segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8Span(segment.Span)); } } else { throw new NotImplementedException(); } return(new Utf8Span(sb.Formatted.AsSpan())); }
private void EncodeStringToUtf8() { string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; ArrayFormatter formatter = new ArrayFormatter(size, EncodingData.InvariantUtf8, pool); timer.Restart(); for (int itteration = 0; itteration < itterationsInvariant; itteration++) { formatter.Clear(); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } Assert.Equal(size, formatter.CommitedByteCount); } PrintTime(); }
public void FormatLongStringToUtf8() { int length = 260; { var formatter = new ArrayFormatter(length, EncodingData.InvariantUtf8); string data = new string('#', length); formatter.Append(data); Assert.Equal(length, formatter.CommitedByteCount); for(int i=0; i<formatter.CommitedByteCount; i++) { Assert.Equal((byte)'#', formatter.Formatted.Array[i]); } } }
public void DataArray() { // Verify that we can deserialize arrays of data items. var converter = new JsonDataConverter(); var formatter = new ArrayFormatter(); byte[] contents; object[] items; // Empty array. contents = null; items = converter.FromDataArray(contents, Array.Empty <Type>()); Assert.Empty(items); // Single item. formatter.Clear(); formatter.Append("\"foo\""); contents = formatter.ToBytes(); items = converter.FromDataArray(contents, new Type[] { typeof(string) }); Assert.Equal(new string[] { "foo" }, items); // Multiple object items formatter.Clear(); formatter.Append("\"foo\""); formatter.Append("1234"); formatter.Append("{Hello: \"World!\"}"); formatter.Append("null"); contents = formatter.ToBytes(); items = converter.FromDataArray(contents, new Type[] { typeof(string), typeof(int), typeof(TestData), typeof(TestData) }); Assert.Equal(4, items.Length); Assert.Equal("foo", items[0]); Assert.Equal(1234, items[1]); Assert.Equal("World!", ((TestData)items[2]).Hello); Assert.Null(items[3]); // Roundtrip objects var bob = new Person() { Name = "Bob", Age = 27, Data = new byte[] { 0, 1, 2, 3, 4 }, Gender = Gender.Male }; bob.__O.Add("extra", "data"); formatter.Clear(); formatter.Clear(); formatter.Append("\"foo\""); formatter.Append("1234"); formatter.Append(bob.ToString(indented: false)); formatter.Append("null"); contents = formatter.ToBytes(); items = converter.FromDataArray(contents, new Type[] { typeof(string), typeof(int), typeof(Person), typeof(Person) }); Assert.Equal(4, items.Length); Assert.Equal("foo", items[0]); Assert.Equal(1234, items[1]); Assert.Equal(bob, (Person)items[2]); Assert.Equal("data", ((Person)items[2]).__O["extra"].ToString()); Assert.Null(items[3]); // Arrays of other types. formatter.Clear(); var guid = Guid.NewGuid(); formatter.Append("10"); formatter.Append("123.4"); formatter.Append("\"Hello World!\""); formatter.Append("null"); formatter.Append("\"female\""); formatter.Append("true"); formatter.Append(NeonHelper.JsonSerialize(new DateTime(2019, 7, 17, 12, 0, 0))); formatter.Append(NeonHelper.JsonSerialize(TimeSpan.FromSeconds(1.5))); formatter.Append($"\"{guid}\""); contents = formatter.ToBytes(); items = converter.FromDataArray(contents, new Type[] { typeof(int), typeof(double), typeof(string), typeof(string), typeof(Gender), typeof(bool), typeof(DateTime), typeof(TimeSpan), typeof(Guid) }); Assert.Equal(9, items.Length); Assert.Equal(10, (int)items[0]); Assert.Equal(123.4, (double)items[1]); Assert.Equal("Hello World!", (string)items[2]); Assert.Null((string)items[3]); Assert.Equal(Gender.Female, (Gender)items[4]); Assert.True((bool)items[5]); Assert.Equal(new DateTime(2019, 7, 17, 12, 0, 0), (DateTime)items[6]); Assert.Equal(TimeSpan.FromSeconds(1.5), (TimeSpan)items[7]); Assert.Equal(guid, (Guid)items[8]); }