Beispiel #1
0
    static void RunLoop(bool log)
    {
        var loop = new UVLoop();

        var listener  = new TcpListener("127.0.0.1", 8080, loop);
        var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8);

        listener.ConnectionAccepted += (Tcp connection) =>
        {
            if (log)
            {
                Console.WriteLine("connection accepted");
            }

            connection.ReadCompleted += (ByteSpan data) =>
            {
                if (log)
                {
                    unsafe
                    {
                        var requestString = new Utf8String(data.UnsafeBuffer, data.Length);
                        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      response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes
                GCHandle gcHandle;
                var      byteSpan = response.Pin(out gcHandle);
                connection.TryWrite(byteSpan);
                connection.Dispose();
                gcHandle.Free(); // TODO: formatter should format to ByteSpan, to avoid pinning
            };

            connection.ReadStart();
        };

        listener.Listen();
        loop.Run();
    }
Beispiel #2
0
    static void RunLoop(bool log)
    {
        var loop = new UVLoop();

        var listener  = new TcpListener(s_ipAddress, s_port, loop);
        var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8);

        listener.ConnectionAccepted += (Tcp connection) =>
        {
            if (log)
            {
                Console.WriteLine("connection accepted");
            }

            connection.ReadCompleted += (Span <byte> 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 response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes

                connection.TryWrite(response);
                connection.Dispose();
            };

            connection.ReadStart();
        };

        listener.Listen();
        loop.Run();
    }
Beispiel #3
0
        private void EncodeStringToUtf8()
        {
            string          text           = "Hello World!";
            int             stringsToWrite = 2000;
            int             size           = stringsToWrite * text.Length + stringsToWrite;
            BufferFormatter formatter      = new BufferFormatter(size, FormattingData.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();
        }
Beispiel #4
0
        private void EncodeStringToUtf8()
        {
            string text = "Hello World!";
            int stringsToWrite = 2000;
            int size = stringsToWrite * text.Length + stringsToWrite;
            BufferFormatter formatter = new BufferFormatter(size, FormattingData.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();
        }