예제 #1
0
        static void Run(Options options)
        {
            var fragmentCount = 0L;

            try
            {
                using var aeron = Aeron.Connect();
                Console.WriteLine($"Connected to Aeron {aeron.ClientId.ToString()}");
                using var subscription = aeron.AddSubscription(options.Channel, options.StreamId);
                using var cts          = new CancellationTokenSource();
                var messageDecoder  = new MessageDecoder();
                var messageReceiver = new MessageReceiver(subscription, messageDecoder, OnDataValue);

                var ct = cts.Token;

                var pollingTask = Task.Run(async() =>
                {
                    while (!ct.IsCancellationRequested)
                    {
                        var fragmentsProcessed = messageReceiver.Poll();
                        if (fragmentsProcessed == 0)
                        {
                            await Task.Delay(TimeSpan.FromMilliseconds(10), ct);
                        }
                        else
                        {
                            fragmentCount += fragmentsProcessed;
                        }
                    }
                }, ct);


                Console.CancelKeyPress += (o, args) =>
                {
                    args.Cancel = true;
                    Console.Out.WriteLine("Cancel key event intercepted");
                    cts.Cancel();
                };

                pollingTask.GetAwaiter().GetResult();
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unhandled Exception: {e}");
            }
            finally
            {
                Console.Out.WriteLine($"Received {fragmentCount} fragments");
                Console.WriteLine("Subscriber finished");
            }
        }
예제 #2
0
 public MessageReceiver(Subscription subscription, MessageDecoder decoder, Action <DataValue> superFastCallback)
 {
     _subscription      = subscription;
     _decoder           = decoder;
     _superFastCallback = superFastCallback;
 }