private IDisposable StartHost() { return((from client in ObservableTcpListener.Start(IPAddress.Loopback, port, maxConcurrent: 1).Take(1) from _ in Observable.Using( () => client.GetStream(), stream => Observable.Create <Unit>((observer, cancel) => SendData(stream, observer, cancel))) select Unit.Default) .Subscribe(_ => { }, () => TraceStatus("Host Completed."))); }
protected override void Main() { var endPoint = new IPEndPoint(IPAddress.Loopback, 15005); IObservable <int> clientQuery = Observable.Using( () => new TcpClient(), client => from _ in client.ConnectObservable(endPoint.Address, endPoint.Port) let socket = client.Client let message = Encoding.UTF8.GetBytes(Text.Rock + Text.Scissors + Text.Paper) from __ in socket.SendUntilCompleted(message, 0, message.Length, SocketFlags.None) select message.Length); IObservable <string> serverQuery = from client in ObservableTcpListener.Start(endPoint) .Synchronize() .Take(1) .Do(_ => { TraceLine(Text.ServerReceivedRequest); PressAnyKeyToContinue(); }) let stream = new NetworkStream(client.Client, ownsSocket: false) from buffer in Observable.Using( () => client, _ => stream.ReadToEndObservable().SelectMany(b => b).ToArray()) let message = Encoding.UTF8.GetString(buffer) select message; using (var responded = new System.Threading.ManualResetEvent(false)) using (serverQuery.Finally(() => responded.Set()).Subscribe(ConsoleOutput(Text.Server))) using (clientQuery.Subscribe(ConsoleOutputFormat(Text.Client, Text.SentBytesFormat))) { responded.WaitOne(); } }
private void StartListeningAway() { this.serverSubscription = ObservableTcpListener.Start(this.CurrentNetworkInterface.Item2, 443, 1) .Subscribe(OnAwayClientConnected); }
private static IObservable <TcpClientTermination> CreateService <TSource, TResult>( IPEndPoint endPoint, Func <IRemotingFormatter> formatterFactory, QbservableServiceOptions options, Func <IObservable <TSource>, IQbservable <TResult> > service) { return(ObservableTcpListener .Start(endPoint, maxConcurrent: 1) // maxConcurrent must be 1 to ensure thread-safety. See the Start method's remarks for details. .SelectMany(client => Observable .StartAsync(async cancel => { var watch = Stopwatch.StartNew(); // disable Nagle algorithm so that observed events are sent as soon as they happen client.NoDelay = true; 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.ProtocolNegotiationCancelled; } catch (Exception ex) { shutDownReason = QbservableProtocolShutDownReason.ProtocolNegotiationError; exceptions.Add(ExceptionDispatchInfo.Capture(ex)); } return new TcpClientTermination(localEndPoint, remoteEndPoint, watch.Elapsed, shutDownReason, exceptions); }) .Finally(client.Close))); }