コード例 #1
0
        public Task <ServiceProxy <TService> > CreateAsync <TService>(string serviceName, Uri uri)
            where TService : IService, new()
        {
            var tcpClient = new TcpRosClient();

            var tcs = new TaskCompletionSource <ServiceProxy <TService> >();

            tcpClient.ConnectAsync(uri.Host, uri.Port)
            .ContinueWith(connectTask =>
            {
                if (connectTask.Status == TaskStatus.RanToCompletion)
                {
                    ConnectToServiceServer <TService>(serviceName, tcpClient)
                    .ContinueWith(serviceTask =>
                    {
                        if (serviceTask.Status == TaskStatus.RanToCompletion)
                        {
                            tcs.SetResult(serviceTask.Result);
                        }
                        else if (serviceTask.Status == TaskStatus.Faulted)
                        {
                            tcs.SetException(serviceTask.Exception.InnerException);
                        }
                    });
                }
                else if (connectTask.Status == TaskStatus.Faulted)
                {
                    tcs.SetException(connectTask.Exception.InnerException);
                }
            });

            return(tcs.Task);
        }
コード例 #2
0
        public void DisposeError()
        {
            var listener = new TcpRosListener(0);

            listener.AcceptAsync().Subscribe(
                x => Console.WriteLine("new socket = {0}", x.RemoteEndPoint),
                ex => Console.WriteLine("accept error = {0}", ex.Message));


            int port = listener.EndPoint.Port;

            listener.Dispose();

            Console.WriteLine("Press Any Key 3");
            Console.ReadKey();

            var client3 = new TcpRosClient();

            try
            {
                client3.ConnectAsync("localhost", port).Wait();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("agg={0}", ex.Message);
            }
        }
コード例 #3
0
        public void ConnectAsObservable_Success()
        {
            var arg = new SocketAsyncEventArgs();

            var task = new Task(() => { });

            MAsyncSocketExtensions.ConnectTaskAsyncSocketEndPoint = (t1, t2) => task;

            var client = new TcpRosClient();

            client.ConnectAsync("127.0.0.1", 50000).ContinueWith(t => { });

            task.Start();
        }
コード例 #4
0
        public Task <IObservable <TMessage> > StartAsync(TopicParam param, bool nodelay = true)
        {
            _client = new TcpRosClient();

            var tcs = new TaskCompletionSource <IObservable <TMessage> >();

            _client.ConnectAsync(param.HostName, param.PortNumber)
            .ContinueWith(connectTask =>
            {
                _logger.Debug("StartAsync Connected");

                if (connectTask.Status == TaskStatus.RanToCompletion)
                {
                    try
                    {
                        ConnectToPublisherAsync(nodelay).ContinueWith(connectedTask =>
                        {
                            _logger.Debug("StartAsync ConnectToPublisherAsync");

                            if (connectedTask.Status == TaskStatus.RanToCompletion)
                            {
                                tcs.SetResult(connectedTask.Result);
                            }
                            else if (connectedTask.Status == TaskStatus.Faulted)
                            {
                                tcs.SetException(connectedTask.Exception.InnerException);
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Connect Error", ex);
                        tcs.SetException(ex);
                    }
                }
                else if (connectTask.Status == TaskStatus.Faulted)
                {
                    tcs.SetException(connectTask.Exception.InnerException);
                }
            });

            return(tcs.Task);
        }
コード例 #5
0
        public void MultipleConnection()
        {
            var listener = new TcpRosListener(0);

            listener.AcceptAsync().Subscribe(
                x => Console.WriteLine("new socket = {0}", x.RemoteEndPoint),
                ex => Console.WriteLine("accept error = {0}", ex.Message));

            int port    = listener.EndPoint.Port;
            var client1 = new TcpRosClient();

            client1.ConnectAsync("localhost", port)
            .ContinueWith(t => Console.WriteLine("task.ex={0}", t.Exception));
            var client2 = new TcpRosClient();

            client2.ConnectAsync("localhost", port)
            .ContinueWith(t => Console.WriteLine("task.ex={0}", t.Exception));
            var client3 = new TcpRosClient();

            client3.ConnectAsync("localhost", port)
            .ContinueWith(t => Console.WriteLine("task.ex={0}", t.Exception));
        }
コード例 #6
0
        public void SendAndReceive()
        {
            var listener = new TcpRosListener(0);

            listener.AcceptAsync()
            .Do(_ => Console.WriteLine("Connected"))
            .Subscribe(s => new TcpRosClient(s).ReceiveAsync()
                       .Subscribe(data => Console.WriteLine("Received:{0}", data)));

            int port = listener.EndPoint.Port;

            Console.WriteLine("Press Any Key 3");
            Console.ReadKey();

            var client3 = new TcpRosClient();

            client3.ConnectAsync("localhost", port).Wait();

            var sendData = new byte[] { 1, 0, 0, 0, 1 };

            client3.SendAsync(sendData).Wait();
        }
コード例 #7
0
        public void Receive_Error()
        {
            var listener = new TcpRosListener(0);

            listener.AcceptAsync()
            .Do(_ => Console.WriteLine("Connected"))
            .Subscribe(s =>
            {
                new TcpRosClient(s).ReceiveAsync()
                .Subscribe(data => Console.WriteLine("Received:{0}", data),
                           ex => Console.WriteLine("Received Error: {0}", ex.Message));
                Thread.Sleep(TimeSpan.FromSeconds(3));
                s.Close();    //3秒待って閉じる。
            },
                       ex => Console.WriteLine("Receive Error = {0}", ex.Message));

            int port = listener.EndPoint.Port;

            var client3 = new TcpRosClient();

            client3.ConnectAsync("localhost", port).Wait();

            var sendData = new byte[] { 1, 0, 0, 0, 1 };

            client3.SendAsync(sendData).Wait();

            Thread.Sleep(TimeSpan.FromSeconds(5));

            try
            {
                client3.SendAsync(sendData).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Send Error:{0}", ex.Message);
            }
        }