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); } }
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); }
public ServiceInstance(string nodeId, IService service, Socket s) { NodeId = nodeId; _logger = RosOutLogManager.GetCurrentNodeLogger(NodeId); _service = service; _client = new TcpRosClient(s); }
public Task <IObservable <byte[]> > StartAsync(Socket socket, bool latching = false) { _client = new TcpRosClient(socket); return(_client.ReceiveAsync() .Take(1) .Timeout(TimeSpan.FromMilliseconds(Ros.TopicTimeout)) .Select(x => OnReceivedHeader(x, latching)) .ToTask()); }
public ServiceProxy(string nodeId, string serviceName, TService service, TcpRosClient client) { NodeId = nodeId; _logger = RosOutLogManager.GetCurrentNodeLogger(NodeId); ServiceName = serviceName; Service = service; _client = client; Service.SetAction(Invoke); Service.Disposing += _ => DisposeAsync(); }
public void ReceiveAsObservable_Success() { var data = new byte[] { 179, 0, 0, 0, 40, 0, 0, 0, 99, 97, 108, 108, 101, 114, 105, 100, 61, 47, 114, 111, 115, 106, 97, 118, 97, 95, 116, 117, 116, 111, 114, 105, 97, 108, 95, 112, 117, 98, 115, 117, 98, 47, 116, 97, 108, 107, 101, 114, 14, 0, 0, 0, 116, 111, 112, 105, 99, 61, 47, 99, 104, 97, 116, 116, 101, 114, 39, 0, 0, 0, 109, 100, 53, 115, 117, 109, 61, 57, 57, 50, 99, 101, 56, 97, 49, 54, 56, 55, 99, 101, 99, 56, 99, 56, 98, 100, 56, 56, 51, 101, 99, 55, 51, 99, 97, 52, 49, 100, 49, 20, 0, 0, 0, 116, 121, 112, 101, 61, 115, 116, 100, 95, 109, 115, 103, 115, 47, 83, 116, 114, 105, 110, 103, 32, 0, 0, 0, 109, 101, 115, 115, 97, 103, 101, 95, 100, 101, 102, 105, 110, 105, 116, 105, 111, 110, 61, 115, 116, 114, 105, 110, 103, 32, 100, 97, 116, 97, 10, 10, 10, 0, 0, 0, 108, 97, 116, 99, 104, 105, 110, 103, 61, 48 }; var scheduler = new TestScheduler(); MAsyncSocketExtensions.ReceiveAsObservableSocketIScheduler = (t1, t2) => scheduler.CreateHotObservable(OnNext(10, data)); var observer = scheduler.CreateObserver <TcpRosHeader>(); var client = new TcpRosClient(); var result = client.ReceiveAsync() .Select(x => TcpRosHeaderSerializer.Deserialize(new MemoryStream(x))) .Subscribe(observer); scheduler.AdvanceTo(10); var header = new { callerid = "/rosjava_tutorial_pubsub/talker", latching = "0", md5sum = "992ce8a1687cec8c8bd883ec73ca41d1", message_definition = "string data\n\n", topic = "/chatter", type = "std_msgs/String", }; observer.Messages.Count.Is(1); dynamic actual = observer.Messages.First().Value.Value; actual.callerid.Is(header.callerid); actual.latching.Is(header.latching); actual.md5sum.Is(header.md5sum); actual.message_definition.Is(header.message_definition); actual.topic.Is(header.topic); actual.type.Is(header.type); }
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(); }
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); }
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)); }
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(); }
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); } }
private Task <ServiceProxy <TService> > ConnectToServiceServer <TService>(string serviceName, TcpRosClient tcpClient) where TService : IService, new() { var tcs = new TaskCompletionSource <ServiceProxy <TService> >(); var receiveHeaderObs = tcpClient.ReceiveAsync() .Select(x => TcpRosHeaderSerializer.Deserialize(new MemoryStream(x))) .Take(1) .PublishLast(); receiveHeaderObs.Connect(); var service = new TService(); var sendHeader = new { callerid = NodeId, md5sum = service.Md5Sum, service = serviceName }; var sendHeaderStream = new MemoryStream(); TcpRosHeaderSerializer.Serialize(sendHeaderStream, sendHeader); tcpClient.SendAsync(sendHeaderStream.ToArray()) .ContinueWith(sendTask => { if (sendTask.Status == TaskStatus.RanToCompletion) { try { var dummy = new TService(); dynamic recvHeader = receiveHeaderObs.Timeout(TimeSpan.FromMilliseconds(Ros.TopicTimeout)).First(); if (recvHeader.HasMember("service") && recvHeader.service != serviceName) { _logger.Error(m => m("ServiceName mismatch error, expected={0} actual={1}", serviceName, recvHeader.topic)); throw new RosTopicException("ServiceName mismatch error"); } if (recvHeader.md5sum != "*" && recvHeader.md5sum != dummy.Md5Sum) { _logger.Error(m => m("MD5Sum mismatch error, expected={0} actual={1}", dummy.Md5Sum, recvHeader.md5sum)); throw new RosTopicException("MD5Sum mismatch error"); } var proxy = new ServiceProxy <TService>(NodeId, serviceName, service, tcpClient); tcs.SetResult(proxy); } catch (Exception ex) { tcs.SetException(ex); } } else if (sendTask.Status == TaskStatus.Faulted) { tcs.SetException(sendTask.Exception.InnerException); } }); return(tcs.Task); }