Exemplo n.º 1
0
        //RunAsync is an "echo" type service which reads
        //one message and sends back to the server same data
        public static async Task RunAsync(string[] args)
        {
            using (var client = new ProcessChannel())
            {
                client.StartClient(args[1], args[2]);

                var    cs2 = new CancellationTokenSource();
                var    packetReceiveTask = client.ReceiveAsync(cs2.Token);
                Task[] taskToWait        = { packetReceiveTask, Task.Delay(5000) };
                //Wait up to 5 seconds for the server to call us and then reply back with the same data
                await Task.WhenAny(taskToWait);

                bool timedOut = !packetReceiveTask.IsCompleted;
                if (timedOut)
                {
                    cs2.Cancel();
                    try
                    {
                        await packetReceiveTask;
                    }
                    catch (OperationCanceledException)
                    {
                        // Ignore OperationCanceledException and TaskCanceledException exceptions
                    }
                    catch (AggregateException errors)
                    {
                        // Ignore OperationCanceledException and TaskCanceledException exceptions
                        errors.Handle(e => e is OperationCanceledException);
                    }
                }
                else
                {
                    var message = JsonUtility.FromString <JobRequestMessage>(packetReceiveTask.Result.Body);
                    await client.SendAsync(MessageType.NewJobRequest, JsonUtility.ToString(message), cs2.Token);
                }
            }
        }
Exemplo n.º 2
0
        //RunAsync is an "echo" type service which reads
        //one message and sends back to the server same data 
        public static async Task RunAsync(string[] args)
        {
            using (var client = new ProcessChannel())
            {
                client.StartClient(args[1], args[2]);

                var cs2 = new CancellationTokenSource();
                var packetReceiveTask = client.ReceiveAsync(cs2.Token);
                Task[] taskToWait = { packetReceiveTask, Task.Delay(30*1000)  };
                //Wait up to 5 seconds for the server to call us and then reply back with the same data
                await Task.WhenAny(taskToWait);
                bool timedOut = !packetReceiveTask.IsCompleted;
                if (timedOut)
                {
                    cs2.Cancel();
                    try
                    {
                        await packetReceiveTask;
                    }
                    catch (OperationCanceledException)
                    {
                        // Ignore OperationCanceledException and TaskCanceledException exceptions
                    }
                    catch (AggregateException errors)
                    {
                        // Ignore OperationCanceledException and TaskCanceledException exceptions
                        errors.Handle(e => e is OperationCanceledException);
                    }
                }
                else
                {
                    var message = JsonUtility.FromString<JobRequestMessage>(packetReceiveTask.Result.Body);
                    await client.SendAsync(MessageType.NewJobRequest, JsonUtility.ToString(message), cs2.Token);
                }
            }
        }