Exemplo n.º 1
0
        private async Task Start(IObserver <byte[]> observer, CancellationToken token)
        {
            var isException = false;

            try
            {
                var length = new byte[sizeof(int)];
                using (var stream = streamFactory.CreateStream(Address))
                {
                    await stream.ConnectionAsync(token);

                    using (publishSubject.Subscribe(async message => await SendMessageAsync(stream, message, token)))
                    {
                        while (!token.IsCancellationRequested)
                        {
                            var count = await stream.ReadAsync(length, 0, length.Length, token);

                            if (count <= 0 || token.IsCancellationRequested)
                            {
                                return;
                            }

                            var messageLength = BitConverter.ToInt32(length, 0);
                            var message       = new byte[messageLength];
                            count = await stream.ReadAsync(message, 0, messageLength, token);

                            if (count != messageLength || token.IsCancellationRequested)
                            {
                                return;
                            }

                            observer.OnNext(message);
                        }
                    }
                }
            }
            catch (TaskCanceledException)
            {
            }
            catch (Exception ex)
            {
                isException = true;
                publishSubject.OnError(ex);
                throw;
            }
            finally
            {
                if (!isException && !token.IsCancellationRequested)
                {
                    var processTerminatedException = new SandboxTerminatedException();
                    publishSubject.OnError(processTerminatedException);
                    throw processTerminatedException;
                }
            }
        }
Exemplo n.º 2
0
        public void Basic_With_Error()
        {
            var up = new UnicastSubject <int>();

            var to = new TestObserver <int>();

            up.Subscribe(ReactiveExtensions.ToSerialized(to));

            up.EmitError(new InvalidOperationException(), 1, 2, 3, 4, 5);

            to.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);
        }
Exemplo n.º 3
0
        public void Basic()
        {
            var up = new UnicastSubject <int>();

            var to = new TestObserver <int>();

            up.Subscribe(ReactiveExtensions.ToSerialized(to));

            up.EmitAll(1, 2, 3, 4, 5);

            to.AssertResult(1, 2, 3, 4, 5);
        }