public static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;
            var decoder  = new DynamicFrameDecoder(0xA0, 0XC0);

            var cts = new CancellationTokenSource();

            endpoint.ToListenerObservable(10)
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(
                client =>
                //client.ToClientObservable(1024, SocketFlags.None)
                client.ToDynamicFrameObservable(bmgr, decoder)
                .Subscribe(async buf => await OnReceived(client, buf, cts.Token),
                           SocketReceiveError,
                           ClientSocketClose, cts.Token),
                error => Console.WriteLine("Error: " + error.Message),
                () => Console.WriteLine("OnCompleted"),
                cts.Token);

            Console.WriteLine("Press <ENTER> to quit");
            Console.ReadLine();

            cts.Cancel();
        }
示例#2
0
        public static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts           = new CancellationTokenSource();
            var bufferManager = BufferManager.CreateBufferManager(2 << 16, 2 << 8);

            endpoint.ToConnectObservable()
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(socket =>
            {
                var frameClientSubject = socket.ToFrameClientSubject(SocketFlags.None, bufferManager, cts.Token);

                var observerDisposable =
                    frameClientSubject
                    .ObserveOn(TaskPoolScheduler.Default)
                    .Subscribe(
                        managedBuffer =>
                {
                    var buf = managedBuffer.Value;
                    if (buf.Array != null)
                    {
                        Console.WriteLine("Read: " + Encoding.UTF8.GetString(buf.Array, buf.Offset, buf.Count));
                    }
                    managedBuffer.Dispose();
                },
                        error => Console.WriteLine("Error: " + error.Message),
                        () => Console.WriteLine("OnCompleted: FrameReceiver"));

                Console.In.ToLineObservable()
                .Subscribe(
                    line =>
                {
                    var writeBuffer = Encoding.UTF8.GetBytes(line);
                    frameClientSubject.OnNext(DisposableValue.Create(new ArraySegment <byte>(writeBuffer), Disposable.Empty));
                },
                    error => Console.WriteLine("Error: " + error.Message),
                    () => Console.WriteLine("OnCompleted: LineReader"));
                cts.Token.WaitHandle.WaitOne();
                //Thread.Sleep(10*1000);
                observerDisposable.Dispose();

                cts.Cancel();
            },
                       error => Console.WriteLine("Failed to connect: " + error.Message),
                       cts.Token);

            cts.Token.WaitHandle.WaitOne();
        }
示例#3
0
        static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts           = new CancellationTokenSource();
            var bufferManager = BufferManager.CreateBufferManager(2 << 16, 2 << 8);

            endpoint.ToConnectObservable()
            .Subscribe(client =>
            {
                var frameClientSubject = client.ToFrameClientSubject(bufferManager, cts.Token);

                var observerDisposable =
                    frameClientSubject
                    .ObserveOn(TaskPoolScheduler.Default)
                    .Subscribe(
                        disposableBuffer =>
                {
                    Console.WriteLine("Read: " + Encoding.UTF8.GetString(disposableBuffer.Bytes, 0, disposableBuffer.Length));
                    disposableBuffer.Dispose();
                },
                        error => Console.WriteLine("Error: " + error.Message),
                        () => Console.WriteLine("OnCompleted: FrameReceiver"));

                Console.In.ToLineObservable()
                .Subscribe(
                    line =>
                {
                    var writeBuffer = Encoding.UTF8.GetBytes(line);
                    frameClientSubject.OnNext(new DisposableByteBuffer(writeBuffer, writeBuffer.Length, Disposable.Empty));
                },
                    error => Console.WriteLine("Error: " + error.Message),
                    () => Console.WriteLine("OnCompleted: LineReader"));

                observerDisposable.Dispose();

                cts.Cancel();
            },
                       error =>
            {
                Console.WriteLine("Failed to connect: " + error.Message);
                Environment.Exit(-1);
            });

            cts.Token.WaitHandle.WaitOne();
        }
示例#4
0
        static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts           = new CancellationTokenSource();
            var bufferManager = BufferManager.CreateBufferManager(2 << 16, 2 << 8);

            var selector = new Selector();

            Task.Factory.StartNew(() => selector.Dispatch(60000000, cts.Token), cts.Token);

            endpoint.ToConnectObservable(selector, cts.Token)
            .Subscribe(socket =>
            {
                var frameClientSubject = socket.ToFrameClientSubject(SocketFlags.None, bufferManager, selector, cts.Token);

                var observerDisposable =
                    frameClientSubject
                    .ObserveOn(TaskPoolScheduler.Default)
                    .Subscribe(
                        disposableBuffer =>
                {
                    Console.WriteLine("Read: " + Encoding.UTF8.GetString(disposableBuffer.Value.Array, 0, disposableBuffer.Value.Count));
                    disposableBuffer.Dispose();
                },
                        error => Console.WriteLine("Error: " + error.Message),
                        () => Console.WriteLine("OnCompleted: FrameReceiver"));

                Console.In.ToLineObservable()
                .Subscribe(
                    line =>
                {
                    var writeBuffer = Encoding.UTF8.GetBytes(line);
                    frameClientSubject.OnNext(DisposableValue.Create(new ArraySegment <byte>(writeBuffer, 0, writeBuffer.Length), Disposable.Empty));
                },
                    error => Console.WriteLine("Error: " + error.Message),
                    () => Console.WriteLine("OnCompleted: LineReader"));

                observerDisposable.Dispose();

                cts.Cancel();
            });

            cts.Token.WaitHandle.WaitOne();
        }
示例#5
0
        static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts = new CancellationTokenSource();

            endpoint.ToListenerObservable(10)
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(
                client =>
                client.ToClientObservable(1024)
                .Subscribe(client.ToClientObserver(cts.Token), cts.Token),
                error => Console.WriteLine("Error: " + error.Message),
                () => Console.WriteLine("OnCompleted"),
                cts.Token);

            Console.WriteLine("Press <ENTER> to quit");
            Console.ReadLine();

            cts.Cancel();
        }
示例#6
0
        static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts      = new CancellationTokenSource();
            var selector = new Selector();

            endpoint.ToListenerObservable(10, selector)
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(
                client =>
                client.ToClientObservable(1024, SocketFlags.None, selector)
                .Subscribe(client.ToClientObserver(SocketFlags.None, selector, cts.Token), cts.Token),
                error => Console.WriteLine("Error: " + error.Message),
                () => Console.WriteLine("OnCompleted"),
                cts.Token);

            Task.Factory.StartNew(() => selector.Dispatch(60000000, cts.Token), cts.Token);

            Console.WriteLine("Press <ENTER> to quit");
            Console.ReadLine();

            cts.Cancel();
        }
        private static void OldTest(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts           = new CancellationTokenSource();
            var bufferManager = SyncBufManager.Create(2 << 16, 128, 32);
            var encoder       = new ClientFrameEncoder(0xA0, 0XC0);
            var decoder       = new FixedLenFrameDecoder(0xA0, 0XC0);

            endpoint.ToConnectObservable()
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(socket =>
            {
                var frameClientSubject =
                    socket.ToFixedLenFrameSubject(encoder, decoder, bufferManager, cts.Token);

                var observerDisposable =
                    frameClientSubject
                    .ObserveOn(TaskPoolScheduler.Default)
                    .Subscribe(
                        managedBuffer =>
                {
                    var segment = managedBuffer.Value;
                    if (!ReceivedDataProcessor.Instance.Process(segment) && segment.Array != null)
                    {
                        Console.WriteLine(
                            "Echo: " + Encoding.UTF8.GetString(segment.Array, segment.Offset,
                                                               segment.Count));
                    }
                    managedBuffer.Dispose();
                },
                        error =>
                {
                    Console.WriteLine("Error: " + error.Message);
                    cts.Cancel();
                },
                        () =>
                {
                    Console.WriteLine("OnCompleted: Frame Protocol Receiver");
                    cts.Cancel();
                });

                var cmdSender = new DevCommandSender(frameClientSubject, bufferManager, null);

                Console.In.ToLineObservable("exit")
                .Subscribe(
                    line =>
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        return;
                    }
                    if (line == "start")
                    {
                        cmdSender.Start();
                        return;
                    }
                    if (line == "stop")
                    {
                        cmdSender.Stop();
                        return;
                    }
                    var writeBuffer = Encoding.UTF8.GetBytes(line);
                    frameClientSubject.OnNext(
                        DisposableValue.Create(new ArraySegment <byte>(writeBuffer), Disposable.Empty));
                },
                    error =>
                {
                    Console.WriteLine("Error: " + error.Message);
                    cts.Cancel();
                },
                    () =>
                {
                    Console.WriteLine("OnCompleted: LineReader");
                    cts.Cancel();
                });

                cts.Token.WaitHandle.WaitOne();
                observerDisposable.Dispose();
            },
                       error =>
            {
                Console.WriteLine("Failed to connect: " + error.Message);
                cts.Cancel();
            },
                       cts.Token);

            cts.Token.WaitHandle.WaitOne();
        }
示例#8
0
        public static void Main(string[] args)
        {
            var endpoint = ProgramArgs.Parse(args, new[] { "127.0.0.1:9211" }).EndPoint;

            var cts           = new CancellationTokenSource();
            var bufferManager = BufferManager.CreateBufferManager(2 << 16, 2 << 8);
            var encoder       = new EchoProtocolEncoder();
            var decoder       = new EchoProtocolDecoder();

            endpoint.ToConnectObservable()
            .ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(socket =>
            {
                var frameClientSubject = socket.ToFrameClientSubject(encoder, decoder, bufferManager, cts.Token);

                var observerDisposable =
                    frameClientSubject
                    .ObserveOn(TaskPoolScheduler.Default)
                    .Subscribe(
                        managedBuffer =>
                {
                    var segment = managedBuffer.Value;
                    if (segment.Array != null)
                    {
                        Console.WriteLine(
                            "Echo: " + Encoding.UTF8.GetString(segment.Array, segment.Offset,
                                                               segment.Count));
                    }
                    managedBuffer.Dispose();
                },
                        error =>
                {
                    Console.WriteLine("Error: " + error.Message);
                    cts.Cancel();
                },
                        () =>
                {
                    Console.WriteLine("OnCompleted: Frame Protocol Receiver");
                    cts.Cancel();
                });

                Console.In.ToLineObservable("exit")
                .Subscribe(
                    line =>
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        return;
                    }
                    var writeBuffer = Encoding.UTF8.GetBytes(line);
                    frameClientSubject.OnNext(DisposableValue.Create(new ArraySegment <byte>(writeBuffer), Disposable.Empty));
                },
                    error =>
                {
                    Console.WriteLine("Error: " + error.Message);
                    cts.Cancel();
                },
                    () =>
                {
                    Console.WriteLine("OnCompleted: LineReader");
                    cts.Cancel();
                });

                cts.Token.WaitHandle.WaitOne();
                observerDisposable.Dispose();
            },
                       error =>
            {
                Console.WriteLine("Failed to connect: " + error.Message);
                cts.Cancel();
            },
                       cts.Token);

            cts.Token.WaitHandle.WaitOne();
        }