Exemple #1
0
        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 Utf8String(data.Span);
                            Console.WriteLine("*REQUEST:\n {0}", requestString.ToString());
                        }
                    }

                    var formatter = new ArrayFormatter(512, EncodingData.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 OwnedPinnedArray<byte>(segment.Array)) {
                        connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count));
                        connection.Dispose();
                    }
                };

                connection.ReadStart();
            };

            listener.Listen();
            loop.Run();
        }
        public void EagerWrite()
        {
            dynamic json = new JsonDynamicObject();
            json.First = "John";

            var formatter = new ArrayFormatter(1024, EncodingData.InvariantUtf8);
            formatter.Append((JsonDynamicObject)json);
            var formattedText = new Utf8String(formatter.Formatted);
            Assert.Equal(new Utf8String("{\"First\":\"John\"}"), formattedText);
        }
        public void NestedEagerWrite()
        {
            var jsonText = new Utf8String("{\"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, EncodingData.InvariantUtf8);
            formatter.Append(json);
            var formattedText = new Utf8String(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, formattedText); 
        }