コード例 #1
0
        // For testing
        public LibuvTransport(LibuvFunctions uv, LibuvTransportContext context, IEndPointInformation endPointInformation)
        {
            Libuv            = uv;
            TransportContext = context;

            _endPointInformation = endPointInformation;
        }
コード例 #2
0
        public ITransport Create(IEndPointInformation endPointInformation, IConnectionHandler handler)
        {
            var transportContext = new LibuvTransportContext
            {
                Options           = _baseTransportContext.Options,
                AppLifetime       = _baseTransportContext.AppLifetime,
                Log               = _baseTransportContext.Log,
                ConnectionHandler = handler
            };

            return(new LibuvTransport(transportContext, endPointInformation));
        }
コード例 #3
0
        public Listener(IPEndPoint ipep)
        {
            m_EndPoint = ipep;
            LibuvTransportContext transport = new LibuvTransportContext
            {
                Options     = new LibuvTransportOptions(),
                AppLifetime = new ApplicationLifetime(
                    LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger <ApplicationLifetime>()
                    ),
                Log = new LibuvTrace(LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger("network"))
            };

            m_Listener = new LibuvConnectionListener(functions, transport, ipep);
        }
コード例 #4
0
        public LibuvTransportFactory(
            IOptions <LibuvTransportOptions> options,
            IApplicationLifetime applicationLifetime,
            ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (applicationLifetime == null)
            {
                throw new ArgumentNullException(nameof(applicationLifetime));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv");
            var trace  = new LibuvTrace(logger);

            var threadCount = options.Value.ThreadCount;

            if (threadCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(threadCount),
                                                      threadCount,
                                                      "ThreadCount must be positive.");
            }

            if (!LibuvConstants.ECONNRESET.HasValue)
            {
                trace.LogWarning("Unable to determine ECONNRESET value on this platform.");
            }

            if (!LibuvConstants.EADDRINUSE.HasValue)
            {
                trace.LogWarning("Unable to determine EADDRINUSE value on this platform.");
            }

            _baseTransportContext = new LibuvTransportContext
            {
                Options     = options.Value,
                AppLifetime = applicationLifetime,
                Log         = trace,
            };
        }
コード例 #5
0
 public LibuvTransport(LibuvTransportContext context, IEndPointInformation endPointInformation)
     : this(new LibuvFunctions(), context, endPointInformation)
 {
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: LavinasChange/TestLibUv
        static void Main(string[] args)
        {
            Console.WriteLine("Main: {0}", Thread.CurrentThread.ManagedThreadId);

            // Use the factory
            LibuvTransportContext transport = new LibuvTransportContext
            {
                Options     = new LibuvTransportOptions(),
                AppLifetime = new ApplicationLifetime(null),
                Log         = new LibuvTrace(LoggerFactory.Create(builder =>
                {
                    builder.AddConsole();
                }).CreateLogger("core"))
            };

            LibuvThread uvThread = new LibuvThread(libUv, transport);

            uvThread.StartAsync().Wait();

            Task.Run(async() =>
            {
                Console.WriteLine("NIO: {0}", Thread.CurrentThread.ManagedThreadId);

                LibuvConnectionListener listener = new LibuvConnectionListener(libUv, transport, new IPEndPoint(IPAddress.Parse("0.0.0.0"), 2593));
                Console.WriteLine("Binding... ({0})", Thread.CurrentThread.ManagedThreadId);
                await listener.BindAsync();

                Console.WriteLine("Listening... ({0})", Thread.CurrentThread.ManagedThreadId);
                ConnectionContext connectionContext = await listener.AcceptAsync();
                Console.WriteLine("Accepted Connection from {0}", connectionContext.RemoteEndPoint);
                PipeReader reader = connectionContext.Transport.Input;

                while (true)
                {
                    ReadResult readResult = await reader.ReadAsync();
                    int packetId          = readResult.Buffer.FirstSpan[0];
                    Console.WriteLine($"[0x{packetId:X}] Processing Packet");
                    int length     = PacketLengths[packetId];
                    int bodyLength = length - 1;
                    int bodyStart  = 1;
                    if (length == 0)
                    {
                        length     = BinaryPrimitives.ReadUInt16BigEndian(readResult.Buffer.FirstSpan.Slice(1, 2));
                        bodyLength = length - 3;
                        bodyStart  = 3;
                    }
                    else if (length == 0xFFFF)
                    {
                        Console.WriteLine($"[0x{packetId:X}] Unknown Packet");
                        throw new Exception($"[0x{packetId:X}] Unknown Packet");
                    }
                    Console.WriteLine($"[0x{packetId:X}] Found length {length}");
                    Console.WriteLine($"Packet Data: {ByteArrayToString(readResult.Buffer.FirstSpan.ToArray()):X}");
                    Memory <byte> mem = new Memory <byte>(readResult.Buffer.FirstSpan.Slice(bodyStart, bodyLength).ToArray());
                    Console.WriteLine($"[0x{packetId:X}] Buffer length {mem.Length}");

                    _ = uvThread.PostAsync((Tuple <ConnectionContext, Memory <byte> > t) =>
                    {
                        // stuff
                        var(conn, mem) = t;
                        // Do stuff wtih memOwner.Memory.Span;
                        Console.WriteLine($"Packet ID: 0x{packetId:X} - Length: {length} - Data: 0x{ByteArrayToString(mem.ToArray())}");
                    }, Tuple.Create(connectionContext, mem));

                    reader.AdvanceTo(readResult.Buffer.GetPosition(length));
                }
            });

            // Manually putting something on the queue from another thread (or the main thread)
            uvThread.PostAsync <object>(_ =>
            {
                Console.WriteLine("Game: {0}", Thread.CurrentThread.ManagedThreadId);
            }, null);

            // Send an Initialization Request for Timers

            /*
             * uvThread.PostAsync<object>(_ =>
             * {
             * UvTimerHandle timerHandle = new UvTimerHandle(null);
             * timerHandle.Init(uvThread.Loop, (callback, handle) =>
             * {
             *  Console.WriteLine("Closed ({0})", Thread.CurrentThread.ManagedThreadId);
             * });
             * Console.WriteLine("Timer Stuff {0}", Thread.CurrentThread.ManagedThreadId);
             * int count = 10;
             * void cb2(UvTimerHandle handle)
             * {
             *  Console.WriteLine("Called!2 {0} ({1})", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
             *  timerHandle.Start(cb2, 2000, 0);
             * }
             * void cb1(UvTimerHandle handle)
             * {
             *  Console.WriteLine("Called!1 {0} ({1})", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
             *  count--;
             *  if (count < 0)
             *    timerHandle.Start(cb2, 2000, 0);
             *  else
             *    timerHandle.Start(cb1, 1000, 0);
             * }
             * timerHandle.Start(cb1, 1000, 0);
             * }, null);
             */

            Console.ReadLine();
        }