Exemplo n.º 1
0
 public TcpServerQuery(TcpServerQbservableProvider <TSource> provider, Expression expression, object argument)
     : base(provider, expression)
 {
     this.argument = argument;
 }
Exemplo n.º 2
0
        private static IObservable <TcpClientTermination> CreateService <TSource, TResult>(
            IPEndPoint endPoint,
            Func <IRemotingFormatter> formatterFactory,
            QbservableServiceOptions options,
            Func <IObservable <TSource>, IQbservable <TResult> > service)
        {
            var listener = new TcpListener(endPoint);

            listener.Start();

            return(Observable
                   .FromAsync(listener.AcceptTcpClientAsync)
                   .Repeat()
                   .Finally(listener.Stop)
                   .SelectMany(client => Observable
                               .StartAsync(async cancel =>
            {
                var watch = Stopwatch.StartNew();

                var localEndPoint = client.Client.LocalEndPoint;
                var remoteEndPoint = client.Client.RemoteEndPoint;

                var exceptions = new List <ExceptionDispatchInfo>();
                var shutdownReason = QbservableProtocolShutdownReason.None;

                try
                {
                    using (var stream = client.GetStream())
                        using (var protocol = await QbservableProtocol.NegotiateServerAsync(stream, formatterFactory(), options, cancel).ConfigureAwait(false))
                        {
                            var provider = new TcpServerQbservableProvider <TResult>(
                                protocol,
                                options,
                                argument =>
                            {
                                if (argument == null && typeof(TSource).IsValueType)
                                {
                                    return service(Observable.Return(default(TSource)));
                                }
                                else
                                {
                                    return service(Observable.Return((TSource)argument));
                                }
                            });

                            try
                            {
                                await protocol.ExecuteServerAsync(provider).ConfigureAwait(false);
                            }
                            catch (OperationCanceledException)
                            {
                            }
                            catch (Exception ex)
                            {
                                exceptions.Add(ExceptionDispatchInfo.Capture(ex));
                            }

                            var protocolExceptions = protocol.Exceptions;

                            if (protocolExceptions != null)
                            {
                                foreach (var exception in protocolExceptions)
                                {
                                    exceptions.Add(exception);
                                }
                            }

                            shutdownReason = protocol.ShutdownReason;
                        }
                }
                catch (OperationCanceledException)
                {
                    shutdownReason = QbservableProtocolShutdownReason.ProtocolNegotiationCanceled;
                }
                catch (Exception ex)
                {
                    shutdownReason = QbservableProtocolShutdownReason.ProtocolNegotiationError;

                    exceptions.Add(ExceptionDispatchInfo.Capture(ex));
                }

                return new TcpClientTermination(localEndPoint, remoteEndPoint, watch.Elapsed, shutdownReason, exceptions);
            })
                               .Finally(client.Close)));
        }