Esempio n. 1
0
        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();
        }
Esempio n. 2
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();
    }
Esempio n. 3
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 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();
        }
Esempio n. 4
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();
    }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var loop = new UVLoop();

            var listener = new TcpListener("0.0.0.0", 7, loop);

            listener.ConnectionAccepted += (Tcp connection) =>
            {
                connection.ReadCompleted += (data) =>
                {
                    connection.TryWrite(data);
                };

                connection.ReadStart();
            };

            listener.Listen();
            loop.Run();
        }
Esempio n. 6
0
        public static void Test1()
        {
            //--------------------------------------------------------
            Console.WriteLine("EspressoND test libuv inside nodejs");
            Console.WriteLine("SharpConnect.IO says : hello!");
            //--------------------------------------------------------
            UVLoop      uvLoop      = new UVLoop();
            TcpListener tcpListener = new TcpListener("127.0.0.1", 8080, uvLoop);

            tcpListener.ConnectionAccepted += (Tcp newIncommingConn) =>
            {
                newIncommingConn.ReadCompleted += (arg) =>
                {
                    //get data --temp
                    string req = null;
                    unsafe
                    {
                        byte * dataH    = (byte *)arg.UnsafePointer;
                        int    dataLen  = arg.Length;
                        byte[] copyData = new byte[dataLen];
                        System.Runtime.InteropServices.Marshal.Copy((IntPtr)dataH,
                                                                    copyData, 0, dataLen
                                                                    );
                        req = System.Text.Encoding.UTF8.GetString(copyData);
                    }
                    //--------------------------------------------------------
                    StringBuilder stbuilder = new StringBuilder();
                    stbuilder.Append("OK-12345");
                    newIncommingConn.TryWrite(
                        CreateTestResponse(stbuilder));

                    //--------------------------------------------------------
                };
                newIncommingConn.ReadStart();
            };
            tcpListener.Listen(); //start listening
            uvLoop.Run();

            //server wait for new incomming connection
            //-----------
            //try test by call http://127.0.0.1:8080 from web browser
            uvLoop.Stop();
        }
Esempio n. 7
0
        public static void Main(string[] args)
        {
            var buffer = new byte[1024];
            var quote  = new Utf8String("Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein");;

            var loop = new UVLoop();

            var listener = new TcpListener("0.0.0.0", 17, loop);

            listener.ConnectionAccepted += (Tcp connection) =>
            {
                connection.ReadCompleted += (data) =>
                {
                    quote.CopyTo(buffer);
                    connection.TryWrite(buffer, quote.Length);
                };

                connection.ReadStart();
            };

            listener.Listen();
            loop.Run();
        }