Esempio n. 1
0
        public static void Main()
        {
            var ctx = new Aeron.Context()
                .AvailableImageHandler(SamplesUtil.PrintUnavailableImage)
                .UnavailableImageHandler(SamplesUtil.PrintUnavailableImage);
            
            IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

            Console.WriteLine("Subscribing Ping at " + PingChannel + " on stream Id " + PingStreamID);
            Console.WriteLine("Publishing Pong at " + PongChannel + " on stream Id " + PongStreamID);

            var running = new AtomicBoolean(true);
            Console.CancelKeyPress += (_, e) => running.Set(false);

            using (var aeron = Aeron.Connect(ctx))
            using (var pongPublication = aeron.AddPublication(PongChannel, PongStreamID))
            using (var pingSubscription = aeron.AddSubscription(PingChannel, PingStreamID))
            {
                FragmentHandler dataHandler = (buffer, offset, length, header) => PingHandler(pongPublication, buffer, offset, length);

                while (running.Get())
                {
                    idleStrategy.Idle(pingSubscription.Poll(dataHandler, FrameCountLimit));
                }

                Console.WriteLine("Shutting down...");
            }
        }
Esempio n. 2
0
        public static void Main()
        {
            // Maximum number of message fragments to receive during a single 'poll' operation
            const int fragmentLimitCount = 10;

            // The channel (an endpoint identifier) to receive messages from
            const string channel = "aeron:udp?endpoint=localhost:40123";

            // A unique identifier for a stream within a channel. Stream ID 0 is reserved
            // for internal use and should not be used by applications.
            const int streamId = 10;

            Console.WriteLine("Subscribing to " + channel + " on stream Id " + streamId);

            var running = new AtomicBoolean(true);
            // Register a SIGINT handler for graceful shutdown.
            Console.CancelKeyPress += (s, e) => running.Set(false);

            // dataHandler method is called for every new datagram received
            FragmentHandler fragmentHandler = (buffer, offset, length, header) =>
            {
                var data = new byte[length];
                buffer.GetBytes(offset, data);

                Console.WriteLine($"Received message ({Encoding.UTF8.GetString(data)}) to stream {streamId:D} from session {header.SessionId:x} term id {header.TermId:x} term offset {header.TermOffset:D} ({length:D}@{offset:D})");

                // Received the intended message, time to exit the program
                running.Set(false);
            };

            // Create a context, needed for client connection to media driver
            // A separate media driver process need to run prior to running this application
            var ctx = new Aeron.Context();

            // Create an Aeron instance with client-provided context configuration, connect to the
            // media driver, and add a subscription for the given channel and stream using the supplied
            // dataHandler method, which will be called with new messages as they are received.
            // The Aeron and Subscription classes implement AutoCloseable, and will automatically
            // clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(ctx))
            using (var subscription = aeron.AddSubscription(channel, streamId))
            {
                IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

                // Try to read the data from subscriber
                while (running.Get())
                {
                    // poll delivers messages to the dataHandler as they arrive
                    // and returns number of fragments read, or 0
                    // if no data is available.
                    var fragmentsRead = subscription.Poll(fragmentHandler, fragmentLimitCount);
                    // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
                    // use if no messages were received.
                    idleStrategy.Idle(fragmentsRead);
                }

                Console.WriteLine("Press any key...");
                Console.ReadLine();
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Return a reusable, parameterized event loop that calls and idler when no messages are received
 /// </summary>
 /// <param name="fragmentHandler"> to be called back for each message. </param>
 /// <param name="limit">           passed to <seealso cref="Subscription#poll(FragmentHandler, int)"/> </param>
 /// <param name="running">         indication for loop </param>
 /// <param name="idleStrategy">    to use for loop </param>
 /// <returns> loop function </returns>
 public static Action<Subscription> SubscriberLoop(FragmentHandler fragmentHandler, int limit, AtomicBoolean running, IIdleStrategy idleStrategy)
 {
     return subscription =>
     {
         while (running.Get())
         {
             idleStrategy.Idle(subscription.Poll(fragmentHandler, limit));
         }
     };
 }
Esempio n. 4
0
        /// <summary>
        /// Return a reusable, parameterised event loop that calls a default idler when no messages are received
        /// </summary>
        /// <param name="fragmentHandler"> to be called back for each message. </param>
        /// <param name="limit">           passed to <seealso cref="Subscription#poll(FragmentHandler, int)"/> </param>
        /// <param name="running">         indication for loop </param>
        /// <returns> loop function </returns>
        public static Action<Subscription> SubscriberLoop(FragmentHandler fragmentHandler, int limit, AtomicBoolean running)
        {
            IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

            return SubscriberLoop(fragmentHandler, limit, running, idleStrategy);
        }