예제 #1
0
        /// <summary>
        /// Attemps to connect to the TCP server.
        /// </summary>
        public IObservable <Unit> ConnectAsync()
        {
            var client = new TcpClient();

            return(ObservableEx
                   .FromAsyncPattern <string, int>(client.BeginConnect, client.EndConnect)(hostname, port)
                   .Do(_ => Connect(client)));
        }
예제 #2
0
        /// <summary>
        /// Attemps to connect to the TCP server.
        /// </summary>
        public IObservable <Unit> ConnectAsync()
        {
            TcpClient client = AddressFamily.InterNetwork != this.addressFamily ? new TcpClient(this.addressFamily) : new TcpClient();

            return(ObservableEx
                   .FromAsyncPattern <string, int>(client.BeginConnect, client.EndConnect)(hostname, port)
                   .Do(_ => Connect(client)));
        }
예제 #3
0
        /// <summary>
        /// Starts accepting connections.
        /// </summary>
        public void Start()
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            // This is equivalent to the behavior of TcpListener.Create in .NET 4.5.
            listener = new Net.TcpListener(IPAddress.Any, port);
            //listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);

            listener.Start();

            tracer.ReactiveListenerStarted(port);

            listenerSubscription = Observable
                                   .Defer(() =>
            {
                tracer.ReactiveListenerAwaitingNewTcpConnection();
                return(ObservableEx.FromAsyncPattern <TcpClient>(listener.BeginAcceptTcpClient, listener.EndAcceptTcpClient)());
            })
                                   .Repeat()
                                   .Select(client => new ReactiveSocket(client))
                                   .Subscribe(socket =>
            {
                connections.Add(socket);
                observable.OnNext(socket);

                IDisposable disposeSubscription = Observable.FromEventPattern <EventHandler, EventArgs>(
                    h => h.Invoke,
                    h => socket.Disposed += h, h => socket.Disposed -= h)
                                                  .First().Subscribe(x =>
                {
                    tracer.ReactiveListenerRemovingDisposedSocket();
                    connections.Remove(socket);
                });

                this.socketDisposable.Add(disposeSubscription);
            });
        }
예제 #4
0
        private void BeginRead()
        {
            Stream stream = this.GetStream();

            this.readSubscription = Observable.Defer(() =>
            {
                var buffer = new byte[this.ReceiveBufferSize];

                return(ObservableEx.FromAsyncPattern <byte[], int, int, int>(stream.BeginRead, stream.EndRead)(buffer, 0, buffer.Length)
                       .Select(x => buffer.Take(x).ToArray()));
            })
                                    .Repeat()
                                    .TakeWhile(x => x.Any())
                                    .Subscribe(xs => { foreach (var x in xs)
                                                       {
                                                           this.received.Add(x);
                                                       }
                                               }, ex =>
            {
                tracer.ReactiveSocketReadFailed(ex);
                Disconnect(false);
            }, () => Disconnect(false));
        }